Given two natural numbers and , you must construct a number using the digits (exactly one of each) such that no (non-empty) prefix of is a multiple of . For example, with and , is not a valid order, because is a multiple of . In contrast, is a valid order, because neither , nor , nor , nor are multiples of .
Moreover, you have a matrix such that contains the prize that is obtained if digit is placed immediately to the right of digit . Maximize the total sum of the prizes.
The input consists of several cases. Each case starts with and , followed by : rows, each with natural numbers between 1 and , except for the diagonal, that is filled with zeros. You can assume that and .
For each case, print the maximum possible prize. If no can be constructed, print 0.
Input
10 2 0 4 3 0 6 2 0 1000000 1 0 3 3 0 7 7 7 0 7 7 7 0 3 4 0 1 2 3 1000 0 4 2000 5 6 0 7 8 9 1 0
Output
4 1 0 18