#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <algorithm>

using namespace std;

typedef pair<int, int> P;

int main() {

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

	int h, w, c;
	while (cin >> h >> w >> c) {
		if (h == 0 && w == 0 && c == 0) {
			break;
		}
		vector< vector<int> > mp(h, vector<int>(w));
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				cin >> mp[i][j];
			}
		}

		set<long long int> st;
		map<long long int, vector< vector<int> > > mpp;
		mpp[0] = mp;
		vector<int> num(5, 0);
		for (long long int i = 1; i <= 7 * 7 * 7 * 7 * 7; i++) {
			num[0]++;
			for (int j = 0; j < 5; j++) {
				if (num[j] > 6) {
					num[j + 1]++;
					num[j] = 1;
				}
			}
			long long int nn = 0;
			int cc = 0;
			for (int j = 0; j < 5; j++) {
				nn *= 10;
				nn += num[j];
				if (num[j] > 0) {
					cc = j;
				}
			}
			long long int cc2 = 0;
			if (cc == 0) {
				cc2 = 10000;
			}
			else if (cc == 1) {
				cc2 = 1000;
			}
			else if (cc == 2) {
				cc2 = 100;
			}
			else if (cc == 3) {
				cc2 = 10;
			}
			else {
				cc2 = 1;
			}
			//cout << nn << ' ' << i << endl;
			//cout << nn << ' ' << nn - num[cc] * cc2 << endl;
			vector< vector<int> > mp2 = mpp[nn - num[cc] * cc2];
			//cout << mp2.size() << endl;
			int ss = mp2[0][0];
			//cout << ss << endl;
			queue<P> q;
			mp2[0][0] = num[cc];

			if (ss != num[cc]) {
				q.push(P(0, 0));
			}

			//cout << q.size() << endl;

			while (!q.empty()) {
				P pp = q.front();
				q.pop();
				for (int j = 0; j < 4; j++) {
					if (0 <= pp.first + dx[j] && pp.first + dx[j] < w && 0 <= pp.second + dy[j] && pp.second + dy[j] < h) {
						if (mp2[pp.second + dy[j]][pp.first + dx[j]] == ss) {
							mp2[pp.second + dy[j]][pp.first + dx[j]] = num[cc];
							q.push(P(pp.first + dx[j], pp.second + dy[j]));
						}
					}
				}
			}


			mpp[nn] = mp2;
			if (nn == 66666) {
				break;
			}
		}

		//cout << -1 << endl;

		int ans = 0;
		num = vector<int>(5, 0);
		for (long long int i = 1; i <= 7 * 7 * 7 * 7 * 7; i++) {
			num[0]++;
			for (int j = 0; j < 5; j++) {
				if (num[j] > 6) {
					num[j + 1]++;
					num[j] = 1;
				}
			}
			long long int nn = 0;
			for (int j = 0; j < 5; j++) {
				nn *= 10;
				nn += num[j];
			}
			//cout << i << ' ' << nn << endl;
			int ans2 = 0;
			vector< vector<int> > mp3 = mpp[nn];
			queue<P> q;
			if (mp3[0][0] == c) {
				q.push(P(0, 0));
				ans2++;
				mp3[0][0] = 0;
			}
			while (!q.empty()) {
				P pp = q.front();
				q.pop();
				for (int j = 0; j < 4; j++) {
					if (0 <= pp.first + dx[j] && pp.first + dx[j] < w && 0 <= pp.second + dy[j] && pp.second + dy[j] < h) {
						if (mp3[pp.second + dy[j]][pp.first + dx[j]] == c) {
							mp3[pp.second + dy[j]][pp.first + dx[j]] = 0;
							q.push(P(pp.first + dx[j], pp.second + dy[j]));
							ans2++;
						}
					}
				}
			}
			/*if (ans2 == 11) {
				cout << nn << endl;
				for (int j = 0; j < 3; j++) {
					for (int k = 0; k < 5; k++) {
						cout << mp3[j][k];
					}
					cout << endl;
				}
			}*/
			ans = max(ans, ans2);
			if (nn == 66666) {
				break;
			}
		}
		cout << ans << endl;
	}

	
	return 0;
}


Measured memory_bytes: 10232