#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

int H,W,C,diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};


struct Info{
	int table[8][8],count;
};

struct Data{
	Data(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	int row,col;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(){

	Info first;
	first.count = 0;

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++)scanf("%d",&first.table[i][k]);
	}

	queue<Info> Q;

	Q.push(first);

	queue<Data> D;

	int maximum = 1,self_color,num,next_row,next_col,new_color;

	bool tonari_color[7],check[8][8];
	Info work;

	while(!Q.empty()){

		if(Q.front().count == 5){

			num = 0;

			D.push(Data(0,0));

			for(int i = 0; i < H; i++){
				for(int k = 0; k < W; k++)check[i][k] = false;
			}

			check[0][0] = true;

			while(!D.empty()){

				Q.front().table[D.front().row][D.front().col] = 9;
				num++;

				for(int i = 0; i < 4; i++){
					next_row = D.front().row + diff_row[i];
					next_col = D.front().col + diff_col[i];

					if(rangeCheck(next_row,next_col) == true && Q.front().table[next_row][next_col] == C && check[next_row][next_col] == false){
						check[next_row][next_col] = true;
						D.push(Data(next_row,next_col));
					}
				}
				D.pop();
			}

			maximum = max(maximum,num);

			Q.pop();
		}else{

			Q.front().count++;

			if(Q.front().count <= 4){

				self_color = Q.front().table[0][0];
				for(int i = 1; i <= 6; i++){
					tonari_color[i] = false;
				}

				for(int i = 0; i < H; i++){
					for(int k = 0; k < W; k++){
						work.table[i][k] = Q.front().table[i][k];
					}
				}

				D.push(Data(0,0));
				check[0][0] = true;

				while(!D.empty()){
					work.table[D.front().row][D.front().col] = 9;

					for(int i = 0; i < 4; i++){
						next_row = D.front().row + diff_row[i];
						next_col = D.front().col + diff_col[i];

						if(rangeCheck(next_row,next_col)){
							if(work.table[next_row][next_col] != self_color && work.table[next_row][next_col] != 9){
								tonari_color[work.table[next_row][next_col]] = true;
							}else{
								if(work.table[next_row][next_col] == self_color){
									D.push(Data(next_row,next_col));
								}
							}
						}
					}
					D.pop();
				}

				num = 0;
				for(int i = 1; i <= 6; i++){
					if(tonari_color[i] == true){
						num++;
					}
				}

				if(num == 0){
					maximum = H*W;
					break;
				}

				for(int i = 1; i <= 6; i++){
					if(i == self_color || tonari_color[i] == false)continue;

					Info next;
					next.count = Q.front().count;
					for(int a = 0; a < H; a++){
						for(int b = 0; b < W; b++){
							next.table[a][b] = Q.front().table[a][b];
							check[a][b] = false;
						}
					}

					new_color = i;

					check[0][0] = true;

					D.push(Data(0,0));

					while(!D.empty()){
						next.table[D.front().row][D.front().col] = new_color;

						for(int a = 0; a < 4; a++){
							next_row = D.front().row + diff_row[a];
							next_col = D.front().col + diff_col[a];

							if(rangeCheck(next_row,next_col) == true && check[next_row][next_col] == false && next.table[next_row][next_col] == self_color){
								check[next_row][next_col] = true;
								D.push(Data(next_row,next_col));
							}
						}
						D.pop();
					}

					Q.push(next);
				}

				Q.pop();

			}else{

				self_color = Q.front().table[0][0];

				D.push(Data(0,0));

				for(int i = 0; i < H; i++){
					for(int k = 0; k < W; k++)check[i][k] = false;
				}

				check[0][0] = true;

				while(!D.empty()){
					Q.front().table[D.front().row][D.front().col] = C;

					for(int i = 0; i < 4; i++){
						next_row = D.front().row + diff_row[i];
						next_col = D.front().col + diff_col[i];

						if(rangeCheck(next_row,next_col) == true && check[next_row][next_col] == false && Q.front().table[next_row][next_col] == self_color){
							check[next_row][next_col] = true;
							D.push(Data(next_row,next_col));
						}
					}
					D.pop();
				}

				Q.push(Q.front());
			}
		}
	}

	printf("%d\n",maximum);
}

int main(){

	while(true){
		scanf("%d %d %d",&H,&W,&C);
		if(H == 0 && W == 0 && C == 0)break;

		func();
	}

	return 0;
}

Measured memory_bytes: 2764