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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

int**dp;

void func(int N,int C){
	int beat[N][16],push[C][16];

	int maximum = 0;

	for(int i = 0; i < N; i++){
		for(int k = 0; k < 16; k++)scanf("%d",&beat[i][k]);
	}

	for(int i = 0; i < C; i++){
		for(int k = 0; k < 16; k++)scanf("%d",&push[i][k]);
	}

	int tmp,S,code;

	int work[C][16];

	S = 1;
	code = 0;
	for(int i = 0; i < 16; i++){
		code += S*beat[0][i];
		S *= 2;
	}
	dp[1][code] = 0;


	for(int k = 0; k < C; k++){
		tmp = 0;
		for(int p = 0; p < 16; p++){
			work[k][p] = beat[0][p];
			if(work[k][p] == 1){
				if(push[k][p] == 1){
					work[k][p] = 0;
					tmp++;
				}
			}
		}

		S = 1;
		code = 0;
		for(int i = 0; i < 16; i++){
			code += S*work[k][i];
			S *= 2;
		}
		dp[1][code] = max(dp[1][code],tmp);
		maximum = max(maximum,dp[1][code]);
	}

	for(int i = 1; i < N; i++){
		for(int state = 0; state < 65536; state++){
			if(dp[i][state] != -1){

				S = 1;
				code = 0;
				for(int loop = 0; loop < 16; loop++){
					if(state & (1 << loop)){
						code += S;
					}else{
						code += S*beat[i][loop];
					}
					S *= 2;
				}
				dp[i+1][code] = dp[i][state];

				for(int k = 0; k < C; k++){
					tmp = 0;
					for(int p = 0; p < 16; p++){
						if(state & (1 << p)){
							if(push[k][p] == 1){
								work[k][p] = 0;
								tmp++;
							}else{
								work[k][p] = 1;
							}

						}else{
							work[k][p] = beat[i][p];
							if(work[k][p] == 1){
								if(push[k][p] == 1){
									work[k][p] = 0;
									tmp++;
								}
							}
						}
					}

					S = 1;
					code = 0;
					for(int a = 0; a < 16; a++){
						code += S*work[k][a];
						S *= 2;
					}
					dp[i+1][code] = max(dp[i+1][code],tmp+dp[i][state]);
				}
			}
		}
	}

	for(int i = 0; i < 65536; i++)maximum = max(maximum,dp[N][i]);

	printf("%d\n",maximum);
}

int main(){

	int N,C;
	dp = new int*[31];
	for(int i = 0; i < 31; i++){
		dp[i] = new int[65536];
	}

	while(true){
		scanf("%d %d",&N,&C);
		if(N == 0 && C == 0)break;

		for(int i = 0; i <= N; i++){
			for(int k = 0; k < 65536; k++)dp[i][k] = -1;
		}

		func(N,C);
	}

    return 0;
}