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

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define max(x,y) x>y?x:y
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)

typedef int bool;

#define true 1
#define false 0

int dd[]={-1,0,1,0,-1};

int h,w,c;
int p[11][11];

bool visc[11][11];
int cnt;

void cntc(int i,int j,int cc){
	visc[i][j]=true;
	cnt++;
	rep(d,4){
		int ni=i+dd[d],nj=j+dd[d+1];
		if(ni>=0&&ni<h&&nj>=0&&nj<w&&p[ni][nj]==cc&&!visc[ni][nj])cntc(ni,nj,cc);
	}
}

void change(int i,int j,int mc,int cc){
	p[i][j]=cc;
	visc[i][j]=true;
	rep(d,4){
		int ni=i+dd[d],nj=j+dd[d+1];
		if(ni>=0&&ni<h&&nj>=0&&nj<w&&p[ni][nj]==mc&&!visc[ni][nj])change(ni,nj,mc,cc);
	}
}

int dfs(int d){
	if(d==5){
		cnt=0;
		memset(visc,0,sizeof(visc));
		cntc(0,0,c);
		return cnt;
	}else{
		int res=0;
		int sav[11][11];
		memcpy(sav,p,sizeof(p));
		if(d==4){
			memset(visc,0,sizeof(visc));
			change(0,0,p[0][0],c);
			maxch(res,dfs(d+1));
		}else{
			repl(i,1,6+1){
				memset(visc,0,sizeof(visc));
				change(0,0,p[0][0],i);
				maxch(res,dfs(d+1));
				memcpy(p,sav,sizeof(sav));
			}
		}
		return res;
	}
}

int main(){
	//cin.sync_with_stdio(false);
	while(1){
		scanf("%d%d%d",&h,&w,&c);
		if(h==0)break;
		rep(i,h)rep(j,w)scanf("%d",&p[i][j]);
		printf("%d\n", dfs(0));
	}
	return 0;
}