// 左上の色を変える順番を全列挙
// 列挙したものについて、それぞれシミュレート
#include <bits/stdc++.h>
using namespace std;

int main() {
    while (true) {
        int h, w, c;
        cin >> h >> w >> c;
        if (!(h || w || c)) break;

        vector<vector<int>> p(h, vector<int>(w));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                cin >> p[i][j];
            }
        }

        //cout << "input done" << endl;
        // 全列挙
        auto enumaration = [](int c0, int c, vector<vector<int>> &res) -> void {
            stack<pair<int, int>> stk;
            for (int i = 1; i <= 6; ++i) {
                if (i != c0) stk.emplace(0, i);
            }
            vector<int> tmp(5);
            while (!stk.empty()) {
                auto P = stk.top();
                stk.pop();
                if (P.first >= 5) {
                    bool flg = true;
                    for (int i = 1; i < 5; ++i) {
                        if (tmp[i] == tmp[i - 1]) flg = false;
                    }
                    assert(flg);
                    if (tmp.back() == c) res.push_back(tmp);
                    continue;
                }
                tmp[P.first] = P.second;
                for (int i = 1; i <= 6; ++i) {
                    if (i != P.second) stk.emplace(P.first + 1, i);
                }
            }
        };

        vector<vector<int>> candidate;
        enumaration(p[0][0], c, candidate);
        //cout << candidate.size() << endl;

        //cout << "enumeration done" << endl;

        // それぞれに対してシミュレーション
        static const int dx[] = {-1, 0, 1, 0};
        static const int dy[] = {0, 1, 0, -1};
        auto simulate = [&](vector<int> &op) -> int {
            static int Panel[10][10];
            static bool visited[10][10];
            for (int i = 0; i < h; ++i) {
                for (int j = 0; j < w; ++j) {
                    Panel[i][j] = p[i][j];
                    visited[i][j] = false;
                }
            }

            for (int color : op) {
                int prev = Panel[0][0];
                stack<pair<int, int>> tsk;
                assert(prev != color);
                tsk.emplace(0, 0);
                while (!tsk.empty()) {
                    int x = tsk.top().first, y = tsk.top().second;
                    tsk.pop();
                    if (Panel[x][y] != prev) continue;
                    //cout << x << " " << y << endl;
                    Panel[x][y] = color;
                    for (int i = 0; i < 4; ++i) {
                        int nx = x + dx[i], ny = y + dy[i];
                        if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
                        tsk.emplace(nx, ny);
                    }
                }
            }
            //cout << "simullation done" << endl;
            int res = 0;
            stack<pair<int, int>> tsk;
            tsk.emplace(0, 0);
            while (!tsk.empty()) {
                int x = tsk.top().first, y = tsk.top().second;
                tsk.pop();
                if (visited[x][y] || Panel[x][y] != c) continue;
                visited[x][y] = true;
                ++res;
                for (int i = 0; i < 4; ++i) {
                    int nx = x + dx[i], ny = y + dy[i];
                    if (nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
                    tsk.emplace(nx, ny);
                }
            }

            return res;
        };

        int ans = 0;
        for (vector<int> &cd : candidate) {
            ans = max(ans, simulate(cd));
        }

        //cout << "simulation done" << endl;
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 3188