#include <iostream>
using namespace std;

const int MAX_MATRIX = 8;
typedef enum __COLOR{YELLOW=1, PEACH, RED, PURPLE, GREEN, BLUE, NONE_COLOR} COLOR;
typedef enum __CONNECT{NO_CONNECT, UP, DOWN = UP<<1,
	LEFT= DOWN<<1, RIGHT = LEFT<<1} CONNECT;

class PANEL{
public:
	COLOR col;
	int connect;
	bool check;
	PANEL(){
		col=NONE_COLOR;
		connect=NO_CONNECT;
		check = false;
	}
};

int h, w;
COLOR dest;

void input(PANEL **p);
void output(PANEL **p);
void connectCheck(PANEL ** p);
int colChange(PANEL ** chp, COLOR chcol);
int colChange(PANEL ** chp, COLOR chcol, int row, int column);
int search(PANEL **p);

int main()
{
	int c;
	PANEL **p;
	while(true){
		cin.clear();
		cin >> h >> w >> c;//???????????????????????\???
		if(h==w && w==c && c==0){
			break;
		}
		//??????????????????
		p = new PANEL *[h];
		for(int i=0; i<h; i++){
			p[i] = new PANEL[w];
		}
		//??????????????????
		dest = static_cast<COLOR>(c);
		input(p);			//???????????\???
		connectCheck(p);	//????????????????????????????¢????
		cout << search(p) << endl;
		for(int i=0; i<h; i++) delete[] p[i];	//??????????????????
		delete[] p;								//
	}
	return 0;
}

void input(PANEL **p)
{
	int panel[MAX_MATRIX][MAX_MATRIX];
	for(int i=0; i<h; i++){
		switch(w){//????????°????????£?????\???????????´
		case 1:
			cin >> panel[i][0];
			break;
		case 2:
			cin >> panel[i][0] >> panel[i][1];
			break;
		case 3:
			cin >> panel[i][0] >> panel[i][1] >> panel[i][2];
			break;
		case 4:
			cin >> panel[i][0] >> panel[i][1] >> panel[i][2] >> panel[i][3];
			break;
		case 5:
			cin >> panel[i][0] >> panel[i][1] >> panel[i][2] >> panel[i][3]
			>> panel[i][4];
			break;
		case 6:
			cin >> panel[i][0] >> panel[i][1] >> panel[i][2] >> panel[i][3]
			>> panel[i][4] >> panel[i][5];
			break;
		case 7:
			cin >> panel[i][0] >> panel[i][1] >> panel[i][2] >> panel[i][3]
			>> panel[i][4] >> panel[i][5] >> panel[i][6];
			break;
		case 8:
			cin >> panel[i][0] >> panel[i][1] >> panel[i][2] >> panel[i][3]
			>> panel[i][4] >> panel[i][5] >> panel[i][6] >> panel[i][7];
			break;
		default:
			break;
		}
	}
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			COLOR col = static_cast<COLOR>(panel[i][j]);
			if(0 <= col && col < NONE_COLOR)
				p[i][j].col = col;//????????????
			else
				p[i][j].col = NONE_COLOR;
		}
	}
}

void output(PANEL **p)
{
	bool *downside = new bool[w],
		*rightside = new bool[w];
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			downside[j] = rightside[j] = false;
			if(p[i][j].connect == NO_CONNECT) continue;
			if((p[i][j].connect & DOWN)!=0) downside[j] = true;
			if((p[i][j].connect & RIGHT)!=0) rightside[j] = true;
		}
		for(int j=0; j<w; j++){
			cout << p[i][j].col;
			if(rightside[j]){
				cout << "-";
			}else{
				cout << " ";
			}
		}
		cout << endl;
		for(int j=0; j<w; j++){
			if(downside[j]){
				cout << "| ";
			} else {
				cout << "  ";
			}
		}
		cout << endl;
	}
	delete[] downside;
	delete[] rightside;
}

