#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
int term(State& begin);
int expression(State& begin) {
	int now_value = 0;
	if (*begin == '(') {
		begin++;
		int d1 = expression(begin);
		int d2;
		if (*begin == '+') {
			begin++;
			d2 = expression(begin);
			now_value = max(d1, d2);
		}
		else if(*begin == '*'){
			begin++;
			d2 = expression(begin);
			now_value = min(d1, d2);
		}
		begin++;
	}
	else {
		now_value = term(begin);
	}
	return now_value;
}
int term(State& begin) {
	int now_value = 0;
	if (*begin == '-') {
		begin++;
		now_value = 2 - expression(begin);
	}
	else if (*begin >= '0' && *begin <= '9') {
		now_value = *begin - '0';
		begin++;
	}
	return now_value;
}
int main() {
	while (true) {
		int h, w, c;
		cin >> h >> w >> c;
		if (h == 0) return 0;
		int base[9][9] = {};
		int grid[9][9] = {};
		REP(i, h) {
			REP(q, w) {
				cin >> grid[i][q];
			}
		}
		long long ans = 0;
		for (int i = 0; i < pow(6, 4); ++i) {
			REP(q, h) {
				REP(j, w) {
					base[q][j] = grid[q][j];
				}
			}
			int coloring[5] = {};
			int geko = i;
			REP(q, 4) {
				coloring[q] = geko % 6 + 1;
				geko /= 6;
			}
			coloring[4] = c;
			REP(q, 5) {
				int geko = base[0][0];
				if (geko == coloring[q]) continue;
				base[0][0] = coloring[q];
				queue<pair<int, int>> next;
				next.push(make_pair(0, 0));
				while (next.empty() == false) {
					pair<int, int> now = next.front();
					next.pop();
					int xe[4] = { 1,-1,0,0 };
					int ye[4] = { 0,0,1,-1 };
					REP(j, 4) {
						int now_x = now.first + xe[j];
						int now_y = now.second + ye[j];
						if (now_x >= 0 && now_x < h && now_y >= 0 && now_y < w) {
							if (base[now_x][now_y] == geko) {
								base[now_x][now_y] = coloring[q];
								next.push(make_pair(now_x, now_y));
							}
						}
					}
				}
			}
			long long now_ans = 1;
			queue<pair<int, int>> next;
			next.push(make_pair(0, 0));
			base[0][0] = 0;
			while (next.empty() == false) {
				pair<int, int> now = next.front();
				next.pop();
				int xe[4] = { 1,-1,0,0 };
				int ye[4] = { 0,0,1,-1 };
				REP(j, 4) {
					int now_x = now.first + xe[j];
					int now_y = now.second + ye[j];
					if (now_x >= 0 && now_x < h && now_y >= 0 && now_y < w) {
						if (base[now_x][now_y] == c) {
							base[now_x][now_y] = 0;
							now_ans++;
							next.push(make_pair(now_x, now_y));
						}
					}
				}
			}
			if (ans < now_ans) {
				ans = max(ans, now_ans);
			}
		}
		cout << ans << endl;
	}
}


Measured memory_bytes: 3092