#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n, c, dp[31][1<<16 + 1];
int npt[32], cpt[31];

int rec(int idx, int now){
  if(idx == n) return 0;
  if(dp[idx][now]) return dp[idx][now];
  
  int ret = 0;
  for(int i = 0; i < c; i++){
    ret = max(ret, rec(idx+1, (now & ~cpt[i]) | npt[idx+1]) + __builtin_popcount(now & cpt[i]));
  }
  
  return dp[idx][now] = ret;

}

int main(){
  
  while(cin >> n >> c, n||c){
    
    for(int i = 0; i < n; i++){
      for(int j = 0; j < 16; j++){
	int bit; cin >> bit;
	if(bit) npt[i] |= 1;
	npt[i] <<= 1;
      } npt[i] >>= 1;
    }
    for(int i = 0; i < c; i++){
      for(int j = 0; j < 16; j++){
	int bit; cin >> bit;
	if(bit) cpt[i] |= 1;
	cpt[i] <<= 1;
      } cpt[i] >>= 1;
    }

    npt[n] = 0;
    cout << rec(0, npt[0]) << endl;
    memset(dp, 0, sizeof(dp));
    memset(npt, 0, sizeof(npt));
    memset(cpt, 0, sizeof(cpt));

  }
}