#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

typedef pair<long long int, int> P;

P count(long long int b, long long int j) {
	P p = P(0, 0);
	for (int i = 0; i < 16; i++) {
		if (((b & (1<<i)) != 0)&&((j & (1<<i)) != 0)) {
			p.second++;
		} else if ((j & (1<<i)) != 0) {
			p.first += (1<<i);
		}
	}
	return p;
}

int main() {
	int n, c;
	while (cin >> n >> c) {
		if (n == 0 && c == 0) {
			break;
		}
		vector<long long int> a(n);
		int cc;
		for (int i = 0; i < n; i++) {
			long long int aa = 0;
			for (int j = 0; j < 16; j++) {
				cin >> cc;
				if (cc == 1) {
					aa += (1<<j);
				}
			}
			a[i] = aa;
		}
		a.push_back((1<<16)-1);
		vector<long long int> b(c);
		for (int i = 0; i < c; i++) {
			long long int bb = 0;
			for (int j = 0; j < 16; j++) {
				cin >> cc;
				if (cc == 1) {
					bb += (1<<j);
				}
			}
			b[i] = bb;
		}
		vector< vector<int> > dp((1<<16), vector<int>(n+1, -1));
		for (int i = 0; i < n; i++) {
			if (i == 0) {
				dp[a[0]][0] = 0;
			}
			for (int j = 0; j < (1<<16); j++) {
				if (dp[j][i] != -1) {
					dp[(a[i+1]|j)][i+1] = max(dp[(a[i+1]|j)][i+1], dp[j][i]);
					for (int k = 0; k < c; k++) {
						P pp = count(b[k], j);
						if (pp.second != 0) {
							dp[(a[i+1]|pp.first)][i+1] = max(dp[(a[i+1]|pp.first)][i+1], dp[j][i]+pp.second);
							//cout << i << ' ' << dp[j][i]+pp.second << endl;
						}
					}
				}
			}
		}
		int MAX = 0;
		for (int i = 0; i < (1<<16); i++) {
			MAX = max(MAX, dp[i][n]);
		}
		cout << MAX << endl;
	}
	return 0;
}