#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>

using namespace std;

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define EPS 1e-14

#define all(v) v.begin(),v.end()
#define INF
#define mp
#define pb

#define fi
#define se

#define each

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

typedef long long ll;
int ss[8][8];
int main(){
    rep(i,8){
        rep(j,8){
            ss[i][j]=0;
        }
    }

    while(1){
        int h,w,c;
        int ss[8][8];
        int p[8][8];
        int cw[8];
        int c_num;
        cin >> h >> w >> c_num;
        if(h==0) break;
        rep(i,h){
            rep(j,w){
                cin >> ss[i][j];
            }
        }
        cw[0] = ss[0][0];
        cw[5] = c_num;
        cw[6] = 9;
        int ma=0;
        for(cw[1]=1;cw[1]<7;cw[1]++){
            for(cw[2]=1;cw[2]<7;cw[2]++){
                for(cw[3]=1;cw[3]<7;cw[3]++){
                    for(cw[4]=1;cw[4]<7;cw[4]++){
         
                            rep(i,h){
                                rep(j,w){
                                    p[i][j] = ss[i][j];
                                }
                            }
                            const int dx[] = {1,0,-1,0};
                            const int dy[] = {0,1,0,-1};
                            int cnt;

                            //??????????????????????????????
                            rep(luc,6){
                                queue <int> qx;
                                queue <int> qy;
                                qx.push(0);
                                qy.push(0);
                                cnt = 0;
                                while(!qx.empty()){
                                    p[qy.front()][qx.front()]=0;

                                    rep(i,4){
                                        int nx = qx.front() + dx[i];
                                        int ny = qy.front() + dy[i];
                                        if(nx >= 0 && nx < w &&
                                           ny >= 0 && ny < h &&
                                           p[ny][nx] == cw[luc]){
                                            qx.push(nx);
                                            qy.push(ny);
                                        }
                                    }
                                    // cout << qx.front() << " " << qy.front() << endl;
                                    qx.pop();qy.pop();
                                }
                                rep(y,h){
                                	rep(x,w){

                                		if(p[y][x]==0){
                                			p[y][x] = cw[luc+1] ;
                                			cnt++;
                                		}

                                	}
                                }
                                ma = max(ma,cnt);
                            }
                            
                            // if(cnt>7){
                            // rep(y,h){
                            //     rep(x,w){
                            //         cout << p[y][x] << " ";
                            //     }
                            //     cout << endl;
                            // }
                            // cout << "-----------"<<endl;
                            // }

                    }
                }
            }
        }
        cout << ma << endl;

    }
    return 0;
}

Measured memory_bytes: 3100