#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

vector< vector<int> > c(int k,vector< vector<int> > M){
    queue< pair<int,int> > q;
    q.push(make_pair(0,0));
    int f_c = M.at(0).at(0);
    vector< vector<bool> > reached(M.size(),vector<bool>(M.at(0).size(),false));
    reached.at(0).at(0)=true;
    while(q.size()>0){
        pair<int,int> next=q.front();
        q.pop();
        for(int i=-1;i<=1;i++){
            if(0<=next.first+i && next.first+i<M.size()){
                if(M.at(next.first+i).at(next.second)==f_c && !(reached.at(next.first+i).at(next.second))){
                    reached.at(next.first+i).at(next.second)=true;
                    q.push(make_pair(next.first+i,next.second));
                }
            }
        }
        for(int j=-1;j<=1;j++){
            if(0<=next.second+j && next.second+j<M.at(0).size() ){
                if(M.at(next.first).at(next.second+j)==f_c && !(reached.at(next.first).at(next.second+j))){
                    reached.at(next.first).at(next.second+j)=true;
                    q.push(make_pair(next.first,next.second+j));
                }
            }
        }
    }
    for(int i=0;i<M.size();i++){
        for(int j=0;j<M.at(0).size();j++){
            if(reached.at(i).at(j)){
                M.at(i).at(j)=k;
            }
        }
    }
    return M;
}

int c_count(int l,int m,int k,vector< vector<int> > M){
    queue< pair<int,int> > q;
    q.push(make_pair(l,m));
    vector< vector<bool> > reached(M.size(),vector<bool>(M.at(0).size(),false));
    reached.at(0).at(0)=true;
    while(q.size()>0){
        pair<int,int> next=q.front();
        q.pop();
        for(int i=-1;i<=1;i++){
            if(0<=next.first+i && next.first+i<M.size()){
                if(M.at(next.first+i).at(next.second)==k && !(reached.at(next.first+i).at(next.second))){
                    reached.at(next.first+i).at(next.second)=true;
                    q.push(make_pair(next.first+i,next.second));
                }
            }
        }
        for(int j=-1;j<=1;j++){
            if(0<=next.second+j && next.second+j<M.at(0).size() ){
                if(M.at(next.first).at(next.second+j)==k && !(reached.at(next.first).at(next.second+j))){
                    reached.at(next.first).at(next.second+j)=true;
                    q.push(make_pair(next.first,next.second+j));
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<M.size();i++){
        for(int j=0;j<M.at(0).size();j++){
            if(reached.at(i).at(j)){
                ans++;
            }
        }
    }
    return ans;
}

int change(int count,vector< vector<int> > M,int k){
    int ans=0;
    if(count==5){
        /*
        for(int i=0;i<M.size();i++){
            for(int j=0;j<M.at(0).size();j++){
                if(M.at(i).at(j)==k){
                    ans=max(ans,c_count(i,j,k,M));
                }
            }
        }*/
        ans=c_count(0,0,k,M);
        return ans;
    }
    for(int i=1;i<=6;i++){
        vector< vector<int> > M_;
        M_=c(i,M);
        ans=max(ans,change(count+1,M_,k));
        /*
        if(ans==11){
            cout << endl;
        for(int i=0;i<M.size();i++){
            for(int j=0;j<M.at(0).size();j++){
                cout << M.at(i).at(j) << " ";
            }
            cout << endl;
        }
        cout << ans << endl;
        }
        */
    }
    return ans;
}

int main(){
    int h,w,color;
    cin >> h >> w >> color;
    while(!(h==0 && w==0 && color==0)){
        vector< vector<int> > M(h,vector<int>(w));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> M.at(i).at(j);
            }
        }
        /*
        vector< vector<int> > M_ = c(2,M);
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cout << M_.at(i).at(j) << " ";
            }
            cout << endl;
        }
        */
        int ans = change(0,M,color);
        cout << ans << endl;
        cin >> h >> w >> color;
    }
}


Measured memory_bytes: 3168