//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>

using namespace std;

typedef vector<int> VI;
typedef vector <VI> VVI;
typedef pair<int, int> PII;

#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i)
#define REP(i, n)  FOR(i,0,n)
#define RREP(i, n) for(int i = n-1;i >= 0;i--)
#define FORLL(i, a, b) for(LL i=LL(a);i<LL(b);++i)
#define RFORLL(i, a, b) for(LL i=LL(b)-1;i>=LL(a);--i)
#define REPLL(i, n) for(LL i=0;i<LL(n);++i)
#define RREPLL(i, n) for(LL i=LL(n)-1;i>=0;--i)
#define FOREACH(x, v) for(auto &(x) : (v))
#define FORITER(x, v) for(auto (x) = (v).begin(); (x) != (v).end(); ++(x))

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

int draw(int h, int w, int c, VVI p, int stage) {
    //PIIは first=y second = x;
    PII start = make_pair(0, 0);

    if (stage == 5) {
        int count = 0;
        VVI map = p;
        VVI used(h, VI(w, false));

        queue<PII> Q;
        Q.push(start);

        while (!Q.empty()) {
            PII pii = Q.front();
            Q.pop();
            if (used[pii.first][pii.second]) continue;
            if (map[pii.first][pii.second] != c) continue;

            used[pii.first][pii.second] = true;
            count++;

            REP(j, 4) {
                PII next = make_pair(dy[j] + pii.first, dx[j] + pii.second);
                if (next.first >= 0
                    && next.first < h
                    && next.second >= 0
                    && next.second < w) {
                    Q.push(next);
                }
            }

        }

//        if (map[start.first][start.second] == 5) {
//            bool ok = true;
//            REP(i, w){
//                if(map[0][w] != 5){
//                    ok = false;
//                }
//            }
//            if(ok) {
//                REP(i, h) {
//                    REP(j, w) {
//                        cout << map[i][j];
//                    }
//                    cout << endl;
//                }
//                cout << endl;
//            }
//        }

        return count;
    }

    int ans = 0;

    for (int i = 1; i <= 6; i++) {
        VVI map = p;
        int start_color = map[start.first][start.second];

        if (i == start_color) continue;

        queue<PII> S;
        S.push(start);

        while (!S.empty()) {
            PII pii = S.front();
            S.pop();

            map[pii.first][pii.second] = i;

            REP(j, 4) {
                PII next = make_pair(dy[j] + pii.first, dx[j] + pii.second);
                if (next.first >= 0
                    && next.first < h
                    && next.second >= 0
                    && next.second < w
                    && map[next.first][next.second] == start_color) {
                    S.push(next);
                }
            }

        }


        ans = max(draw(h, w, c, map, stage + 1), ans);

    }

    return ans;

}

int main() {

    while (true) {
        int h, w, c;
        cin >> h >> w >> c;

        if (h == 0 && w == 0 && c == 0) {
            break;
        }

        VVI p(h, VI(w));
        REP(i, h)REP(j, w) cin >> p[i][j];

        int ans = draw(h, w, c, p, 0);

        cout << ans << endl;

    }

    return 0;
}
























































Measured memory_bytes: 3184