#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;

// →↑←↓
const int dx[] = {1,0,-1,0};
const int dy[] = {0,-1,0,1};


const double EPS = 1e-9;

struct State{
    vector<vector<int> > field;
    int level;
    State(vector<vector<int> > f,int l) : field(f),level(l) {};
};

int cntup(const vector<vector<int> > &field,const int c){
    if(field[0][0] != c) return 0;
    int h = field.size(),w = field[0].size();
    queue<pii> que;
    que.push(make_pair(0,0));
    vector<vector<char> > used(h,vector<char>(w,false));
    int ret = 0;
    while(not que.empty()){
        int y=que.front().first;
        int x=que.front().second;
        que.pop();
        if(used[y][x]) continue;
        used[y][x] = true;
        ret++;
        for(int i=0;i<4;i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 or ny >= h or nx < 0 or nx >= w) continue;
            if(field[ny][nx] == c){
                que.push(make_pair(ny,nx));
            }
        }
    }
    return ret;
}

vector<vector<int> > go(const vector<vector<int> > &field,int c){
    if(field[0][0] == c) return field;
    vector<vector<int> > ret = field;
    int h = field.size(),w = field[0].size();
    queue<pii> que;
    que.push(make_pair(0,0));
    vector<vector<char> > used(h,vector<char>(w,false));

    while(not que.empty()){
        int y=que.front().first;
        int x=que.front().second;
        que.pop();
        if(used[y][x]) continue;
        used[y][x] = true;
        ret[y][x] = c;
        for(int i=0;i<4;i++){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(ny < 0 or ny >= h or nx < 0 or nx >= w) continue;
            if(field[ny][nx] == field[0][0]){
                que.push(make_pair(ny,nx));
            }
        }
    }
    return ret;
}

int main(){
    while(true){
        int h,w,c;
        cin >> h >> w >> c;
        if(h == 0 and w == 0 and c == 0) break;
        vector<vector<int> > field(h,vector<int>(w));
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> field[i][j];
            }
        }

        stack<State> st;
        st.push(State(field,0));
        int ret = 0;
        while(not st.empty()){
            State s = st.top();
            st.pop();
            if(s.level == 5){
                // for(int i=0;i<s.field.size();i++){
                //     for(int j=0;j<s.field[i].size();j++){
                //         cerr << s.field[i][j];
                //     }
                //     cerr << endl;
                // }
                // cerr << endl;
                ret = max(ret,cntup(s.field,c));
            }else{
                for(int i=1;i<=6;i++){
                    st.push(State(go(s.field,i),s.level+1));
                }
            }
        }
        cout << ret << endl;
    }
    return 0;
}

Measured memory_bytes: 1260