#include<bits/stdc++.h>
using namespace std;

int ma, h, w, c, aaa;

void huga(int p[10][10], int tempt[10][10], int x, int y, int a){
  if(tempt[x][y] == a) return;
  tempt[x][y] = a;//cout<<x<<" "<<y<<endl;
  if(p[x][y] == p[x][y+1])huga(p, tempt, x, y+1, a);
  if(p[x][y] == p[x][y-1])huga(p, tempt, x, y-1, a);
  if(p[x][y] == p[x+1][y])huga(p, tempt, x+1, y, a);
  if(p[x][y] == p[x-1][y])huga(p, tempt, x-1, y, a);
}

int foo(int p[10][10], int count, int temp[10][10]){//cout<<count<<" "<<c<<endl;
  int tempp[10][10] = {},tempt[10][10] = {}, temp2[10] = {}, count2 = 0, count3 = 0 ,temp3 = p[1][1];


  // count2 = 1; temp2[count2] = c;


 
  // huga(p, tempt, 1, 1, 1);
  
  /* for(int i = 1; i <= h; i++){
    for(int j = 1; j <= w; j++){
      // tempp[i][j] = p[i][j];
      if(tempt[i][j+1] == 1|| tempt[i][j-1] == 1||tempt[i+1][j] == 1|| tempt[i-1][j] == 1){
	if(p[1][1] == p[i][j]) tempt[i][j] = 1;
	else if(p[i][j] != 0){ temp2[count2++] = p[i][j]; tempt[i][j] = 2;}
      }
    }
    }*/
  
  for(int i = 1; i <= h; i++){
    for(int j = 1; j <= w; j++){
       if(temp[i][j+1] == 1|| temp[i][j-1] == 1||temp[i+1][j] == 1|| temp[i-1][j] == 1){
	 if(temp[i][j] != 1){
	   //tempt[i][j] = 2;
	    huga(p, temp, i, j, 2);
	   temp2[count2++] = p[i][j];
	 }
       }
    }
  }
  
  temp2[count2++] = c;
  // if((p[1][1] == 5 && p[1][3] == 5) || (p[1][1] == 4 && p[1][3] == 4)){
  /*  cout<<"HHHHHHHHHHHHHHHH  "<<count<<endl;
  for(int i = 0; i <= h; i++){
	  for(int j = 0; j <= w; j++)cout<<tempt[i][j]<<" ";
	  cout<<endl;
	  }cout<<endl;
  
  for(int i = 0; i <= h; i++){
	  for(int j = 0; j <= w; j++)cout<<p[i][j]<<" ";
	  cout<<endl;
	  }cout<<endl;
	  // }*/
  
  
  if(count < 5){
    for(int k = 0; k < count2 ; k++){// cout<<temp2[k]<<" A "<<count2<<endl;
      for(int i = 1; i <= h; i++){
	for(int j = 1; j <= w; j++){
	  tempp[i][j] = p[i][j];
	  tempt[i][j] = temp[i][j];
	  //  if((p[i][j] == temp2[k] || p[i][j] == p[1][1]) && (tempt[i][j] == 1 || tempt[i][j] == 2)){ tempp[i][j] = temp2[k];}
	  if(( p[i][j] == p[1][1] && temp[i][j] == 1 ) ||(p[i][j] == temp2[k] &&  temp[i][j] == 2)) {tempp[i][j] = temp2[k]; tempt[i][j] = 1;}
	  // else if(p[i][j] == temp2[k] &&  tempt[i][j] == 2){tempp[i][j] = temp2[k];}
	  // else {tempp[i][j] = p[i][j];}//cout<<tempp[i][j];
	}//cout<<endl;
      }/*cout<<"---------"<<endl;
       for(int b = 0; b <= h; b++){
	for(int l = 0; l <= w; l++)cout<<tempp[b][l]<<" ";
	cout<<endl;
	}cout<<endl;*/
       // cin>>aaa;
      
      
      //  count++;//cout<<"XXXX"<<endl;
      foo(tempp, count+1, tempt);
    }

  } else {
    count3 = 0;
     for(int i = 1; i <= h; i++){
	for(int j = 1; j <= w; j++){
	  tempp[i][j] = p[i][j];
	  // tempt[i][j] = temp[i][j];
	  if(p[i][j] == p[1][1] && temp[i][j] == 1){tempp[i][j] = c; count3++;}
	  else if(p[i][j] == c && temp[i][j] == 2){ count3++; }
	  //else tempp[i][j] = p[i][j];//cout<<tempp[i][j];
	}//cout<<endl;
     }

         ma = max(count3, ma);
	 
	/* if(ma != count3) return 0;
	 
	 // cout<<"BBBBBBBBBBBBBBBBBBBBBb   "<<c<<endl;
	 for(int b = 0; b <= h; b++);
	 // for(int l = 0; l <= w; l++);//cout<<tempp[b][l]<<" ";
	    //	cout<<endl;
	    //cout<<endl;
	 //cin>>aaa;
	 // cout<<count3<<endl;
	 // cout<<"HHHHHHHHHHHHHHHH  "<<count<<endl;
	 for(int i = 0; i <= h; i++);{
	   for(int j = 0; j <= w; j++);//cout<<tempt[i][j]<<" ";
	   // cout<<endl;
	   }//cout<<endl;
	 
	 for(int i = 0; i <= h; i++){
	   for(int j = 0; j <= w; j++);//cout<<p[i][j]<<" ";
	    // cout<<endl;
	 }//cout<<endl;*/
	 
	 return 0;
  }
  
  return 0;
}

