#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

int h, w, c;
int bo[10][10];

struct unionfind {
    pii par[10][10];
    int rank[10][10];

    void init(int h, int w) {
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                par[i][j] = pii(i, j);
                rank[i][j] = 0;
            }
        }
    }

    pii find(int x, int y) {
        if (par[x][y] == pii(x, y)) {
            return pii(x, y);
        } else {
            return par[x][y] = find(par[x][y].first, par[x][y].second);
        }
    }
    
    void unite(int x1, int y1, int x2, int y2) {
        pii p = find(x1, y1);
        pii q = find(x2, y2);
        x1 = p.first;
        y1 = p.second;
        x2 = q.first;
        y2 = q.second;
        if (x1 == x2 && y1 == y2) {
            return;
        }
        if (rank[x1][y1] < rank[x2][y2]) {
            par[x1][y1] = pii(x2, y2);
        } else {
            par[x2][y2] = pii(x1, y1);
            if (rank[x1][y1] == rank[x2][y2]) {
                rank[x1][y1]++;
            }
        }
    }

    bool same(int x1, int y1, int x2, int y2) {
        return find(x1, y1) == find(x2, y2);
    }

    int rankfind(int x, int y) {
        return rank[find(x, y).first][find(x, y).second];
    }
};

int ans;
unionfind u;

int panel[10][10];

void DFS(int n) {
    for (int l = 1; l <= 6; l++) {
        if (n == 5 && l != c) {
            continue;
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (panel[i][j] == -1 && bo[i][j] == l) {
                    for (int k = 0; k < 4; k++) {
                        int x = i + dx[k];
                        int y = j + dy[k];
                        if (x >= 0 && x < h && y >= 0 && y < w) {
                            if (panel[x][y] >= 0) {
                                panel[i][j] = n;
                                pii p = u.find(i, j);
                                panel[p.first][p.second] = n;
                            }
                        }
                    }
                }
            }
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (panel[i][j] == -1 && bo[i][j] == l) {
                    pii p = u.find(i, j);
                    if (panel[p.first][p.second] == n) {
                        panel[i][j] = n;
                    }
                }
            }
        }
        if (n == 5) {
            int num = 0;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (panel[i][j] >= 0) {
                        num++;
                    }
                }
            }
            if (num > ans) {
                ans = num;
            }
        } else {
            DFS(n + 1);
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (panel[i][j] == n) {
                    panel[i][j] = -1;
                }
            }
        }
    }
}

int main() {
    while (1) {
        cin >> h >> w >> c;
        if (h == 0) {
            break;
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> bo[i][j];
            }
        }
        u.init(h, w);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                for (int k = 0; k < 4; k++) {
                    int x = i + dx[k];
                    int y = j + dy[k];
                    if (x >= 0 && x < h && y >= 0 && y < w) {
                        if (bo[i][j] == bo[x][y]) {
                            u.unite(i, j, x, y);
                        }
                    }
                }
            }
        }
        for (int i = 0; i < h; i++) {
            fill(panel[i], panel[i] + w, -1);
        }
        panel[0][0] = 0;
        pii p = u.find(0, 0);
        panel[p.first][p.second] = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                pii q = u.find(i, j);
                if (panel[q.first][q.second] == 0) {
                    panel[i][j] = 0;
                }
            }
        }
        ans = 0;
        DFS(1);
        cout << ans << endl;
    }
}



Measured memory_bytes: 3128