#include <stdbool.h>
#include <stdio.h>

int h, w, c;
int memo[10][10] = {0};
bool added[10][10] = {0};
int stack[105][2] = {0};
int d[] = {1, 0, -1, 0};
int scnt;

void dfs(int col);
int solve(int now, int bf);
int max(int x, int y) { return x > y ? x : y; }

int main() {
  int i, j;
  while(1) {
    scanf("%d %d %d", &h, &w, &c);
    if(h + w + c == 0) break;
    scnt = 1;
    for(i = 0; i < h; ++i)
      for(j = 0; j < w; ++j) added[i][j] = 0;
    for(i = 0; i < h; ++i)
      for(j = 0; j < w; ++j) scanf("%d", &memo[i][j]);
    stack[0][0] = stack[0][1] = 0;
    added[0][0] = 1;
    dfs(memo[0][0]);
    printf("%d\n", solve(0, memo[0][0]));
  }
  return 0;
}

void dfs(int col) {
  int i, j;
  for(i = 0; i < scnt; ++i) {
    int x = stack[i][0], y = stack[i][1];
    for(j = 0; j < 4; ++j) {
      int nx = x + d[j], ny = y + d[1 ^ j];
      if(nx >= 0 && nx < h && ny >= 0 && ny < w &&
         (!added[nx][ny]) && memo[nx][ny] == col) {
        added[nx][ny] = 1;
        stack[scnt][0] = nx;
        stack[scnt][1] = ny;
        ++scnt;
      }
    }
  }
}

int solve(int now, int bf) {
  if(now == 5) return scnt;
  int i, j, ans = scnt, nscnt = scnt;
  for(i = 1; i <= 6; ++i) {
    scnt = nscnt;
    if(bf == i || (now == 4 && i != c)) continue;
    dfs(i);
    ans = max(solve(now + 1, i), ans);
    for(j = scnt - 1; j >= nscnt; --j) {
      int x = stack[j][0], y = stack[j][1];
      added[x][y] = 0;
      --scnt;
    }
  }
  return ans;
}
