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

#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

int h,w,c;

bool used[10][10];

vvi coloring(int color,vvi v){
    int c=v[0][0];
    vector<vector<bool>> used(h,vector<bool>(w,true));
    queue<pii> que;
    que.push(make_pair(0,0));
    while(!que.empty()){
        auto q=que.front();
        que.pop();
        int x=q.first;
        int y=q.second;

        used[x][y]=false;
        v[x][y]=color;
        rep(i,4){
            int nx=x+dx[i],ny=y+dy[i];
            if(nx>=0 and nx<h and ny>=0 and ny<w and v[nx][ny]==c and used[nx][ny]){
                que.push(make_pair(nx,ny));
            }
        }
    }
    return v;
}

int counter(vvi v,int val=0){
    int ret=0;
    vector<vector<bool>> used(h,vector<bool>(w,true));
    queue<pii> que;
    que.push(make_pair(0,0));
    used[0][0]=false;
    while(!que.empty()){
        auto q=que.front();
        que.pop();
        ret++;
        if(val==1) cerr << q << endl;
        int x=q.first,y=q.second;
        rep(i,4){
            int nx=x+dx[i],ny=y+dy[i];
            if(0<=nx and nx<h and 0<=ny and ny<w and used[nx][ny] and v[nx][ny]==v[0][0]){
                used[nx][ny]=false;
                que.push(make_pair(nx,ny));
            }
        }
    }
    return ret;
}

void solve(){
    vvi p(h,vi(w));
    rep(i,h) rep(j,w) cin>>p[i][j];
    rep(i,h) rep(j,w) p[i][j]--;

    int ans=0;
    rep(i,6){
        if(i==p[0][0]) continue;
        rep(j,6) rep(k,6) rep(l,6){
            auto v = coloring(i,p);
//            if(i==1 and j==2 and k==3 and l==4){
//                cerr << "---after i-------" << endl;
//                rep(hoge,h) cerr << v[hoge] << endl;
//            }
            v = coloring(j,v);
//           if(i==1 and j==2 and k==3 and l==4){
//               cerr << "---after j-------" << endl;
//               rep(hoge,h) cerr << v[hoge] << endl;
//           }
            v = coloring(k,v);
//          if(i==1 and j==2 and k==3 and l==4){
//              cerr << "----after k------" << endl;
//              rep(hoge,h) cerr << v[hoge] << endl;
//          }
            v = coloring(l,v);
//           if(i==1 and j==2 and k==3 and l==4){
//               cerr << "-----after l-----" << endl;
//               rep(hoge,h) cerr << v[hoge] << endl;
//           }
            v = coloring(c,v);
//           if(i==1 and j==2 and k==3 and l==4){
//               cerr << "-----last -----" << endl;
//               rep(hoge,h) cerr << v[hoge] << endl;
//           }
//            int tmp;
//            if(i==1 and j==2 and k==3 and l==4) tmp=counter(v,1);
//            else tmp=counter(v);
            int tmp=counter(v);

            ans = max(ans,tmp);
//            if(tmp==6) cerr << i << " " << j << " " << k << " " << l << endl;
        }
    }

    cout << ans << endl;
}

int main(){
    while(cin>>h>>w>>c){
        if(h==0 and w==0 and c==0) break;
        c--;
        solve();
    }
}

Measured memory_bytes: 3172