/*
2017_02_27_AC
??????????????????
????????°??¢??°??§?????????????????°???????????¨????????????????§???????????????????
????????????????????¨?????§??????????????£??????????????¶???????????£????????????????????????
??????????????????dfs()?????§paint()????????¶?????????paint()???????????????????????¨???
????????????????????£??????????????????????????¨??????????????§????????????????????????????????????????????????
  ??????????????????????§??????????????????????????????????check????????°?????????????????????????????????????????????????????????
?????¢??°???????????§????????°???(x, y)????????????????????????????????¶??¨??????(i(=y),j(=x))??§??????????????????
???-> ???????????¨?????§???((y(=i),x(=j)))????????§??????????????????????¬??????????????????????????????????
????????????
??????????????¨????????????????????£??¨???????????????????????????????????£????????????
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

#define CH(N,A,B) (A<=N&&N<B)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=(b-1);a<=i;i--)

using namespace std;

struct S{
  int A[9][9];
};

int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

int h, w, c, p[9][9], colcnt, ans;
bool visit[9][9];

void paint(int bfcolor, int x, int y,int color, int tp[9][9]){
  visit[y][x] = true;
  bfcolor = tp[y][x];
  tp[y][x] = color;
  REP(k,0,4){
    int posx = x + dx[k];
    int posy = y + dy[k];
    if(!CH(posx,0,w) || !CH(posy,0,h)) continue;
    if(tp[posy][posx] != bfcolor || visit[posy][posx]) continue;
    paint(bfcolor, posx, posy, color, tp);
  }
  return ;
}

void check(int x, int y, int color, struct S s){
  visit[y][x] = true;
  colcnt++;
  REP(k,0,4){
    int posx = x + dx[k];
    int posy = y + dy[k];
    if(!CH(posx,0,w) || !CH(posy,0,h)) continue;
    if(s.A[posy][posx] != color || visit[posy][posx]) continue;
    check(posx, posy, color, s);
  }
  return ;
}

void dfs(int k, int turn, struct S s){

  //??????
  int tp[9][9];
  REP(i,0,h)REP(j,0,w) tp[i][j] = s.A[i][j];  //??????????????§??????????????¨???????????????
  REP(i,0,h)REP(j,0,w) visit[i][j] = false;
  paint(s.A[0][0],0,0,k,tp);
  REP(i,0,h)REP(j,0,w) s.A[i][j] = tp[i][j]; //??????????????????????????????

  //??????????????????????????§???????????????
  /*
  REP(i,0,h)REP(j,0,w){
    colcnt = 0;
    if(s.A[i][j]==c){
      REP(ii,0,h)REP(jj,0,w) visit[ii][jj] = false;
      check(j,i,c,s);
    }
    ans = max(ans, colcnt);
  }
  if(turn == 5){
    return;
  }
  */
  if(turn == 5){
    REP(i,0,h)REP(j,0,w){
      colcnt = 0;
      if(s.A[i][j]==c){
        REP(ii,0,h)REP(jj,0,w) visit[ii][jj] = false;
        check(j,i,c,s);
      }
      ans = max(ans, colcnt);
    }
    return;
  }



  REP(kk,1,7){ //??????????????????????????????
    int bcol = s.A[0][0];
    dfs(kk,turn + 1,s);
  }

  return;
}

int main() {

while(1){
    cin>>h>>w>>c;
    if(h==0 && w==0 && c==0) break;
    REP(i,0,h)REP(j,0,w) cin>>p[i][j];

    ans = 0;
    S sc;
    REP(i,0,h)REP(j,0,w) sc.A[i][j] = p[i][j];

    REP(k,1,7) dfs(k,1,sc);

    cout<<ans<<endl;
  }

  return 0;
}

Measured memory_bytes: 3036