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

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

#define fors(i, n) for (int i = 0; i < n; i++)
#define clean_visited fors(i, h) fors(j, w) visited[i][j] = 0
#define when_visited fors(i, h) fors(j, w) if (visited[i][j] == 1)

int parse()
{
    scanf("%d %d %d", &h, &w, &c);
    if (h == 0)
        return 0;
    fors(i, h) fors(j, w) scanf("%d", &map[0][i][j]);
    max_count = 0;
    return 1;
}

int dfs_fillVisited(int depth, int x, int y)
{
    visited[y][x] = 1;
    if (x + 1 < w && map[depth][y][x + 1] == map[depth][y][x] && visited[y][x + 1] != 1)
        dfs_fillVisited(depth, x + 1, y);
    if (y + 1 < h && map[depth][y + 1][x] == map[depth][y][x] && visited[y + 1][x] != 1)
        dfs_fillVisited(depth, x, y + 1);
    if (x > 0 && map[depth][y][x - 1] == map[depth][y][x] && visited[y][x - 1] != 1)
        dfs_fillVisited(depth, x - 1, y);
    if (y > 0 && map[depth][y - 1][x] == map[depth][y][x] && visited[y - 1][x] != 1)
        dfs_fillVisited(depth, x, y - 1);
}

int dfs(int depth, int flip)
{
    if (depth < 5)
    {
        fors(i, h) fors(j, w) map[depth + 1][i][j] = map[depth][i][j];
        clean_visited;
        dfs_fillVisited(depth, 0, 0);
        when_visited
        {
            map[depth + 1][i][j] = flip;
        }
        fors(i, 6)
        {
            dfs(depth + 1, i + 1);
        }
    }
    else
    {
        if (map[depth][0][0] != c)
            return 0;
        clean_visited;
        dfs_fillVisited(depth, 0, 0);
        int count = 0;
        when_visited
        {
            count++;
        }
        if (count > max_count)
            max_count = count;
    }
}
int main()
{
    while (parse())
    {
        fors(i, 6)
        {
            dfs(0, i + 1);
        }
        printf("%d\n", max_count);
    }
}

