#include<bits/stdc++.h>
using namespace std;
#define INF 1e9

int di[4] = {0, -1, 0, 1};
int dj[4] = {1, 0, -1, 0};

int temp, h, w, from, to, c;

void count(int i, int j, vector<vector<int> >& b, int& temp){
    temp++;
    b[i][j] = -1;
    for(int l = 0; l < 4; l++){
        int ni = i + di[l];
        int nj = j + dj[l];
        if(0 <= ni && ni < h && 0 <= nj && nj < w && b[ni][nj] == c) count(ni, nj, b, temp);
    }
    return;

}

void change(int i, int j, vector<vector<int> >& a){

    if(from == to) return;
    a[i][j] = to;
    for(int l = 0; l < 4; l++){
        int ni = i + di[l];
        int nj = j + dj[l];
        if(0 <= ni && ni < h && 0 <= nj && nj < w && a[ni][nj] == from) change(ni, nj, a);
    }
    return;
}

int main(){
    
    while(1){

        //入力
        cin >> h >> w >> c;
        if(!h) break;
        vector<vector<int> > a(h, vector<int> (w));
        for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) cin >> a[i][j];

        
        vector<vector<int> > b(h, vector<int> (w));
        //change(h, w, 0, 0, 1, 3, a);
        int ans = 0;

        /*
        change(h, w, 0, 0, b[0][0], 1, a);
        change(h, w, 0, 0, b[0][0], 1, a);
        change(h, w, 0, 0, b[0][0], 2, a);
        change(h, w, 0, 0, b[0][0], 2, a);
        change(h, w, 0, 0, b[0][0], c, a);
        */


        for(int i = 1; i <= 6; i++){
            for(int j = 1; j <= 6; j++){
                for(int k = 1; k <= 6; k++){
                    for(int l = 1; l <= 6; l++){

                        
                        for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) b[i][j] = a[i][j];
                        
                        //cout << "ok" << endl;
                        //cout << i << j << k << l << endl;
                        from = b[0][0];
                        to = i;
                        //cout << from << " " << to << endl;
                        change(0, 0, b);
                        from = b[0][0];
                        to = j;
                        //cout << from << " " << to << endl;                        
                        change(0, 0, b);
                        from = b[0][0];
                        to = k;
                        //cout << from << " " << to << endl;                        
                        change(0, 0, b);
                        from = b[0][0];
                        to = l;
                        //cout << from << " " << to << endl;
                        change(0, 0, b);
                        from = b[0][0];
                        to = c;
                        //cout << from << " " << to << endl; 

                        /*for(int i = 0; i < h; i++){
                            for(int j = 0; j < w; j++){
                                cout << b[i][j] << " ";
                            }
                            cout << endl;
                        }*/

                        change(0, 0, b);
                        //cout << "ok" << endl;
                        //int temp = 0;
                        //for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) if(b[i][j] == c) temp++;
                        temp = 0;
                        count(0, 0, b, temp);
                        ans = max(ans, temp);

                    }
                }
            }
        }

        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 3136