#include<bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define lcm( a, b ) ((a)/__gcd(a,b)*(b))
#define MP make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef pair< LL, LL > L_P;

const int INF = INT_MAX;
const int MIN = INT_MIN;
const LL LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const int SIZE = 100005;


int st[35][(1<<16)]; //���׻�
int a[31]; //������
int b[31]; //������
int dp[31][(1<<16)];
        
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while( true ) {
        int n, c;
        cin >> n >> c;
        if( n == 0 ) break;
        for( int i = 0; i < 31; ++i ) a[i] = 0;
        for( int i = 0; i < 31; ++i ) b[i] = 0;

        for( int i = 0; i < 35; ++i ) {
            for( int j = 0; j < (1<<16); ++j ) st[i][j] = 0;

        }
        for( int i = 0; i < 31; ++i ) {
            for( int j = 0; j < (1<<16); ++j ) dp[i][j] = MIN;

        }

        for( int i = 0; i < n; ++i ) {
            for( int j = 0; j < 16; ++j ) {
                int in;
                cin >> in;
                a[i] |= ( in << j );

            }
        }

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

            }
        }

        //���׻�
        for( int i = 0; i < n; ++i ) {
            for( int j = 0; j < (1<<16); ++j ) {
                int score = 0;
                unsigned int bits = j & a[i];
                while( bits ) {
                    if( bits & 1 ) ++score;
                    bits >>= 1;
        
                }
                st[i][j] = score;

            }
        }
        /*
        for( int i = 0; i < n; ++i ) {
            for( int j = 0; j < (1<<16); ++j ) {
                if( dp[i % 2][j] < 0 ) continue;
                int nx = ( j | a[i + 1] );
                dp[(i + 1) % 2][nx] = max( dp[(i + 1) % 2][nx], dp[i % 2][j] );

                for( int k = 0; k < c; ++k ) {
                    int tx = ( nx - ( nx & b[k] ) );
                    dp[(i + 1) % 2][tx] = max( dp[(i + 1) % 2][tx], 
                                                dp[i % 2][j] + st[k][nx] );
                }
            }
        }*/

        dp[0][0] = 0;
        for( int i = 0; i < n; ++i ) {
            for( int j = 0; j < (1<<16); ++j ) {
                int nx = ( j | a[i] );
                dp[i + 1][nx] = max( dp[i + 1][nx], dp[i][j] );

                for( int k = 0; k < c; ++k ) {
                    int tx = ( nx & ~( nx & b[k] ) );
                    dp[i + 1][tx] = max( dp[i + 1][tx], dp[i][j] + __builtin_popcount( nx & b[k] ) );

                }
            }
        }

        int res = 0;
        for( int i = 0; i < (1<<16); ++i ) {
            res = max( res, dp[n][i] );

        }
        cout << res << endl;

    }

    return 0;
}

