#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; i++ )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
const int inf=1e9+7;
const ll longinf=1LL<<60;
const ll mod=1e9+7;
int dx[4]={1,0,-1,0} , dy[4]={0,1,0,-1};

int main(){
    int n,m;
    while(cin>>n>>m,n!=0&&m!=0){
        int a[n],c[m];
        rep(i,n){
            a[i]=0;
            rep(j,16){
                a[i]*=2;
                int x;cin>>x;
                a[i]+=x;
            }
        }
        rep(i,m){
            c[i]=0;
            rep(j,16){
                c[i]*=2;
                int x;cin>>x;
                c[i]+=x;
            }
        }
        int dp[n+1][65536];
        rep(i,n+1)rep(j,65536)dp[i][j]=0;
        for(int i=n-1;i>=0;i--)rep(j,65536){
            int pos=j|a[i];
            rep(k,m){
                int res=pos&c[k];
                dp[i][j]=max(dp[i][j],__builtin_popcount(res)+dp[i+1][pos^res]);
            }
        }
        cout<<dp[0][0]<<endl;
    }
    return 0;
}
