#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = k; i < (int)(n); i++)

#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second

typedef long long ll;
const int         dx[] = {0, 1, 0, -1};
const int         dy[] = {1, 0, -1, 0};

int main() {
    // ifstream in("cin.txt");
    // cin.rdbuf(in.rdbuf());
    int h, w, c;
    int prob_num = 0;
    while (cin >> h >> w >> c, h) {

        int ma = 0;
        int p[10][10] = {};
        rep(y, h) rep(x, w) cin >> p[y][x];
        // prob_num += 1;
        // if (prob_num == 23) {
        //     cout << h << " " << w << " " << c << endl;
        //     rep(y, h) {
        //         rep(x, w) cout << p[y][x] << " ";
        //         cout << endl;
        //     }
        // }
        int a[6] = {};
        rep(bit, pow(6, 4)) {
            int tmp_bit = bit;
            rep(i, 4) {
                a[i] = tmp_bit % 6;
                tmp_bit /= 6;
                a[i] += 1;
                if (i > 0 && a[i - 1] == a[i]) a[0] = -1;
                //                cout << a[i] << " ";
            }
            if (a[0] == -1 || a[3] == c) continue;
            //            cout << endl;
            a[4] = c;
            //            rep(i,c) cout << a[i] << " ";
            //            cout << endl;
            int t[10][10];
            rep(y, h) rep(x, w) t[y][x] = p[y][x];

            rep(i, 5) {

                queue<pii> que;
                que.push(pii(0, 0));
                int  now_color = t[0][0];
                bool u[10][10] = {};
                while (!que.empty()) {
                    int x, y;
                    tie(x, y) = que.front();
                    que.pop();
                    if (u[y][x]) continue;
                    u[y][x] = true;
                    t[y][x] = a[i];
                    rep(d, 4) {
                        int nx = x + dx[d];
                        int ny = y + dy[d];
                        if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
                        if (u[ny][nx] || t[ny][nx] != now_color) continue;
                        que.push(pii(nx, ny));
                    }
                }
            }
            int        cnt = 0;
            queue<pii> qe;
            qe.push(pii(0, 0));
            bool uu[10][10] = {};

            while (!qe.empty()) {
                int xp, yp;

                tie(xp, yp) = qe.front();
                qe.pop();


                if (uu[yp][xp]) continue;
                uu[yp][xp] = true;
                // if (cnt > 1000) {
                //     // cout << qe.front().fi << " : " << qe.front().se << endl;
                //     // rep(y, h) {
                //     //     rep(x, w) { cout << uu[y][x] << " "; }
                //     //     cout << endl;
                //     // }
                // }
                cnt += 1;
                //                memset(uu,1,sizeof(uu));
                rep(d, 4) {
                    int nx = xp + dx[d];
                    int ny = yp + dy[d];
                    if (nx < 0 || w <= nx || ny < 0 || h <= ny) continue;
                    if (uu[ny][nx]) continue;
                    if (t[ny][nx] != c) continue;
                    qe.push(pii(nx, ny));
                }
            }
            // if (cnt == 9) cout << bit << endl;
            ma = max(ma, cnt);
        };
        cout << ma << endl;
    }
    return 0;
}



Measured memory_bytes: 3080