Time Limit : sec, Memory Limit : KB
English / Japanese  

Beat Panel

Yuta is addicted to a popular game called "Beat Panel" at a local game center. This game consists of 16 panel-shaped buttons (4×4) arranged in a grid like the following figure.


As shown in the figure, the buttons are lined up in order from top left to bottom right: button 1, button 2, ..., button 16. The game plays a beat sound at regular intervals and ends with an exit sound. When the beat sounds, several buttons light up at the same time, but sometimes none of them light up. The player can use the fingers of both hands to press multiple buttons at the same time only once, between the time the beat sounds and the time the next beat sounds. It is also possible to not press anything. The game will end as soon as the end sound is heard.

Yuta has learned c ways to press the buttons, and each time the beat sounds, he decides one of those ways to press the button. If the button he presses glows, the light on the button goes out, and the number of buttons that go out is added to the player's score. Once a button glows, the light will not go out until that button is pressed.

Create a program that takes as input the way the buttons light up when the beat sounds n times and the c different ways of pressing the buttons that Yuta has mastered, and outputs the maximum score that Yuta can get.

Input

The input consists of multiple data sets. The end of the input is indicated by two zero lines. Each data set 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 a single space. n (1 ≤ n ≤ 30) is the number of beat tones, and c (1 ≤ c ≤ 30) is the number of button presses you have learned. The subsequent n+c lines give the button glow and button presses. The input items in the lines are separated by a single space. ak,i indicates how button i glows when the kth beat is played, and bj,i indicates whether button i is pressed with the jth press of c. The value of ak,i is 0 for "no glow" and 1 for "glow". The value of bj,i is 0 for " Do not press" and 1 for "Press".

The number of data sets should not exceed 20.

Output

For each data set, output the maximum value of scores on one line.

Sample 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

Sample Output

8
7
16