import java.util.*;
class C {
public void run() {
Scanner sc
= new Scanner
(System.
in); while (sc.hasNext()) {
int h = sc.nextInt();
int w = sc.nextInt();
int c = sc.nextInt();
if ((h | w | c) == 0)
return;
int[][] p = new int[h][w];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
p[i][j] = sc.nextInt();
int ans = 0;
boolean[][][] mm = null;
for (int t = 0; t < 1296; t++) {
int[] cs = new int[5];
for (int j = 0, k = t; j < 4; j++, k /= 6)
cs[j] = k % 6 + 1;
cs[4] = c;
boolean[][][] map = new boolean[6][h][w];
dfs(0, 0, p[0][0], p, map[0]);
for (int k = 0; k < 5; k++)
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (map[k][i][j])
dfs(i, j, cs[k], p, map[k + 1]);
int res = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (map[5][i][j])
res++;
ans
= Math.
max(ans, res
); if (ans == res)
mm = map;
}
// for (int k = 0; k <= 5; k++)
// for (int i = 0; i < h; i++) {
// for (int j = 0; j < w; j++)
// System.out.print((mm[k][i][j] ? "1" : "0") + " ");
// System.out.println();
// }
}
}
static final int[] di = { 1, 0, -1, 0 };
static final int[] dj = { 0, 1, 0, -1 };
private void dfs(int i, int j, int c, int[][] p, boolean[][] map) {
map[i][j] = true;
for (int d = 0; d < 4; d++) {
int ni = i + di[d];
int nj = j + dj[d];
if (0 <= ni && ni < p.length && 0 <= nj && nj < p[0].length)
if (p[ni][nj] == c && !map[ni][nj])
dfs(ni, nj, c, p, map);
}
}
new C().run();
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKY2xhc3MgQyB7CgoJcHVibGljIHZvaWQgcnVuKCkgewoJCVNjYW5uZXIgc2MgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwoJCXdoaWxlIChzYy5oYXNOZXh0KCkpIHsKCQkJaW50IGggPSBzYy5uZXh0SW50KCk7CgkJCWludCB3ID0gc2MubmV4dEludCgpOwoJCQlpbnQgYyA9IHN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