#include<iostream>
#include<fstream>

using namespace std;

#define fin cin

int panel[8][8];
int h, w, last;

bool come[8][8];

void come_init() {
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 8; j++) {
			come[i][j] = false;
		}
	}
}

//????????????????????????????????£??\????????????????¨????
int getCount(int map[][8], int y, int x) {

	//??????????????¨NullPo??????
	if (x < 0 || y < 0 || x >= 8 || y >= 8)return 0;
	
	if (map[y][x] != map[0][0] || come[y][x])return 0;

	come[y][x] = true;

	int count = 1;
	count += getCount(map, y + 1, x);
	count += getCount(map, y, x + 1);
	count += getCount(map, y - 1, x);
	count += getCount(map, y, x - 1);

	return count;
}

void change(int map[][8], bool (*f_change)[8], int y, int x) {
	if (x < 0 || y < 0 || x >= 8 || y >= 8)return;

	if (map[y][x] != map[0][0] || f_change[y][x])return;

	f_change[y][x] = true;

	change(map, f_change, y + 1, x);
	change(map, f_change, y, x + 1);
	change(map, f_change, y - 1, x);
	change(map, f_change, y, x - 1);

	//cout << f_change[0][1];

}


//search(????????????????????????????????????????????????)
int search(int map[][8], int level, int color) {
	static int max = 0;
	if (level == 0)max = 0;

	
	//??£??\??????????????????color????????????

	bool f_change[8][8] = {};
	//f_change[0][0] = true;

	change(map, f_change, 0, 0);

	/*
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 8; j++) {

			if (map[i][j] == map[0][0]) {
				if (
					(i < 7 && map[i][j] == map[i + 1][j] && f_change[i + 1][j]) ||
					(j < 7 && map[i][j] == map[i][j + 1] && f_change[i][j + 1]) ||
					(i > 0 && map[i][j] == map[i - 1][j] && f_change[i - 1][j]) ||
					(j > 0 && map[i][j] == map[i][j - 1] && f_change[i][j - 1])
					)
					f_change[i][j] = true;
			}

		}
	}
	*/

	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 8; j++) {
			if(f_change[i][j])map[i][j] = color;
		}
	}
	level++;

	if (level < 4) {

		for (int i = 1; i <= 6; i++) {

			int map_tmp[8][8];

			//copy
			for (int y = 0; y < 8; y++) {
				for (int x = 0; x < 8; x++) {
					map_tmp[y][x] = map[y][x];
				}
			}

			int tmp = search(map_tmp, level, i);
			if (max < tmp)max = tmp;

		}
	}
	//4??????????????????5??????
	else if (level == 4) {
		int map_tmp[8][8];

		//copy
		for (int y = 0; y < 8; y++) {
			for (int x = 0; x < 8; x++) {
				map_tmp[y][x] = map[y][x];
			}
		}

		int tmp = search(map_tmp, level, last);
		if (max < tmp)max = tmp;
	
	}
	//5??????????????????
	else if (level == 5) {
		come_init();
		int tmp = getCount(map, 0, 0);
		if (max < tmp) {
			max = tmp;

			if(0)
			{
				for (int y = 0; y < h; y++) {
					for (int x = 0; x < w; x++) {
						cout << map[y][x] << " ";
					}
					cout << endl;
				}
				cout << "   :   " << level << " " << color << endl;
			}

		}

	}

	return max;
}


int main() {
	//ifstream fin("in.txt");

	while (true) {
		fin >> h >> w >> last;
		if ((h | w) == 0)break;

		//initialize
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				panel[i][j] = 0;
			}
		}

		//input
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				fin >> panel[i][j];
			}
		}

		int max = 0;
		for (int i = 1; i <= 6; i++) {
			int panel_tmp[8][8];
			
			//copy
			for (int y = 0; y < 8; y++) {
				for (int x = 0; x < 8; x++) {
					panel_tmp[y][x] = panel[y][x];
				}
			}

			int tmp = search(panel_tmp, 0, i);
			if (max < tmp)max = tmp;
		}

		cout << max << endl;

	}


	return 0;
}

Measured memory_bytes: 1164