/*
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2331961#2
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2331969#2
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2331970#2
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2332101#2
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <tuple>
#include <algorithm>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i,n)	for(int i=0;i<n;i++)
#define srep(i,a,n)	for(int i=a;i<n;i++)
#define REP(i,n)	for(int i=0;i<=n;i++)
#define SREP(i,a,n)	for(int i=a;i<=n;i++)
#define rrep(i,n)	for(int i=n-1;i>=0;i--)
#define all(a)	(a).begin(),(a).end()
#define mp(a,b)	make_pair(a,b)
#define mt	make_tuple
#define fst	first
#define scn second

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;

int bit_cnt(int a) {
	int ret = 0;
	while (a) {
		a &= a - 1;
		ret++;
	}
	return ret;
}

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

int main() {
	while (true) {
		int n, c;	cin >> n >> c;
		if(n==0)	break;
		vint a(n, 0);
		rep(i, n) {
			rep(j, 16) {
				a[i] <<= 1;
				int in;	cin >> in;
				a[i] += in;
			}
		}
		vint b(c, 0);
		rep(i, c) {
			rep(j, 16) {
				b[i] <<= 1;
				int in;	cin >> in;
				b[i] += in;
			}
		}
		REP(i, n)	rep(j, 1 << 16)	dp[i][j] = 0;
		priority_queue<tuple<int, int, int> > pq;
		pq.push(mt(0, 0, 0));
		int ret = 0;
		while (!pq.empty()) {
			int point = 0, i = 0, state = 0;
			tie(i, point, state) = pq.top();	pq.pop();
			if (point < dp[i][state])	continue;
			if (i == n) {
				ret = max(ret, point);
				continue;
			}
			state |= a[i];
			rep(j, c) {
				int ns = state&(state^b[j]);
				int add = bit_cnt(state&b[j]);
				if (add + point > dp[i + 1][ns]) {
					dp[i + 1][ns] = add + point;
					pq.push(mt(i + 1, add + point, ns));
				}
			}
		}
		cout << ret << endl;
	}
	return 0;
}