output_description: For each dataset, output the largest possible number of unit panels in the
  united panel at the upper left corner with the target color after five times
  of color changes of the panel at the upper left corner. No extra characters
  should occur in the output.
input_description: The input consists of multiple datasets, each being in the following format.
  
  > _h_ _w_ _c_  
  >  _p_ 1,1 _p_ 1,2 ... _p_ 1,_w_  
  >  _p_ 2,1 _p_ 2,2 ... _p_ 2,_w_  
  >  ...  
  >  _p_ _h_ ,1 _p_ _h_ ,2 ... _p_ _h_ ,_w_  
  >
  
  _h_ and _w_ are positive integers no more than 8 that represent the height and
  the width of the given rectangle. _c_ is a positive integer no more than 6
  that represents the target color of the finally united panel. _p_ _i_ ,_j_ is
  a positive integer no more than 6 that represents the initial color of the
  panel at the position (_i_ , _j_).
  
  The end of the input is indicated by a line that consists of three zeros
  separated by single spaces.
attempt: import copy
  import itertools
  
  
  drc = [(-1, 0), (0, 1), (1, 0), (0, -1)]
  C_NUM = 6
  while True:
      H, W, C = map(int, input().split())
      if not (H | W | C):
          break
      board = [[int(x) - 1 for x in input().split()] for _ in range(H)]
      ans = 0
      for ope in itertools.product(range(C_NUM), repeat=4):
          # print(ope)
          tmp_board = copy.deepcopy(board)
          for color in itertools.chain(ope, (C - 1,)):
              now_c = tmp_board[0][0]
              if now_c == color:
                  continue
              stack = [(0, 0)]
              while stack:
                  r, c = stack.pop()
                  if tmp_board[r][c] == color:
                      continue
                  tmp_board[r][c] = color
                  for dr, dc in drc:
                      nr, nc = r + dr, c + dc
                      if 0 <= nr < H and 0 <= nc < W and tmp_board[nr][nc] == now_c:
                          stack.append((nr, nc))
          stack = [(0, 0)]
          while stack:
              r, c = stack.pop()
              if tmp_board[r][c] == -1:
                  continue
              tmp_board[r][c] = -1
              for dr, dc in drc:
                  nr, nc = r + dr, c + dc
                  if 0 <= nr < H and 0 <= nc < W and tmp_board[nr][nc] == C - 1:
                      stack.append((nr, nc))
          ans = max(ans, sum(row.count(-1) for row in tmp_board))
      print(ans)
  
problem_description: _Identically Colored Panels Connection_
  
  Dr. Fukuoka has invented fancy panels. Each panel has a square shape of a unit
  size and has one of the six colors, namely, yellow, pink, red, purple, green
  and blue. The panel has two remarkable properties. One property is that, when
  two or more panels with the same color are placed adjacently, their touching
  edges melt a little and they are fused each other. The fused panels are united
  into a polygonally shaped panel. The other property is that the color of a
  panel can be changed to one of six colors by giving an electrical shock. The
  resulting color can be controlled by its waveform. The electrical shock to an
  already united panel changes the color of the whole to a specified single
  color.
  
  Since he wants to investigate the strength with respect to the color and the
  size of a united panel compared to unit panels, he tries to unite panels into
  a polygonal panel with a specified color.
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1174_1)  
  Figure C-1: panels and their initial colors  
    
  
  Since many panels are simultaneously synthesized and generated on a base plate
  through some complex chemical processes, the fabricated panels are randomly
  colored and they are arranged in a rectangular shape on the base plate (Figure
  C-1). Note that the two purple (color 4) panels in Figure C-1 are already
  united at the initial state since they are adjacent to each other.
  
  Installing electrodes to a panel, and changing its color several times by
  giving electrical shocks according to an appropriate sequence for a specified
  target color, he can make a united panel merge the adjacent panels to unite
  them step by step and can obtain a larger panel with the target color.
  Unfortunately, panels will be broken when they are struck by the sixth
  electrical shock. That is, he can change the color of a panel or a united
  panel only five times.
  
  Let us consider a case where the panel at the upper left corner of the panel
  configuration (Figure C-1) is attached with the electrodes. First, changing
  the color of the panel from yellow to blue, the two adjacent panels are fused
  into a united panel (Figure C-2).
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1174_2)  
  Figure C-2: Change of the color of the panel at the upper left corner, from
  yellow (color 1) to blue (color 6).  
    
  
  Second, changing the color of the upper left united panel from blue to red, a
  united red panel that consists of three unit panels is newly formed (Figure
  C-3). Then, changing the color of the united panel from red to purple, panels
  are united again to form a united panel of five unit panels (Figure C-4).
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1174_3)  
  Figure C-3: Change of the color of the panel at the upper left corner, from
  blue (color 6) to red (color 3).  
    
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1174_4)  
  Figure C-4: Change of the color of the panel at the upper left corner, from
  red (color 3) to purple (color 4).  
    
  
  Furthermore, through making a pink united panel in Figure C-5 by changing the
  color from purple to pink, then, the green united panel in Figure C-6 is
  obtained by changing the color from pink to green. The green united panel
  consists of ten unit panels.
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1174_5)  
  Figure C-5: Change of the color of the panel at the upper left corner, from
  purple (color 4) to pink (color 2).  
    
  
  ![](https://judgeapi.u-aizu.ac.jp/resources/images/IMAGE2_1174_6)  
  Figure C-6: Change of the color of the panel at the upper left corner, from
  pink (color 2) to green (color 5).  
    
  
  In order to check the strength of united panels with various sizes and colors,
  he needs to unite as many panels as possible with the target color. Your job
  is to write a program that finds a sequence to change the colors five times in
  order to get the largest united panel with the target color. Note that the
  electrodes are fixed to the panel at the upper left corner.
samples: [{"input": "5 5\n    1 6 3 2 5\n    2 5 4 6 1\n    1 2 4 1 5\n    4 5 6\n    1 5 6 1 2\n    1 4 6 3 2\n    1 5 2 3 2\n    1 1 2 3 2\n    1 1 5\n    1\n    1 8 6\n    1 2 3 4 5 1 2 3\n    8 1 1\n    1\n    2\n    3\n    4\n    5\n    1\n    2\n    3\n    8 8 6\n    5 2 5 2 6 5 4 2\n    4 2 2 2 5 2 2 2\n    4 4 4 2 5 2 2 2\n    6 4 5 2 2 2 6 6\n    6 6 5 5 2 2 6 6\n    6 2 5 4 2 2 6 6\n    2 4 4 4 6 2 2 6\n    2 2 2 5 5 2 2 2\n    8 8 2\n    3 3 5 4 1 6 2 3\n    2 3 6 4 3 6 2 2\n    4 1 6 6 6 4 4 4\n    2 5 3 6 3 6 3 5\n    3 1 3 4 1 5 6 3\n    1 6 6 3 5 1 5 3\n    2 4 2 2 2 6 5 3\n    4 1 3 6 1 5 5 4\n    0 0 0", "output": "18\n    1\n    5\n    6\n    64\n    33"}]