#include<stdio.h>
#include<stdlib.h>

typedef struct{
  int col;
  int check;
} panels;

int max,h,w,c,num;

void getmax(panels **panel,int color,int nest,int n);
void change(panels **panel,int color,int i,int j);
void countnum(panels **panel,int i,int j);


int main(void){
  panels **panel;
  int i,j,colors;
  while(scanf("%d %d %d",&h,&w,&c) && h && w && c){
    panel=(panels **)calloc(h,sizeof(panels *));
    for(i=0;i<h;i++) *(panel+i)=(panels *)calloc(w,sizeof(panels *));
    for(i=0;i<h;i++){
      for(j=0;j<w;j++){
	scanf("%d",&colors);
	(*(panel+i)+j)->col=colors;
	(*(panel+i)+j)->check=0;
      }
    }
    max=0;
    for(i=0;i<6;i++) getmax(panel,i+1,0,0);
    printf("%d\n",max);
    for(i=0;i<h;i++) free(*(panel+i));
    free(panel);
  }
  return 0;
}

void getmax(panels **panel,int color,int nest,int n){
  panels **tmp;
  int i,j,k;
  if(nest==4 && color!=c) return;
  tmp=(panels **)calloc(h,sizeof(panels *));
  for(i=0;i<h;i++) *(tmp+i)=(panels *)calloc(w,sizeof(panels *));
  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      (*(tmp+i)+j)->col=(*(panel+i)+j)->col;
      (*(tmp+i)+j)->check=0;
    }
  }
  change(tmp,color,0,0);
  num=0;
  countnum(tmp,0,0);
  if(max<num) max=num;
  if(nest==4 || num==n){
    for(i=0;i<h;i++) free(*(tmp+i));
    free(tmp);
    return;
  }
  for(i=0;i<6;i++) getmax(tmp,i+1,nest+1,1);
  for(i=0;i<h;i++) free(*(tmp+i));
  free(tmp);
}

void change(panels **panel,int color,int i,int j){
  (*(panel+i)+j)->check=1;
  if(i>0 && (*(panel+i)+j)->col==(*(panel+i-1)+j)->col && (*(panel+i-1)+j)->check==0) change(panel,color,i-1,j);
  if(i<h-1 && (*(panel+i)+j)->col==(*(panel+i+1)+j)->col && (*(panel+i+1)+j)->check==0) change(panel,color,i+1,j);
  if(j>0 && (*(panel+i)+j)->col==(*(panel+i)+j-1)->col && (*(panel+i)+j-1)->check==0) change(panel,color,i,j-1);
  if(j<w-1 && (*(panel+i)+j)->col==(*(panel+i)+j+1)->col && (*(panel+i)+j+1)->check==0) change(panel,color,i,j+1);
  (*(panel+i)+j)->col=color;
}

void countnum(panels **panel,int i,int j){
  (*(panel+i)+j)->check=2;
  if(i>0 && (*(panel+i)+j)->col==(*(panel+i-1)+j)->col && (*(panel+i-1)+j)->check!=2) countnum(panel,i-1,j);
  if(i<h-1 && (*(panel+i)+j)->col==(*(panel+i+1)+j)->col && (*(panel+i+1)+j)->check!=2) countnum(panel,i+1,j);
  if(j>0 && (*(panel+i)+j)->col==(*(panel+i)+j-1)->col && (*(panel+i)+j-1)->check!=2) countnum(panel,i,j-1);
  if(j<w-1 && (*(panel+i)+j)->col==(*(panel+i)+j+1)->col && (*(panel+i)+j+1)->check!=2) countnum(panel,i,j+1);
  num++;
}