#include <bits/stdc++.h>
using namespace std;

#define int long long

int n, c;
int a[40] = {};
int b[40] = {};

int memo[32][70010];

int dp(int idx, int bit){
    if(idx == n){
        return 0;
    }

    if(memo[idx][bit] != -1) return memo[idx][bit];

    int ttt = bit;
    bit |= a[idx];

    int ans = dp(idx+1, bit);

    for(int i = 0;i < c;i++){
        int tmp = (bit & b[i]);

        int cnt = 0;
        while(tmp > 0){
            cnt += (tmp & 1);
            tmp >>= 1;
        }

        ans = max(ans, dp(idx+1, bit & (~b[i])) + cnt);
    }

    return memo[idx][ttt] = ans;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    while(cin >> n >> c, n != 0 && c != 0){
        for(int i = 0;i < n;i++){
            a[i] = 0;
            for(int j = 0;j < 16;j++){
                int tmp;
                cin >> tmp;
                a[i] |= (tmp << j);
            }
        }

        for(int i = 0;i < c;i++){
            b[i] = 0;
            for(int j = 0;j < 16;j++){
                int tmp;
                cin >> tmp;
                b[i] |= (tmp << j);
            }
        }

        memset(memo, -1, sizeof(memo));

        cout << dp(0, 0) << endl;
    }
    return 0;
}


