#include <bits/stdc++.h>
#define REP(a,b,c) for(int a=b; a<(int)c; a++)
#define REPE(a,b,c) for(int a=b; a<=(int)c; a++)
#define REPD(a,b,c) for(int a=b; a>=(int)c; a--)
#define rep(a,b) REP(a,0,b)
#define repd(a,b) REPD(a,b,0)
typedef long long int ll;
using namespace std;
typedef vector<vector<int> > board;
typedef vector<vector<bool> > conect_map;
typedef struct{int x;int y;}pos;
int h, w, c;
void show(conect_map p){
  rep(i,h){
    rep(j,w){
      if(p[i][j]){
        cout << "o";
      }else{
          cout << "x";
      }
    }
    cout << endl;
  }
}
void show(board b){
  rep(i,h){
    rep(j,w) cout << b[i][j];
    cout << endl;
  }
}

int func(board b){
  conect_map rtn(h, vector<bool>(w, false));
  vector<pos> now;
  rtn[0][0] = true;
  int cnt = 0;
  now.push_back({0,0});
  while(now.size() != 0){
    vector<pos> next;
    cnt += now.size();
    rep(i,now.size()){
      int x,y;
      x = now[i].x+1; y= now[i].y;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
      x = now[i].x-1; y= now[i].y;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
      x = now[i].x; y= now[i].y+1;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
      x = now[i].x; y= now[i].y-1;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
    }
    now = next;
  }
  return cnt;
}
board move(board b, int color){
  conect_map rtn(h, vector<bool>(w, false));
  vector<pos> now;
  rtn[0][0] = true;
  now.push_back({0,0});
  int c = b[0][0];
  while(now.size() != 0){
    vector<pos> next;
    rep(i,now.size()){
      // cout << now[i].x << "," << now[i].y << endl;
      int x,y;
      x = now[i].x+1; y= now[i].y;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
      x = now[i].x-1; y= now[i].y;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
      x = now[i].x; y= now[i].y+1;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
      x = now[i].x; y= now[i].y-1;
      if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==c){
        next.push_back({x,y});
        rtn[x][y] = true;
      }
    }
    now = next;
  }
  //cout << "color:" << color << endl;
  //show(rtn);
  rep(i,h){
    rep(j,w){
      if(rtn[i][j]){
        b[i][j] = color;
        int x,y;
        x = i+1; y= j;
        if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==color){
          b[x][y] = color;
        }
        x = i-1; y= j;
        if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==color){
          b[x][y] = color;
        }
        x = i; y= j+1;
        if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==color){
          b[x][y] = color;
        }
        x = i; y= j-1;
        if(x >= 0 && x<h && y>=0 && y<w && !rtn[x][y] && b[x][y]==color){
          b[x][y] = color;
        }
      }
    }
  }
  return b;
}
int DFS(board b,int n){
  //cout << "n:"<< n << endl;
  if(n==5){
    //cout << func(b) << endl;
    //show(b);
    return func(b);
  }
  if(n!=4){
    board sent = b;
    int rtn = 0;
    REPE(i,1,6){
      sent = move(b,i);
      rtn = max(rtn, DFS(sent,n+1));
    }
    return rtn;
  }else{
    board sent = b;
    sent = move(b,c);
    return DFS(sent, n+1);
  }

}


int main(){
  while(true){
    cin >> h >> w >> c;
    if(h==0 && w==0 && c==0) break;
    board b(h,vector<int>(w,0));
    rep(i,h) rep(j,w) cin >> b[i][j];
    cout << DFS(b,0) << endl;;
  }
  return 0;
}

Measured memory_bytes: 1236