#include <bits/stdc++.h>
using namespace std;

// type {{{
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
// }}}


// macro {{{
#define REP(i,n) for (int i=0; i<(n); ++i)
#define RREP(i,n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i,a,n) for (int i=(a); i<(n); ++i)
#define RFOR(i,a,n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define all(x) begin(x),end(x)
// }}}


// debug {{{
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "[";
    REP (i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    return os << "(" << p.first << " " << p.second << ")";
}
// }}}


// chmax, chmin {{{
template<class T>
bool chmax(T& a, const T& b) {
    if (a < b) { a = b; return true; }
    return false;
}
template<class T>
bool chmin(T& a, const T& b) {
    if (b < a) { a = b; return true; }
    return false;
}
// }}}


// constants {{{
#define inf(T) (numeric_limits<T>::max() / 2)
const ll MOD = 1e9+7;
const ld EPS = 1e-9;
const int dy[] = {0, 1, 0, -1},
          dx[] = {-1, 0, 1, 0};
// }}}



int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    for (;;) {
        int H, W, C;
        cin >> H >> W >> C;
        --C;
        if (H == 0) break;

        vvi p(H, vi(W));
        REP (i, H) {
            REP (j, W) {
                cin >> p[i][j];
                --p[i][j];
            }
        }

        const int MAX = 5, COLORS = 6;

        int ans = 0;

        vector<int> a(MAX+1);
        while (!a[MAX]) {

            vvi board = p;
            int ret = 0;

            vvi used(H, vi(W));
            used[0][0] = 1;

            for (int color : a) {
                queue<pair<int,int>> que;
                REP (i, H) {
                    REP (j, W) {
                        if (used[i][j]) {
                            que.emplace(i, j);
                        }
                    }
                }

                while (!que.empty()) {
                    int i, j; tie(i, j) = que.front(); que.pop();
                    REP (k, 4) {
                        int y = i + dy[k], x = j + dx[k];
                        if (y < 0 or H <= y or x < 0 or W <= x) continue;

                        if (board[y][x] == board[i][j] and !used[y][x]) {
                            used[y][x] = 1;
                            que.emplace(y, x);
                        }
                    }
                }

                REP (i, H) {
                    REP (j, W) {
                        if (used[i][j]) {
                            board[i][j] = color;
                        }
                    }
                }
            }

            if (a[MAX-1] == C) {
                int cnt = 0;
                REP (i, H) {
                    REP (j, W) {
                        cnt += used[i][j];
                    }
                }
                chmax(ans, cnt);
            }

            for (int i = 0; ++a[i] == COLORS; ++i) a[i] = 0;
        }

        cout << ans << endl;
    }

    return 0;
}



Measured memory_bytes: 3140