#include <iostream>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cstring>
#include <map>
#include <cassert>
using namespace std;

int dp[31][1<<16];

int main(){
	int n,c;
	while(cin >> n >> c  && n){
		vector<int> a(n),b(c);
		for(int i = 0 ; i < n ; i++){	
			int t = 0;
			for(int j = 0 ; j < 16 ; j++){
				int p; cin >> p; t = t * 2 + p;
			}
			a[i] = t;
		}
		
		for(int i = 0 ; i < c ; i++){	
			int t = 0;
			for(int j = 0 ; j < 16 ; j++){
				int p; cin >> p; t = t * 2 + p;
			}
			b[i] = t;
		}
		for(int i = 0 ; i < 31 ; i++)
			for(int j = 0 ; j < (1<<16) ; j++)
				dp[i][j] = -1e9;
		dp[0][0] = 0;
		for(int i = 0 ; i < n ; i++){
			for(int j = 0 ; j < (1<<16) ; j++){
				if( dp[i][j] >= 0 ){
					for(int k = 0 ; k < c ; k++){
						dp[i+1][(j|a[i])&~(b[k])] = 
							max(dp[i+1][(j|a[i])&~(b[k])],dp[i][j]+__builtin_popcount((j|a[i])&(b[k])) );

					}
				}
			}
		}
		int ans = 0;
		for(int j = 0 ; j < (1<<16) ; j++)
			ans = max( ans , dp[n][j] );
		cout << ans << endl;
	}
}