void connectCheck(PANEL **p)
{
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			p[i][j].connect = NO_CONNECT;
			//????????°????????????
			if(i!=0){
				//???????????????????????´???????????£?????????
				if((p[i-1][j].connect & DOWN)!=0){
					p[i][j].connect |= UP;//???????????°????????????
				}
			}
			if(i<h-1){
				if(p[i+1][j].col == p[i][j].col){//?????????????????¨????????????
					p[i][j].connect |= DOWN;//???????????°????????????
				}
			}
			if(j!=0){
				//???????????????????????´???????????£?????????
				if((p[i][j-1].connect & RIGHT)!=0){
					p[i][j].connect |= LEFT;//???????????°????????????
				}
			}
			if(j<w-1){
				if(p[i][j+1].col == p[i][j].col){//?????????????????¨????????????
					p[i][j].connect |= RIGHT;//???????????°????????????
				}
			}
		}
	}
}

int colChange(PANEL **chp, COLOR chcol)
{
	chp[0][0].col = chcol;
	//?????????????????????????????¨??????????????£??????????????´???????????????
	if(chp[0][0].connect == NO_CONNECT) return 1;
	int changenum=colChange(chp, chcol, 0, 0);
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			chp[i][j].check = false;//????§????
		}
	}
	return changenum;
}
int colChange(PANEL **chp, COLOR chcol, int row, int column)
{
	if(chp[row][column].check) return 0;
	int chpanels = 1;
	chp[row][column].check = true;
	chp[row][column].col = chcol;
	if(chp[row][column].connect == NO_CONNECT) return chpanels;
	if((chp[row][column].connect & UP) != 0)
		chpanels += colChange(chp, chcol, row-1, column);
	if((chp[row][column].connect & DOWN) != 0)
		chpanels += colChange(chp, chcol, row+1, column);
	if((chp[row][column].connect & LEFT) != 0)
		chpanels += colChange(chp, chcol, row, column-1);
	if((chp[row][column].connect & RIGHT) != 0)
		chpanels += colChange(chp, chcol, row, column+1);
	return chpanels;
}

int search(PANEL **p)
{
	int ans=0;
	PANEL **chp1,
		**chp2,
		**chp3,
		**chp4;
	for(int i=YELLOW; i<NONE_COLOR; i++){
		chp1 = new PANEL*[h];
		for(int j=0; j<h; j++){
			chp1[j]=new PANEL[w];
			for(int k=0; k<w; k++) chp1[j][k] = p[j][k];
		}
		colChange(chp1, (COLOR)i);//1??????
		connectCheck(chp1);

		for(int j=YELLOW; j<NONE_COLOR; j++){
			if(i==j) continue;

			chp2 = new PANEL*[h];
			for(int k=0; k<h; k++){
				chp2[k]=new PANEL[w];
				for(int l=0; l<w; l++) chp2[k][l] = chp1[k][l];
			}
			colChange(chp2, (COLOR)j);//2??????
			connectCheck(chp2);

			for(int k=YELLOW; k<NONE_COLOR; k++){
				if(k==j) continue;

				chp3 = new PANEL*[h];
				for(int l=0; l<h; l++){
					chp3[l]=new PANEL[w];
					for(int m=0; m<w; m++) chp3[l][m] = chp2[l][m];
				}
				colChange(chp3, (COLOR)k);//3??????
				connectCheck(chp3);

				for(int l=YELLOW; l<NONE_COLOR; l++){
					if(l==k) continue;
					if(l==dest) continue;
					chp4 = new PANEL*[h];
					for(int m=0; m<h; m++){
						chp4[m]=new PANEL[w];
						for(int n=0; n<w; n++) chp4[m][n] = chp3[m][n];
					}
					colChange(chp4, (COLOR)l);//4??????
					connectCheck(chp4);

					colChange(chp4, dest);//5??????
					connectCheck(chp4);

					int pnum = colChange(chp4, dest);//??°????¢????
					if(ans < pnum) ans=pnum;

					for(int m=0; m<h; m++){
						delete[] chp4[m];
					}
					delete[] chp4;

				}

				for(int l=0; l<h; l++){
					delete[] chp3[l];
				}
				delete[] chp3;

			}

			for(int k=0; k<h; k++){
				delete[] chp2[k];
			}
			delete[] chp2;

		}

		for(int j=0; j<h; j++){
			delete[] chp1[j];
		}
		delete[] chp1;

	}
	return ans;
}

Measured memory_bytes: 1216