#include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
int a[30], b[30];
int dp[31][1 << 16];
int bit_count(int);
int main()
{
	int n, c;
	while (true) {
		cin >> n >> c;
		if (n == 0 && c == 0)break;
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(dp, -1, sizeof(dp));
		dp[0][0] = 0;
		for (int i = 0; i < n; i++) {
			for (int i2 = 0; i2 < 16; i2++) {
				int bit;
				cin >> bit;
				a[i] |= (bit << i2);
			}
		}
		for (int i = 0; i < c; i++) {
			for (int i2 = 0; i2 < 16; i2++) {
				int bit;
				cin >> bit;
				b[i] |= (bit << i2);
			}
		}
		for (int i = 0; i < n; i++) {
			for (int i2 = 0; i2 < (1 << 16); i2++) {
				if (dp[i][i2] == -1)continue;
				for (int i3 = 0; i3 < c; i3++) {
					int bit = a[i] | i2;
					int add = bit & b[i3];
					int bitcount = bit_count(add);
					dp[i + 1][bit - add] = max(dp[i + 1][bit - add], dp[i][i2] + bitcount);
				}
			}
		}
		int maxv = 0;
		for (int i = 0; i < (1 << 16); i++) {
			maxv = max(maxv, dp[n][i]);
		}
		cout << maxv << endl;
	}

}
int bit_count(int bit) { 
	if (bit)
		return (bit & 1) + bit_count(bit >> 1);
	else
		return 0;
}