// Aizu 1174: Identically Colored Panels Connection
// 2017.9.17 bal4u@uu

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

typedef struct { int r, c; } Q;
Q q[70]; int top, end;
char mk[8][8];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
char map[8][8]; int h, w, color;
int ans;

int count(char p[8][8])
{
	int i, r, c, r2, c2, f;
	q[0].r = q[0].c = 0, top = 0, end = 1, memset(mk, 0, sizeof(mk)), mk[0][0] = 1, f = 1;
	while (top < end) {
		r = q[top].r, c = q[top++].c;
		for (i = 0; i < 4; i++) {
			r2 = r + mv[i][0], c2 = c + mv[i][1];
			if (r2 < 0 || r2 >= h || c2 < 0 || c2 >= w || p[r2][c2] != p[r][c] || mk[r2][c2]) continue;
			mk[r2][c2] = 1, f++, q[end].r = r2, q[end++].c = c2;
		}
	}
	return f;
}

void combi(int c0, int k, char p[8][8])
{
	int i, j, r, c, r2, c2, m;
	char t[8][8];

	if (k == 5) return;
	for (j = 1; j <= 6; j++) {
		if (j == c0) continue;
		memcpy(t, p, sizeof(t)); t[0][0] = j;
		q[0].r = q[0].c = 0, top = 0, end = 1, memset(mk, 0, sizeof(mk)), mk[0][0] = 1;
		while (top < end) {
			r = q[top].r, c = q[top++].c;
			for (i = 0; i < 4; i++) {
				r2 = r + mv[i][0], c2 = c + mv[i][1];
				if (r2 < 0 || r2 >= h || c2 < 0 || c2 >= w || t[r2][c2] != c0 || mk[r2][c2]) continue;
				mk[r2][c2] = 1, t[r2][c2] = j, q[end].r = r2, q[end++].c = c2;
			}
		}
		if (j == color) if ((m = count(t)) > ans) ans = m;
		combi(j, k+1, t);
	}
}

int main()
{
	int a, r, c;

	while(scanf("%d%d%d", &h, &w, &color) && h) {
		for (r = 0; r < h; r++) for (c = 0; c < w; c++) scanf("%d", &a), map[r][c] = a;
		ans = 0; if (map[0][0] == color) ans = count(map);
		combi(map[0][0], 0, map);
		printf("%d\n", ans);
	}
	return 0;
}