#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
constexpr int SIZE = 16;

int input() {
	int res = 0;
	for(int i = 0; i < SIZE; ++i) {
		int in;
		cin >> in;
		res = (res << 1) | in;
	}
	return res;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int n, c; cin >> n >> c && n;) {
		vector<int> a(n), b(c);
		for(auto &e : a) e = input();
		for(auto &e : b) e = input();

		b.emplace_back(0);

		unordered_map<int, int> calc(1 << SIZE);
		calc[0] = 0;

		for(const auto &on : a) {
			unordered_map<int, int> next_calc(1 << SIZE);

			for(const auto &current : calc) {
				const int before = (current.first | on);
				const int before_count = __builtin_popcount(before);

				for(const auto &e : b) {
					const int after = (before & ~e);
					const int score = current.second + before_count - __builtin_popcount(after);
					chmax(next_calc[after], score);
				}
			}

			calc = move(next_calc);
		}

		int ans = 0;
		for(const auto &e : calc){
			chmax(ans, e.second);
		}

		cout << ans << endl;
	}

	return EXIT_SUCCESS;
}