Problem: Yuta is addicted to the popular game "Beat Panel" at a nearby arcade. The game consists of a total of 16 panel-type buttons, 4x4, arranged in a grid as shown.

<image>


As shown in the figure, the buttons are arranged in the order of button 1, button 2,…, button 16 from the upper left to the lower right. In the game, you will hear a beat sound at regular intervals and a final sound at the end. Multiple buttons light up at the same time as the beat sounds. In some cases, even one does not shine. The player can press multiple buttons at the same time using the fingers of both hands from immediately after the beat sound to the next sound. It is also possible not to press anything. The game ends as soon as the end sound is heard.

Yuta has mastered c ways of pressing, and each time a beat sounds, he decides one of those pressing methods and presses the button. If the buttons you press are lit, those buttons will be lit and the number of buttons that have disappeared will be added to the player's score. Also, once a button is lit, the light will not go out until the button is pressed.

A program that outputs the maximum score that Yuta can obtain by inputting how to illuminate the button when the beat sound is played n times and how to press the button in c ways that Yuta has learned. Please create.



input

The input consists of multiple datasets. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format.


n c
a1,1 a1,2 ... a1,16
a2,1 a2,2 ... a2,16
...
an, 1 an, 2 ... an, 16
b1,1 b1,2 ... b1,16
b2,1 b2,2 ... b2,16
...
bc,1 bc,2 ... bc,16


The first line consists of two integers separated by one space. n (1 ≤ n ≤ 30) indicates the number of beat sounds, and c (1 ≤ c ≤ 30) indicates the number of button presses that you have learned. The following n + c lines give how to illuminate the button and how to press the button. The input item in the line is separated by one blank. ak and i indicate how the button i shines when the kth beat sound is heard, and bj and i indicate whether the button i is pressed by pressing the jth button in the middle of c. As for the values ​​of ak and i, 0 means "does not shine" and 1 means "shines". For the values ​​of bj and i, 0 means "do not press" and 1 means "press".

The number of datasets does not exceed 20.

output

For each data set, the maximum score is output on one line.

Example

Input

2 2
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
2 2
0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 0
0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0
0 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0
5 3
0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0
1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1
0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0
1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
0 0


Output

8
7
16

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
/* I can't know bit!!! >< */
int n, c, a[31][16], b[31][16];
int dp[31][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2][2];
 
int main()
{
  while(cin >> n >> c, n) {
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < 16; j++) {
        cin >> a[i][j];
      }
    }
    for(int i = 0; i < c; i++) {
      for(int j = 0; j < 16; j++) {
        cin >> b[i][j];
      }
    }
#define R(i, n) for(int i = 0; i < n; i++)
#define get(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) [a][b][c][d][e][f][g][h][i][j][k][l][m][n][o][p]
     
    memset(dp, -1, sizeof(dp));
    dp[0]get(a[0][0],a[0][1],a[0][2],a[0][3],a[0][4],a[0][5],a[0][6],a[0][7],a[0][8],a[0][9],a[0][10],a[0][11],a[0][12],a[0][13],a[0][14],a[0][15]) = 0;
    for(int i = 0; i < n; i++) {
      R(A,2) R(B,2) R(C,2) R(D,2) R(E,2) R(F,2) R(G,2) R(H,2) R(I,2) R(J,2) R(K,2) R(L,2) R(M,2) R(N,2) R(O,2) R(P,2) {
        if(dp[i]get(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P) == -1) continue;
        for(int j = 0; j < c; j++) {
          dp[i+1]get((A&~b[j][0])|a[i+1][0],(B&~b[j][1])|a[i+1][1],(C&~b[j][2])|a[i+1][2],(D&~b[j][3])|a[i+1][3],(E&~b[j][4])|a[i+1][4],(F&~b[j][5])|a[i+1][5],(G&~b[j][6])|a[i+1][6],(H&~b[j][7])|a[i+1][7],(I&~b[j][8])|a[i+1][8],(J&~b[j][9])|a[i+1][9],(K&~b[j][10])|a[i+1][10],(L&~b[j][11])|a[i+1][11],(M&~b[j][12])|a[i+1][12],(N&~b[j][13])|a[i+1][13],(O&~b[j][14])|a[i+1][14],(P&~b[j][15])|a[i+1][15]) = max(dp[i+1]get((A&~b[j][0])|a[i+1][0],(B&~b[j][1])|a[i+1][1],(C&~b[j][2])|a[i+1][2],(D&~b[j][3])|a[i+1][3],(E&~b[j][4])|a[i+1][4],(F&~b[j][5])|a[i+1][5],(G&~b[j][6])|a[i+1][6],(H&~b[j][7])|a[i+1][7],(I&~b[j][8])|a[i+1][8],(J&~b[j][9])|a[i+1][9],(K&~b[j][10])|a[i+1][10],(L&~b[j][11])|a[i+1][11],(M&~b[j][12])|a[i+1][12],(N&~b[j][13])|a[i+1][13],(O&~b[j][14])|a[i+1][14],(P&~b[j][15])|a[i+1][15]),dp[i]get(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P)+(A&b[j][0])+(B&b[j][1])+(C&b[j][2])+(D&b[j][3])+(E&b[j][4])+(F&b[j][5])+(G&b[j][6])+(H&b[j][7])+(I&b[j][8])+(J&b[j][9])+(K&b[j][10])+(L&b[j][11])+(M&b[j][12])+(N&b[j][13])+(O&b[j][14])+(P&b[j][15]));
        }
      }
    }
    int ret = 0;
    R(A,2) R(B,2) R(C,2) R(D,2) R(E,2) R(F,2) R(G,2) R(H,2) R(I,2) R(J,2) R(K,2) R(L,2) R(M,2) R(N,2) R(O,2) R(P,2) {
      ret = max(ret, dp[n]get(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P));
    }
    cout << ret << endl;
  }
}