Identically Colored Panels Connection

No attempts yetTime limit1sMemory limit128 MB

Problem

Dr. Fukuoka has invented fancy panels. Each panel is a unit square and has one of six colors: yellow, pink, red, purple, green, and blue. A panel has two remarkable properties.

First, when two or more panels of the same color are placed edge to edge, their touching edges melt a little and they fuse together. The fused panels unite into a single polygonal panel.

Second, an electrical shock can change a panel's color to one of the six colors, and the resulting color is controlled by the waveform. A shock applied to an already united panel changes the whole of it to a single specified color.

To investigate how strong a united panel is in color and size compared with unit panels, he tries to unite panels into a polygonal panel of a specified color.

Figure C-1: panels and their initial colors

Because many panels are synthesized simultaneously through complex chemical processes and arranged in a rectangle on a base plate (Figure C-1), the fabricated panels are randomly colored. Note that the two purple (color 4) panels in Figure C-1 are already united in the initial state, since they are adjacent.

By installing electrodes on one panel and changing its color several times with electrical shocks in an appropriate sequence for a target color, he can make the united panel absorb its adjacent panels step by step and obtain a larger panel of the target color. Unfortunately, a panel breaks when it is struck by the sixth shock. That is, he can change the color of a panel (or a united panel) at most five times.

Suppose the panel at the upper-left corner of Figure C-1 has the electrodes. First, changing its color from yellow to blue fuses the two adjacent panels into a united panel (Figure C-2).

Figure C-2: Changing the upper-left panel from yellow (color 1) to blue (color 6).

Second, changing the upper-left united panel from blue to red forms a new red united panel of three unit panels (Figure C-3). Then, changing it from red to purple unites more panels into a panel of five unit panels (Figure C-4).

Figure C-3: Changing the upper-left panel from blue (color 6) to red (color 3).

Figure C-4: Changing the upper-left panel from red (color 3) to purple (color 4).

Furthermore, changing purple to pink yields the pink united panel in Figure C-5, and then changing pink to green yields the green united panel in Figure C-6, which consists of ten unit panels.

Figure C-5: Changing the upper-left panel from purple (color 4) to pink (color 2).

Figure C-6: Changing the upper-left panel from pink (color 2) to green (color 5).

To check the strength of united panels of various sizes and colors, he wants to unite as many panels as possible with the target color. Write a program that finds a sequence of five color changes that yields the largest united panel of the target color. The electrodes are fixed to the panel at the upper-left corner.

Input

The input consists of multiple datasets, each in the following format.

h w c
p1,1 p1,2 ... p1,w
p2,1 p2,2 ... p2,w
...
ph,1 ph,2 ... ph,w

$h$ and $w$ are positive integers no greater than 8 that give the height and the width of the rectangle. $c$ is a positive integer no greater than 6 that gives the target color of the final united panel. $p_{i,j}$ is a positive integer no greater than 6 that gives the initial color of the panel at position $(i, j)$.

The end of the input is a line of three zeros separated by single spaces.

Output

For each dataset, output the largest possible number of unit panels in the upper-left united panel whose color is the target color after five color changes of the upper-left panel. No other characters may appear in the output.