#include <stdio.h>

#define maxN 10

void dfs(int time, int color, int bfr[maxN][maxN]);
int changeColor(int bfr[maxN][maxN], int panel[maxN][maxN], int color, int x, int y);

int h, w, c, max;
int vx[] = {1,0,-1,0};
int vy[] = {0,1,0,-1};

int main(){
	int i, j;

	while(1){
		int panel[maxN][maxN] = {};

		scanf("%d %d %d", &h, &w, &c);
		if(h==0&&w==0&&c==0) break;

		for(i=1; i<=h; i++){
			for(j=1; j<=w; j++){
				scanf("%d", &panel[i][j]);
			}
		}
		dfs(0, panel[1][1], panel);
		printf("%d\n", max);

		max = 0;
	}

	return 0;
}

void dfs(int time, int color, int bfr[maxN][maxN]){
	int panel[maxN][maxN];
	int i, j, cnt = 0;

	for(i=0; i<maxN; i++){
		for(j=0; j<maxN; j++){
			panel[i][j] = bfr[i][j];
		}
	}

	if(time<4){
		for(i=1; i<=6; i++){
			if(i==color) continue;
			cnt = changeColor(bfr, panel, i, 1, 1);
			dfs(time+1, i, panel);
		}
	}else if(time==4){
		cnt = changeColor(bfr, panel, c, 1, 1);
		dfs(time+1, c, panel);
	}else if(time==5){
		cnt = changeColor(bfr, panel, 7, 1, 1);
		if(max<cnt) max = cnt;
	}
}

int changeColor(int bfr[maxN][maxN], int panel[maxN][maxN], int color, int x, int y){
	int i, cnt = 1;
	panel[y][x] = color;
	for(i=0; i<4; i++){
		if(bfr[y+vy[i]][x+vx[i]]==bfr[y][x] && panel[y+vy[i]][x+vx[i]]!=color){
			cnt += changeColor(bfr, panel, color, x+vx[i], y+vy[i]);
		}
	}
	return cnt;
}