On a triangular board of numbered stones, place one stone so the turn's score (opponents' stones removed minus your own removed) is maximized.
Medium6SimulationGraphImplementationBrute forceNo attempts yetTime limit2sMemory limit512 MBLet's play a new board game, "Life Line".
The number of players is at least 2 and at most 9.
The board is a large regular triangle made of many small regular triangles (Figure 1). Every small triangle has edges of the same length.

Figure 1: The board
The size of the board is the number of vertices on the bottom edge of the outer triangle. For example, the board in Figure 1 has size 4.
At the start of the game, each player receives a distinct identification number between 1 and 9 and some stones with that number written on them.
The players take turns. On a turn, a player puts one of his stones on an empty vertex. An empty vertex is a vertex with no stone on it.
When a player puts a stone on a vertex, some stones may be removed from the board. The player gains one point for each removed stone of another player and loses one point for each removed stone of his own. The points of a player for a turn are the points gained minus the points lost in that turn.
Stones are removed under the following conditions:

Figure 2: Groups of stones
Figure 2 shows an example of groups of stones.
Suppose it is the turn of player '4'. If he puts his stone on the vertex shown in Figure 3a, some groups satisfy the removal condition (shaded in Figure 3b). The player gains 6 points, because 6 stones of other players are removed (Figure 3c).
![]() | ![]() | ![]() |
| Figure 3a | Figure 3b | Figure 3c |
As another example, suppose it is the turn of player '2' in Figure 2. If he puts his stone on the vertex shown in Figure 4a, some groups satisfy the removal condition (shaded in Figure 4b). The player gains 4 points because 4 stones of other players are removed, but at the same time loses 3 points because 3 of his own stones are removed. The points of this turn are therefore 4 - 3 = 1 (Figure 4c).
![]() | ![]() | ![]() |
| Figure 4a | Figure 4b | Figure 4c |
The game ends when every player has put all of his stones on the board. The total score of a player is the sum of the points of all his turns.
Write a program that computes the maximum points (points gained minus points lost) the current player can get in his current turn.
The input consists of multiple data sets. Each data set describes the state of a board of a game in progress.
Each data set has the following format.
N C
S1,1
S2,1 S2,2
S3,1 S3,2 S3,3
...
SN,1 ... SN,N
N is the size of the board (3≤N≤10).
C is the identification number of the player whose turn it is (1≤C≤9). Your program must compute his points for this turn.
Si,j is the state of a vertex of the board (0≤Si,j≤9). A positive Si,j means that a stone numbered Si,j is on that vertex. A value of 0 means that the vertex is empty. Every board in the input has at least one empty vertex.
A line with two zeros, 0 0, marks the end of the input.
For each data set, print the maximum points the player can get in this turn on its own line.