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

#define pb push_back
#define fr first
#define sc second
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> pip; 
const int INF = (1<<25);
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main() {
  int n, c;
  int dp[31][1<<16];
  int a[31], b[31];
  
  while(cin>> n>> c, n) {
    for(int i=0; i<n; i++) {
      a[i] = 0;
      for(int j=0; j<16; j++) {
	int bit;
	cin>> bit;
	a[i] = (a[i]<<1)+bit;
	
      }
    }
    
    for(int i=0; i<c; i++) {
      b[i] = 0;
      for(int j=0; j<16; j++) {
	int bit;
	cin>> bit;
	b[i] = (b[i]<<1)+bit;
	
      }
    }
    
    for(int i=0; i<n+1; i++) {
      for(int j=0; j<(1<<16); j++) {
	dp[i][j] = -1;
      }
    }
    
    dp[0][0]=0;
    for(int i=0; i<n; i++) {
      for(int j=0; j<(1<<16); j++) {
	if(dp[i][j] == -1) continue;
	int panel = (a[i] | j);
	for(int k=0; k<c; k++) {
	  int push_panel = (panel & b[k]);
	  int score = __builtin_popcount(push_panel);
	  dp[i+1][panel-push_panel] = max(dp[i+1][panel-push_panel],dp[i][j]+score);
	}
      }
    }

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

    cout<< ans<< endl;
  }
}