#include <stdio.h>
#include <string.h>

#define rep(i,n) for(int i=0;i<(int)n;++i)
int max(const int a, const int b) {
  return a > b ? a : b ;
}

int H, W, C;
int base[10][10];
int copy[10][10], visit[10][10];
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, -1, 1};

void rec(int base_color, int color, int i, int j) {
  // printf(">>%2d %2d %2d %2d\n", base_color, color, i, j);
  if (visit[i][j]) return;
  visit[i][j] = 1;
  rep(z,4) {
    int ni = i+di[z], nj = j+dj[z];
    if (ni < 0 || nj < 0 || H <= ni || W <= nj) continue;
    if (visit[ni][nj] || (base_color != copy[ni][nj])) continue;
    if (color == copy[ni][nj]) continue;
    copy[ni][nj] = color;
    rec(base_color, color, ni, nj);
  }
}

void update(int color) {
  // memset(visit, 0, sizeof (visit));
  rep(i,H) rep(j,W) visit[i][j] = 0;
  int prev_color = copy[0][0];
  copy[0][0] = color;
  rec(prev_color, color, 0, 0);
}

void debug() {
  rep(h,H) {
    rep(w,W) printf("%2d", copy[h][w]);
    puts("");
  }
}

int t_ans = 0;


void calc_rec(int i, int j) {
  if (visit[i][j]) return;
  t_ans++;
  visit[i][j] = 1;
  rep(z,4) {
    int ni = i+di[z], nj = j+dj[z];
    if (ni < 0 || nj < 0 || H <= ni || W <= nj) continue;
    if (visit[ni][nj] || copy[ni][nj] != copy[i][j]) continue;
    calc_rec(ni, nj);
  }
}

int calc() {
  memset(visit, 0, sizeof (visit));
  t_ans = 0;
  calc_rec(0, 0);
  return t_ans;
}

int solve() {
  int ans = 0;
  rep(i,6) rep(j,6) rep(k,6) rep(l,6) {
    if (i==j || j==k || k==l || l+1==C)
      continue;
    rep(h,H) rep(w,W) copy[h][w] = base[h][w];
    int ch[5] = {i+1, j+1, k+1, l+1, C};
    rep(z,5) {
      update(ch[z]);
      /* if (ch[0] == 6 && ch[1] == 3 && ch[2] == 4 && */
      /*     ch[3] == 2 && ch[4] == 5) { */
      /*   puts(""); */
      /*   debug(); */
      /* } */
    }
    ans = max(ans,calc());
  }
  return ans;
}

int main() {
  while(scanf("%d%d%d", &H, &W, &C), H|W) {
    rep(i,H) rep(j,W) scanf("%d", base[i]+j);
    printf("%d\n",solve());
  }
}