#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = 998244353;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e6 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()

struct UnionFindTree {
    vector<int> par;
    vector<int> rank;
    vector<int> siz;

    void init(int n) {
        par.resize(n);
        rank.resize(n);
        siz.resize(n);
        for (int i = 0; i < n; i++) {
            par[i] = i;
            rank[i] = 0;
            siz[i] = 1;
        }
    }
    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) rank[x]++;
        par[y] = x;
        siz[x] += siz[y];
    }
    bool is_same(int x, int y) {
        return find(x) == find(y);
    }
    int size(int x) {
        x = find(x);
        return siz[x];
    }
};

int h, w, c, p[10][10];
vector<vector<int> > ls;

void dfs(int i, vector<int> &v){
    if(i==4){
        ls.push_back(v);
    }
    else{
        for(int m=1;m<=6;m++){
            if(i==0 || v.back() != m){
                v.push_back(m);
                dfs(i+1,v);
                v.pop_back();
            }
        }
    }
}

int solve(int si, int sj){
    int res = 0;
    UnionFindTree uf;
    for(int m=0;m<ls.size();m++){
        if(ls[m][0] == p[si][sj])continue;
        uf.init(h*w);
        queue<P> que, used;
        que.push({si,sj});
        int now = p[si][sj];
        for(int i=0;i<6;i++){
            //cout << now << endl;
            while(!que.empty()){
                P tmp = que.front();
                que.pop();
                int pi = tmp.first, pj = tmp.second;
                if(now == p[pi][pj]){
                    //cout << pi << " " << pj << endl;
                    uf.unite(si*w+sj, pi*w+pj);
                    for(int k=0;k<4;k++){
                        int ni = pi + di[k], nj = pj + dj[k];
                        if(ni < 0 || ni >= h) continue;
                        if(nj < 0 || nj >= w) continue;
                        if(uf.is_same(si*w+sj, ni*w+nj))continue;
                        que.push({ni,nj});
                    }
                }
                else{
                    used.push(tmp);
                }
            }
            if(i<4){
                now = ls[m][i];
            }
            else{
                now = c;
            }
            while(!used.empty()){
                que.push(used.front());
                used.pop();
            }
        }
        if(res < uf.size(si*w+sj)){
            res = uf.size(si*w+sj);
            //cout << res << endl;
            //for(auto t : ls[m]){
                //cout << t << " ";
            //}
            //cout << c << endl;
        }
    }
    return res;
}

int main() {
    vector<int> v;
    dfs(0,v);
    while(cin >> h >> w >> c, h){
        REP(i,h)REP(j,w) cin >> p[i][j];
        int ans = 0;
        cout << solve(0,0) << endl;
    }
    return 0;
}


Measured memory_bytes: 3228