//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>


#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LLINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair< long long int, long long  int> P;
typedef string::const_iterator State;

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


int main(void) {
	
	while (1) {
		int h, w, c;
		int maxi = 0;
		int point;
		int first;
		int p[9][9] = {};
		int save[9][9] = {};
		queue< pair<int, int> > que;//y,x 
		pair<int, int> now;
		int x, y;

		cin >> h >> w >> c;
		if (h == 0) {
			break;
		}

		FOR(i, 1, h) {
			FOR(j, 1, w) {
				cin >> p[i][j];
				save[i][j] = p[i][j];
			}
		}
		
		first = p[1][1];

		FOR(i, 1, 6) {
			if (i != first) {
				FOR(j, 1, 6) {
					if (j != i) {
						FOR(k, 1, 6) {
							if (k != j) {
								FOR(l, 1, 6) {
									if (l != c&&l!=k) {

										//cout << i << " " << j << " " << k << " " << l << " " << c << endl;

										FOR(m, 1, h) {
											FOR(n, 1, w) {
												p[m][n] = save[m][n];
											}
										}
										

										//cout << "a" << endl;
										que.push(make_pair(1,1));
										while (!que.empty()) {
											now = que.front();
											y = now.first;
											x = now.second;
											que.pop();

											FOR(m, 0, 3) {
												if (y + dy[m] >= 1 && y + dy[m] <= h && x + dx[m] >= 1 && x + dx[m] <= w) {
													if (p[y + dy[m]][x + dx[m]] == first) {
														que.push(make_pair(y + dy[m], x + dx[m]));
													}
												}
											}
											p[y][x] = i;

										}

										//cout << "b" << endl;
										que.push(make_pair(1, 1));
										while (!que.empty()) {
											now = que.front();
											y = now.first;
											x = now.second;
											que.pop();

											FOR(m, 0, 3) {
												if (y + dy[m] >= 1 && y + dy[m] <= h && x + dx[m] >= 1 && x + dx[m] <= w) {
													if (p[y + dy[m]][x + dx[m]] == i) {
														que.push(make_pair(y + dy[m], x + dx[m]));
													}
												}
											}
											p[y][x] = j;

										}

										//cout << "c" << endl;
										que.push(make_pair(1, 1));
										while (!que.empty()) {
											now = que.front();
											y = now.first;
											x = now.second;
											que.pop();

											FOR(m, 0, 3) {
												if (y + dy[m] >= 1 && y + dy[m] <= h && x + dx[m] >= 1 && x + dx[m] <= w) {
													if (p[y + dy[m]][x + dx[m]] == j) {
														que.push(make_pair(y + dy[m], x + dx[m]));
													}
												}
											}
											p[y][x] = k;

										}

										//cout << "d" << endl;
										que.push(make_pair(1, 1));
										while (!que.empty()) {
											now = que.front();
											y = now.first;
											x = now.second;
											que.pop();

											FOR(m, 0, 3) {
												if (y + dy[m] >= 1 && y + dy[m] <= h && x + dx[m] >= 1 && x + dx[m] <= w) {
													if (p[y + dy[m]][x + dx[m]] == k) {
														que.push(make_pair(y + dy[m], x + dx[m]));
													}
												}
											}
											p[y][x] = l;

										}

										//cout << "e" << endl;
										que.push(make_pair(1, 1));
										while (!que.empty()) {
											now = que.front();
											y = now.first;
											x = now.second;
											que.pop();

											FOR(m, 0, 3) {
												if (y + dy[m] >= 1 && y + dy[m] <= h && x + dx[m] >= 1 && x + dx[m] <= w) {
													if (p[y + dy[m]][x + dx[m]] == l) {
														que.push(make_pair(y + dy[m], x + dx[m]));
													}
												}
											}
											p[y][x] = c;

										}


										/*FOR(m, 1, h) {
											FOR(n, 1, w) {
												cout << p[m][n] << " ";
											}
											cout << endl;
										}*/

										point = 0;
										//cout << "f" << endl;
										que.push(make_pair(1, 1));
										while (!que.empty()) {
											now = que.front();
											y = now.first;
											x = now.second;
											que.pop();

											//cout << y << " " << x << endl;
											point++;

											FOR(m, 0, 3) {
												if (y + dy[m] >= 1 && y + dy[m] <= h && x + dx[m] >= 1 && x + dx[m] <= w) {
													if (p[y + dy[m]][x + dx[m]] == c) {
														que.push(make_pair(y + dy[m], x + dx[m]));
														p[y + dy[m]][x + dx[m]] = -1;
													}
												}
											}
											p[y][x] = -1;

											/*FOR(n, 1, h) {
												FOR(o, 1, w) {
													cout << p[n][o] << " ";
												}
												cout << endl;
											}*/

										}
										//cout << maxi << endl;
										//cout << endl;


										//cout << "g" << endl;
										if (maxi < point) {
											maxi = point;
										}
										
									}
								}
							}
							
						}
					}
					
				}
			}
			
		}
		
		cout << maxi << endl;
	}

	return 0;
}


Measured memory_bytes: 3180