#include <bits/stdc++.h>

using namespace std;

using i64 = int64_t;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;

template <typename T = i64>
struct Range{
    struct iterator{
        T value;
        const T step, last;
        const T& operator*(){return value;}
        iterator(T value, T step, T last) :
            value(step < static_cast<T>(0) ? max(last, value) : min(last, value)),
            step(step),
            last(last)
        {
        }
        iterator operator++(){value = step < static_cast<T>(0) ? max(value + step, last) : min(value + step, last); return *this;}
        bool operator!=(const iterator& x){return value != x.value;}
    };
    const T start, last, step;

    Range(const T start, const T last, const T step = static_cast<T>(1)) :
        start(start),
        last(last),
        step(step)
    {
    }

    Range(const T last) :
        start(0),
        last(last),
        step(1)
    {
    }

    iterator begin(){return iterator(start, step, last);}
    iterator end(){return iterator(last, step, last);}
};

template <typename F>
struct FixPoint{
    const F _f;
    FixPoint(F&& f) : _f(forward<F>(f)){}

    template<typename... Types>
    decltype(auto) operator()(Types&&... args) const{
        return _f(*this, forward<Types>(args)...);
    }
};

template <typename F>
static decltype(auto) makeRec(F&& f){
    return FixPoint<F>(forward<F>(f));
}

template <typename T, T Value = T()>
vector<T> makeVector(size_t x){
    return vector<T>(x, T(Value));
}

template <typename T, T Value = T(), typename... Types>
auto makeVector(size_t x, Types... args){
    return vector<decltype(makeVector<T, Value>(args...))>(x, makeVector<T, Value>(args...));
}

template <typename T = i64>
bool chmax(T& a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template <typename T = i64>
bool chmin(T& a, T b){
    if(a > b){
        a = b;
        return true;
    }
    return false;
}

#define dump(x) fprintf(stderr, "line =%4d, name =%7s , ", __LINE__, #x); clog << "value = " << x << endl;

#define vecdump(x) fprintf(stderr, "line =%4d, name =%7s\n", __LINE__, #x); _dump_macro(x);

void _dump(int, string& x){
    clog << x << endl;
}

template <typename T>
void _dump(bool, T& x){
    clog << x << " ";
}

template <typename T, typename U = typename T::iterator>
void _dump(int, T& x){

    for(auto& elm : x)
        _dump(0, elm);

    clog << endl;
}

template <typename T>
void _dump_macro(T& x){
    _dump(0, x);
}

void _input(int, string& x){
    cin >> x;
}

template <typename T>
void _input(bool, T& x){
    cin >> x;
}

template <typename T, typename U = typename T::iterator>
void _input(int, T& x){

    for(auto& elm : x)
        _input(0, elm);
}

template <typename T>
void input_single(T& x){
    _input(0, x);
}

auto input(){}

template <typename T, typename... Types>
void input(T& value, Types&&... args){
    input_single(value);
    input(forward<Types>(args)...);
};

void _pararell_input(size_t){}

template <typename T, typename... Types>
void _pararell_input(size_t index, T& value, Types&&... args){
    input(value[index]);
    _pararell_input(index, forward<Types>(args)...);
}

template <typename... Types>
void pararell_input(size_t count, Types&&... args){
    for(const auto& i : Range<>(count))
        _pararell_input(i, forward<Types>(args)...);
}


bool solve(){

    int h, w, c;
    input(h, w, c);
    if(!h)
        return false;

    vector<vector<int>> p(h, vector<int>(w));
    input(p);

    for(auto& x : p)
        for(auto& y : x)
            --y;

    vector<int> t(1, 1);
    for(int i = 0; i < 4; ++i)
        t.emplace_back(t.back() * 6);

    int ans = 0;

    vector<int> dx{1, 0, -1, 0};
    vector<int> dy{0, 1, 0, -1};

    for(int i = 0; i < t.back(); ++i){

        auto v = p;
        vector<vector<int>> l;
        function<int(int,int,int,int)> f = [&](int x, int y, int from, int to){
            int cnt = 1;
            for(int d = 0; d < 4; ++d){
                int xx = x + dx[d];
                int yy = y + dy[d];
                if(0 <= xx && xx < h && 0 <= yy && yy < w && (v[xx][yy] == from || v[xx][yy] == to) && !l[xx][yy]){
                    l[xx][yy] = 1;
                    if(v[xx][yy] == from){
                        v[xx][yy] = to;
                        cnt += f(xx, yy, from, to);
                    }else
                        ++cnt;
                }
            }
            return cnt;
        };
        function<int(int,int,int)> g = [&](int x, int y, int cc){
            int cnt = 1;
            for(int d = 0; d < 4; ++d){
                int xx = x + dx[d];
                int yy = y + dy[d];
                if(0 <= xx && xx < h && 0 <= yy && yy < w && v[xx][yy] == cc &&  !l[xx][yy]){
                    l[xx][yy] = 1;
                    cnt += g(xx, yy, cc);
                }
            }
            return cnt;
        };
        int i_ = i;
        for(int j = 0; j < 5; ++j){
            int va = (j == 4 ? c - 1 : i_ % 6);
            int fr = v[0][0];
            l.assign(h, vector<int>(w, 0));
            v[0][0] = va;
            l[0][0] = 1;
            f(0, 0, fr, va);
            i_ /= 6;
        }
        l.assign(h, vector<int>(w, 0));
        l[0][0] = 1;
        chmax(ans, g(0, 0, c - 1));
    }

    cout << ans << endl;

    return true;
}

signed main(){

    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    while(solve());

}



Measured memory_bytes: 3164