#include <cstdio>
#include <stack>
#include <vector>

using namespace std;

int main()
{
    while (true) {
        int h;
        int w;
        int c;
        scanf("%d %d %d", &h, &w, &c);
        if (h == 0 && w == 0 && c == 0) {
            break;
        }

        vector<vector<int>> cp;
        cp.resize(h);
        for (auto i = 0; i < h; ++i) {
            for (auto j = 0; j < w; ++j) {
                int pp;
                scanf("%d", &pp);
                cp[i].push_back(pp);
            }
        }
        const auto p = move(cp);
        // ??£??\????????§?¨??????????
        auto getAdjacency = [w, h](int i, int j) {
            vector<pair<int, int>> r;
            if (i - 1 >= 0) {
                r.push_back(make_pair(i - 1, j));
            }
            if (i + 1 < h) {
                r.push_back(make_pair(i + 1, j));
            }
            if (j - 1 >= 0) {
                r.push_back(make_pair(i, j - 1));
            }
            if (j + 1 < w) {
                r.push_back(make_pair(i, j + 1));
            }
            return r;
        };

        // ????????????????????????????????§?????????????????????????????°???????????????
        auto f = [getAdjacency, &p, w, h](const vector<int>& colors) {
            auto pp = p;
            for (size_t n = 0; n < colors.size(); ++n) {
                const auto ccc = colors[n];

                // ??????????????????????????¨????????¢?´¢
                stack<pair<int, int>> s;
                s.push(make_pair(0, 0));
                vector<vector<bool>> v;
                v.resize(h);
                for (auto&& r : v) {
                    r.resize(w);
                }
                auto init = pp[0][0];
                while (!s.empty()) {
                    auto t = s.top();
                    s.pop();
                    // ??°?????§????????????????????????
                    v[t.first][t.second] = true;
                    pp[t.first][t.second] = ccc;
                    auto r = getAdjacency(t.first, t.second);
                    for (auto&& i : r) {
                        // ??£??\???????????????????????????????????¨?????????????????£????????¢?´¢????±?
                        if (!v[i.first][i.second] &&
                            init == pp[i.first][i.second]) {
                            s.push(i);
                        }
                    }
                }
            }

            // ???????????????????????????????????°??????
            stack<pair<int, int>> s;
            s.push(make_pair(0, 0));
            vector<vector<bool>> v;
            v.resize(h);
            for (auto&& r : v) {
                r.resize(w);
            }
            auto count = 0;
            while (!s.empty()) {
                auto t = s.top();
                s.pop();
                auto r = getAdjacency(t.first, t.second);
                for (auto&& i : r) {
                    if (!v[i.first][i.second] &&
                        pp[i.first][i.second] == pp[0][0]) {
                        // ??£????????£??????????????????
                        v[i.first][i.second] = true;
                        ++count;
                        s.push(i);
                    }
                }
            }
            return count == 0 ? 1 : count;
        };

        vector<int> colors;
        colors.resize(5);
        auto max = 0;
        for (auto a = 1; a <= 6; ++a) {
            for (auto b = 1; b <= 6; ++b) {
                for (auto e = 1; e <= 6; ++e) {
                    for (auto d = 1; d <= 6; ++d) {
                        colors[0] = a;
                        colors[1] = b;
                        colors[2] = e;
                        colors[3] = d;
                        colors[4] = c;
                        auto r = f(colors);
                        if (r > max) {
                            max = r;
                        }
                    }
                }
            }
        }
        printf("%d\n", max);
    }
}

Measured memory_bytes: 2700