#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

template<typename T>
using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >;

static const int INTINF = (2147483647 >> 1); // 10^9 + 5000
static const ll LLINF = (9223372036854775807 >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;

namespace Kunitaka{
    template<
        typename TYPE,
        std::size_t SIZE
    >
    std::size_t array_length(const TYPE (&array)[SIZE])
    {
        return SIZE;
    }

    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
}
using namespace Kunitaka;
namespace Printer{
    void br(){
        cout << endl;
    }

    void loop_cnt(int i){
        cout << "i = " << i << endl;
    }

    void loop_cnt(int i, int j){
        cout << "i = " << i << ", " << "j = " << j << endl;
    }

    template<class T>
    void print(T x){
        cout << x << endl;
    }

    template<class T, class S>
    void print(T x, S y){
        cout << x << "," << y << endl;
    }

    template<class T, class S, class U>
    void print(T x, S y, U z){
        cout << x << "," << y << "," << z << endl;
    }

    template<class T, class S>
    void print(pair<T, S> p){
        cout << p.first << ", " << p.second << endl;
    }

    template<
        typename TYPE,
        std::size_t SIZE
    >
    void print(const TYPE (&array)[SIZE]){
        int lim = array_length(array);
        for(int i=0; i<lim; i++){
            if(i) cout << " ";
            cout << array[i];
        }
        cout << endl;
    }

    template<class S>
    void print(vector<S> v){
        for(int i=0; i<v.size(); i++){
            if(i) cout << " ";
            cout<< v[i];
        }
        cout << endl;
    }

    template<class S>
    void print(vector<vector<S> > vv){
        for(int i=0; i<vv.size(); i++){
            for(int j=0; j<vv[i].size(); j++){
                if(j) cout << " ";
                cout << vv[i][j];
            }
            cout << endl;
        }
    }

    void yesno(bool x){
        if(x) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
};
using namespace Printer;
int h, w, c;
int p[10][10];

struct point{
    int x, y;
};

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

ll rec(int color){
    int origin_c = p[0][0];
    queue<point> q;
    int used[10][10];
    memset(used, 0, sizeof(used));
    used[0][0] = 1;
    q.push({0, 0});
    int k = 0;
    while(!q.empty()){
        point now = q.front(); q.pop();
        for(int i=0; i<4; i++){
            point np = {now.x, now.y};
            np.x += dx[i];
            np.y += dy[i];
            if(np.x < 0 || w <= np.x) continue;
            if(np.y < 0 || h <= np.y) continue;
            if(used[np.x][np.y] == 1) continue;
            used[np.x][np.y] = 1;
            if(p[np.y][np.x] == origin_c) q.push({np.x, np.y});
        }
        p[now.y][now.x] = color;
        k++;
    }

    return k;
}

ll calc(string out){
    ll cnt = 0;
    out += '0';
    for(char i : out){
        int color = i - '0';

        cnt = rec(color);
    }
    return cnt;
}

ll make_c(string out){
    if(out.size() == 4){
        int cnt = 0;
        int bef[10][10];
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                bef[i][j] = p[i][j];
            }
        }
        cnt = calc(out + to_string(c));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                p[i][j] = bef[i][j];
            }
        }
        return cnt;
    }

    ll res = 0;
    for(int c=1; c<7; c++){
        res = max(res, make_c(out + to_string(c)));
    }
    return res;
}

int main(int argc, const char * argv[]) {
    //提出時、消す----//
    //--------------//


    // input
    cout << fixed << setprecision(10);
    while(1){
        cin >> h >> w >> c;
        if(!h && !w && !c) break;
        memset(p, 0, sizeof(p));
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                cin >> p[i][j];
            }
        }

        string tmp = "";

        int cnt = make_c(tmp);
        
        cout << cnt << endl;
    }

    
    
    return 0;
}



Measured memory_bytes: 3232