#include<bits/stdc++.h>

using namespace std;

int N, C;
int LightInfo[30];
int ButtonInfo[30];
int DpScore[31][(1 << 16)];

int popcnt(int u) {
	int ret;

	ret = 0;
	while (u) {
		u = (u & (u - 1));
		++ret;
	}

	return ret;
}

void solve() {
	int bits, total;

	fill_n(*DpScore, 31 * ((1 << 16)), -1);
	DpScore[0][0] = 0;
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < ((1 << 16)); ++j) {
			if (DpScore[i][j] == -1) {
				continue;
			}
			for (int k = 0; k < C; ++k) {
				bits = (LightInfo[i] | j) & (~ButtonInfo[k]);
				DpScore[i + 1][bits] = max(DpScore[i + 1][bits], DpScore[i][j] + popcnt((LightInfo[i] | j)&ButtonInfo[k]));
			}
		}
	}

	total = *max_element(DpScore[0], DpScore[0] + 31 * (1 << 16));
	cout << total << endl;
}

int main() {
	int num;

	while (cin >> N >> C, N) {
		memset(LightInfo, 0, sizeof(LightInfo));
		memset(ButtonInfo, 0, sizeof(ButtonInfo));
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < 16; ++j) {
				cin >> num;
				if (num == 1) {
					LightInfo[i] |= (1 << j);
				}
			}
		}
		for (int i = 0; i < C; ++i) {
			for (int j = 0; j < 16; ++j) {
				cin >> num;
				if (num == 1) {
					ButtonInfo[i] |= (1 << j);
				}
			}
		}

		solve();
	}

	return 0;
}

