#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
int c;
int ans;

void dfs(int n, vector<vector<int>> grid){
    int x = (int)grid.size();
    int y = (int)grid[0].size();
    vector<vector<bool>> connect(x, vector<bool>(y, false));
    queue<P> que;
    connect[0][0] = true;
    que.push({0, 0});
    vector<int> dx = {0, 0, 1, -1};
    vector<int> dy = {1, -1, 0, 0};
    while (que.size()){
        int nx = que.front().first;
        int ny = que.front().second;
        que.pop();
        for (int i = 0; i < 4; i++){
            if (0 <= nx + dx[i] && nx + dx[i] < x && 0 <= ny + dy[i] && ny + dy[i] < y){
                if (grid[nx + dx[i]][ny + dy[i]] == grid[0][0] && !connect[nx + dx[i]][ny + dy[i]]){
                    connect[nx + dx[i]][ny + dy[i]] = true;
                    que.push({nx + dx[i], ny + dy[i]});
                }
            }
        }
    }
    if (n == 4){
        que.push({0, 0});
        vector<int> dx = {0, 0, 1, -1};
        vector<int> dy = {1, -1, 0, 0};
        vector<vector<bool>> nconnect(x, vector<bool>(y, false));
        nconnect[0][0] = true;
        while (que.size()){
            int nx = que.front().first;
            int ny = que.front().second;
            que.pop();
            for (int i = 0; i < 4; i++){
                if (0 <= nx + dx[i] && nx + dx[i] < x && 0 <= ny + dy[i] && ny + dy[i] < y){
                    if ((grid[nx + dx[i]][ny + dy[i]] == c || connect[nx + dx[i]][ny + dy[i]]) && !nconnect[nx + dx[i]][ny + dy[i]]){
                        nconnect[nx + dx[i]][ny + dy[i]] = true;
                        que.push({nx + dx[i], ny + dy[i]});
                    }
                }
            }
        }
        int tmp = 0;
        for (int i = 0; i < x; i++){
            for (int j = 0; j < y; j++){
                if (nconnect[i][j]) tmp++;
            }
        }
        ans = max(ans, tmp);
    }
    else{
        for (int nc = 1; nc < 7; nc++){
            vector<vector<int>> ngrid(x, vector<int>(y));
            vector<vector<bool>> nconnect(x, vector<bool>(y, false));
            nconnect[0][0] = true;
            ngrid = grid;
            que.push({0, 0});
            vector<int> dx = {0, 0, 1, -1};
            vector<int> dy = {1, -1, 0, 0};
            while (que.size()){
                int nx = que.front().first;
                int ny = que.front().second;
                que.pop();
                for (int i = 0; i < 4; i++){
                    if (0 <= nx + dx[i] && nx + dx[i] < x && 0 <= ny + dy[i] && ny + dy[i] < y){
                        if ((ngrid[nx + dx[i]][ny + dy[i]] == nc || connect[nx + dx[i]][ny + dy[i]]) && !nconnect[nx + dx[i]][ny + dy[i]]){
                            nconnect[nx + dx[i]][ny + dy[i]] = true;
                            que.push({nx + dx[i], ny + dy[i]});
                        }
                    }
                }
            }
            for (int i = 0; i < x; i++){
                for (int j = 0; j < y; j++){
                    if (nconnect[i][j]) ngrid[i][j] = nc;
                }
            }
            dfs(n+1, ngrid);
        }
    }
}

int main(){
    while (1){
        ans = 0;
        int h, w;
        cin >> h >> w >> c;
        if (h == 0 && w == 0 && c == 0) break;
        vector<vector<int>> grid(h, vector<int>(w));
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) cin >> grid[i][j];
        dfs(0, grid);
        cout << ans << endl;
    }
}


Measured memory_bytes: 3128