#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, T;
int ch[30], pat[30];
int dp[2][1<<16];
int input() {
  int s = 0;
  rep(i, 16) {
    int a;
    cin >> a;
    if (a) s |= 1<<i;
  }
  return s;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> N >> T) {
    if (N == 0 && T == 0) break;
    rep(i, N) ch[i] = input();
    rep(i, T) pat[i] = input();
    rep(i, 1<<16) dp[0][i] = dp[1][i] = -INF;
    dp[0][0] = 0;
    rep(i, N) {
      rep(s, 1<<16) dp[1][s] = -INF;
      rep(s, 1<<16) dp[1][s|ch[i]] = max(dp[1][s|ch[i]], dp[0][s]);
      swap(dp[0], dp[1]);
      rep(s, 1<<16) dp[1][s] = -INF;
      rep(s, 1<<16) {
        rep(j, T) {
          int ns = s & (~pat[j]);
          dp[1][ns] = max(dp[1][ns], dp[0][s] + __builtin_popcount(s & pat[j]));
        }
      }
      swap(dp[0], dp[1]);
    }
    int m = 0;
    rep(s, 1<<16) m = max(m, dp[0][s]);
    cout << m << "\n";
  }
  return 0;
}