#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)

using namespace std;

typedef pair<int,int>P;
typedef pair<P,int>pii;

const int MAX_N = 8;
const int dx[] = {1,-1,0,0};
const int dy[] = {0,0,1,-1};

int h,w,c,opt;

void dfs(int cnt,vector<vector<int> >& fd)
{
    if(cnt == 5){
        int res = 0;
        int flag[MAX_N][MAX_N];
        rep(i,h){
            rep(j,w){
                flag[i][j] = INF;
            }
        }
        queue<pii> que;
        res++;
        que.push(pii(P(0,0),0));
        flag[0][0] = 0;
        while(!que.empty()){
            pii p = que.front();
            que.pop();
            int x = (p.first).first;
            int y = (p.first).second;
            rep(i,4){
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(0 <= nx && nx < h && 0 <= ny && ny < w){
                    if(fd[nx][ny] == c && flag[nx][ny] > p.second + 1){
                        que.push(pii(P(nx,ny),p.second + 1));
                        flag[nx][ny] = p.second + 1;
                        res++;
                    }
                }
            }
        }
        opt = max(opt,res);
        return;
    }
    for(int i=1;i<=6;i++){
        if(cnt != 4 || i == c){
            vector<P> trans;
            int flag[MAX_N][MAX_N];
            int bf[MAX_N][MAX_N];
            queue<pii> que;
            rep(j,h){
                rep(k,w){
                    flag[j][k] = INF;
                }
            }
            que.push(pii(P(0,0),0));
            flag[0][0] = 0;
            trans.push_back(P(0,0));
            while(!que.empty()){
                pii p = que.front();
                que.pop();
                int x = (p.first).first;
                int y = (p.first).second;
                rep(j,4){
                    int nx = x + dx[j];
                    int ny = y + dy[j];
                    if(0 <= nx && nx < h && 0 <= ny && ny < w){
                        if(fd[nx][ny] == fd[0][0] && flag[nx][ny] > p.second + 1){
                            que.push(pii(P(nx,ny),p.second+1));
                            flag[nx][ny] = p.second + 1;
                            trans.push_back(P(nx,ny));
                        }
                    }
                }
            }
            rep(j,trans.size()){
                int x = trans[j].first;
                int y = trans[j].second;
                bf[x][y] = fd[x][y];
                fd[x][y] = i;
            }
            dfs(cnt+1,fd);
            rep(j,trans.size()){
                int x = trans[j].first;
                int y = trans[j].second;
                fd[x][y] = bf[x][y];
            }
        }
    }
    return;
}

int main()
{
    while(1){
        scanf("%d%d%d",&h,&w,&c);
        if(h == 0 && w == 0 && c == 0){
            break;
        }
        opt = 0;
        vector<vector<int> > a;
    	rep(i,h){
            vector<int> vec;
            int hoge;
            rep(j,w){
                scanf("%d",&hoge);
                vec.push_back(hoge);
            }
            a.push_back(vec);
    	}
        dfs(0,a);
        printf("%d\n",opt);
    }
	return 0;
}

Measured memory_bytes: 3264