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

int h,w,c;
int map[6][8][8];
int visited[8][8];

void change_color(int stage,int x,int y,int src_color,int dist_color) {
	if(x<0 || x>=w || y<0 || y>=h)return;
	if(map[stage][y][x]!=src_color || src_color==dist_color)return;
	map[stage][y][x]=dist_color;
	change_color(stage,x-1,y,src_color,dist_color);
	change_color(stage,x+1,y,src_color,dist_color);
	change_color(stage,x,y-1,src_color,dist_color);
	change_color(stage,x,y+1,src_color,dist_color);
}

int get_color_num(int x,int y,int color) {
	int result=1;
	if(x<0 || x>=w || y<0 || y>=h)return 0;
	if(visited[y][x] || map[5][y][x]!=color)return 0;
	visited[y][x]=1;
	result+=get_color_num(x-1,y,color);
	result+=get_color_num(x+1,y,color);
	result+=get_color_num(x,y-1,color);
	result+=get_color_num(x,y+1,color);
	return result;
}

int tansaku(int stage,int prev_color) {
	int i,j,now_color;
	int result=0,now;
	for(now_color=1;now_color<=6;now_color++) {
		if((stage<5 && prev_color==now_color) ||
			(stage>=5 && now_color!=c))continue;
		for(i=0;i<h;i++) {
			for(j=0;j<w;j++)map[stage][i][j]=map[stage-1][i][j];
		}
		change_color(stage,0,0,map[stage][0][0],now_color);
		if(stage<5) {
			now=tansaku(stage+1,now_color);
			if(result<now)result=now;
		} else {
			memset(visited,0,sizeof(visited));
			result=get_color_num(0,0,c);
		}
	}
	return result;
}

int main(void) {
	int i,j;
	while(1) {
		scanf("%d%d%d",&h,&w,&c);
		if(h==0 && w==0 && c==0)break;
		for(i=0;i<h;i++) {
			for(j=0;j<w;j++)scanf("%d",&map[0][i][j]);
		}
		printf("%d\n",tansaku(1,0));
	}
	return 0;
}