Problem 6—The Sultan's Successors

The Sultan of Nubia has no children, so she has decided that the country will be split into up to k separate parts on her death and each part will be inherited by whoever performs best at some test. It is possible for any individual to inherit more than one or indeed all of the portions. To ensure that only highly intelligent people eventually become her successors, the Sultan has devised an ingenious test. In a large hall filled with the splash of fountains and the delicate scent of incense have been placed k chessboards. Each chessboard has numbers in the range 0 to 10000 written on each square and is supplied with 8 jewelled chess queens. The task facing each potential successor is to place the 8 queens on the chess board in such a way that no queen threatens another one, and so that the numbers on the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For those unfamiliar with the rules of chess, this implies that each row and column of the board contains exactly one queen, and each diagonal contains no more than one.)

Write a program that will read in the number and details of the chessboards and determine the highest scores possible for each board under these conditions. (You know that the Sultan is both a good chess player and a good mathematician and you suspect that her score is the best attainable.)

INPUT SPECIFICATION.  The input file will begin with a decimal integer k inclusively between 1 and 10000 followed by <EOLN>, indicating the number of chessboards that will follow.  Each chessboard will consist of 8 rows of 8 decimal integers inclusively between 1 and 10000 corresponding to the values of the squares.  There will be exactly one space between integers on each line, and each line will be terminated by <EOLN>.  There may or may not be one or more additional <EOLN>'s before, after, or between chessboards, but no extra space or <EOLN> will appear in the input file beyond what is specified above.

OUTPUT SPECIFICATION.  The output cases should be processed in the order in which they appear in the input file.  For each input case, the output file should contain the decimal integer corresponding to the sum of the values on the squares on which the queens should be placed.  Each integer should be followed by exactly one <EOLN>.

SAMPLE INPUT.

2<EOLN>

1·2·3·4·5·6·7·8<EOLN>

9·10·11·12·13·14·15·16<EOLN>

17·18·19·20·21·22·23·24<EOLN>

25·26·27·28·20·30·31·32<EOLN>

33·34·35·36·37·38·39·40<EOLN>

41·42·43·44·45·46·47·48<EOLN>

49·50·51·52·53·54·55·56<EOLN>

57·58·59·60·61·62·63·64<EOLN>

<EOLN>

0·0·0·0·0·0·0·0<EOLN>

0·0·0·0·0·0·0·0<EOLN>

0·0·0·0·0·0·0·0<EOLN>

0·0·0·0·0·0·0·0<EOLN>

0·0·0·0·0·0·0·0<EOLN>

0·0·0·0·0·0·0·0<EOLN>

0·0·0·0·0·0·0·0<EOLN>

0·0·0·0·0·0·0·0<EOLN>

<EOF>

SAMPLE OUTPUT.

260<EOLN>

0<EOLN>

<EOF>