import std.stdio;
import std.format;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

void main() {
    int N, C;
    int[] A, B;
    bool input() {
        scanf("%d %d\n", &N, &C);
        if (N == 0 && C == 0) return false;
        A = new int[N];
        B = new int[C];
        foreach (i; 0 .. N) {
            int x;
            string s = readln.chomp.split(" ").join("");
            formattedRead(s, "%b", &x);
            A[i] = x;
        }
        foreach (i; 0 .. C) {
            int x;
            string s = readln.chomp.split(" ").join("");
            formattedRead(s, "%b", &x);
            B[i] = x;
        }
        return true;
    }
    import core.bitop;
    void solve() {
        int dfs(int bit, int index) {
            if (index == N) return 0;
            bit |= A[index];
            int Ret = dfs(bit, index + 1);
            foreach (b; B) {
                Ret = max(Ret, dfs(bit & ~b, index + 1) + (bit & b).popcnt);
            }
            return Ret;
        }
        writeln(dfs(0, 0));
    }
    while (input) solve;
}