int main(){
  cin >> h >> w >> c;

  while( h != 0 || w != 0 || c != 0){
    int p[10][10] = {}, tempt[10][10] = {};//, temp[10][10] = {}, temp2[10] = {}, count , tempt[10][10] = {};//, tempp[10][10] = {};
    ma = 0;
    for(int i = 1; i <= h; i++){
      for(int j = 1; j <= w; j++){
	cin >> p[i][j];
      }
    }
    
    /*  temp[1][1]  = 1; count = 0;
    for(int i = 1; i <= h; i++){
      for(int j = 1; j <= w; j++){
	tempp[i][j] = p[i][j];
	  if(temp[i][j+1] == 1 || temp[i][j-1] == 1 ||temp[i+1][j] == 1 || temp[i-1][j] == 1){
	    if(p[1][1] == p[i][j]) temp[i][j] = 1;
	    else if(p[i][j] != 0){ temp2[count++] = p[i][j]; temp[i][j] = 2;}
	  }
      }
    }
    temp2[count++] = c;
    */
    /*  for(int k = 1; k <= 6; k++){//cout<<"GFGFGGF k"<<k<<endl;
     for(int i = 1; i <= h; i++){//cout<<"GFGFGGF i"<<i<<endl;
	for(int j = 1; j <= w; j++){//cout<<"GFGFGGF j"<<j<<" "<<h<<" "<<w<<endl;
	  tempt[i][j] = temp[i][j];//cout<<i<<endl;
	  if(p[i][j] == k && (temp[i][j] == 1 || temp[i][j] == 2)){tempp[i][j] = k; tempt[i][j] = 1;}
	  else tempp[i][j] = p[i][j];// cout<<i<<endl;
	}
     }//cout<<"GFGFGGF "<<k<<endl;
      foo(p, 0, temp);
      }*/
    huga(p, tempt, 1, 1, 1);
  
    foo(p, 1, tempt);



    
   /* for(int i = 0; i <= h; i++){
	  for(int j = 0; j <= w; j++)cout<<temp[i][j]<<" ";
       cout<<endl;
       }*/
    cout<<ma<<endl;
 
    cin >> h >> w >> c;
  }
  return 0;
}



Measured memory_bytes: 3084