h,w,c,max;
int *_M;
int D(x,y,c,n){
	int r=1;
	_M[x+(w+2)*y]=n;
	if(_M[x-1+(w+2)*y]==c)r+=D(x-1,y,c,n);
	if(_M[x+1+(w+2)*y]==c)r+=D(x+1,y,c,n);
	if(_M[x+(w+2)*(y-1)]==c)r+=D(x,y-1,c,n);
	if(_M[x+(w+2)*(y+1)]==c)r+=D(x,y+1,c,n);
	return r;
}
E(d,M)int *M;{
	if(d==4&&M[1+w+2]==c)return;
	int *m=malloc((w+2)*(h+2));
	if(d==4){
		memcpy(m,M,(w+2)*(h+2));_M=m;
		D(1,1,m[1+w+2],c);
		int k=D(1,1,m[1+w+2],-1,m);
		if(max<k)max=k;
	}else{
		int i=1;
		for(;i<7;i++){
			if(M[1+w+2]==i)continue;
			memcpy(m,M,(w+2)*(h+2));_M=m;
			D(1,1,m[1+w+2],i,m);
			E(d+1,m);
		}
	}
	free(m);
}
a[100];
main(i,j){for(;scanf("%d%d%d",&h,&w,&c),c;printf("%d\n",max)){
	memset(a,0,(w+2)*(h+2));
	for(j=1;j<h+1;j++)for(i=1;i<w+1;i++)scanf("%d",a+i+(w+2)*j);
	E(max=0,a);
}}