#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif


using namespace std;

int h, w, c;

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

int saiki(vector<vector<int>>data, int n,int c)
{
	if (!n)
	{
		vector<pair<int, int>>p;
		int memo = data[1][1];
		int count = 0;
		p.push_back(pair < int, int > {1, 1});
		while (p.size())
		{
			int x_=p.back().first,y_=p.back().second;
			p.pop_back();
			count++;
			for (size_t i = 0; i < 4; i++)
			{
				if (memo == data[x_ + dx[i]][y_ + dy[i]])
				{
					data[x_ + dx[i]][y_ + dy[i]] = 100;
					//data[x_ + dx[i]][y_ + dy[i]] = 100;
					p.push_back(pair < int, int > {x_ + dx[i], y_ + dy[i]});
				}
			}
		}
		return count;
	}
	int retur = 0;

	if (n == 1)
	{
		int i = c;
		if (data[1][1] != i)
		{
			vector<vector<int>>data2 = data;
			vector<pair<int, int>>p;
			int memo = data[1][1];
			data2[1][1] = i;
			p.push_back(pair < int, int > {1, 1});
			while (p.size())
			{
				int x_ = p.back().first, y_ = p.back().second;
				p.pop_back();
				for (size_t j = 0; j < 4; j++)
				{
					if (memo == data2[x_ + dx[j]][y_ + dy[j]])
					{
						data2[x_ + dx[j]][y_ + dy[j]] = i;
						p.push_back(pair < int, int > {x_ + dx[j], y_ + dy[j]});
					}
				}
			}
			retur = max(retur, saiki(data2, n - 1,c));
			data2.clear();
		}
		return retur;
	} else
	{
		for (size_t i = 1; i <= 6; i++)
		{
			if (data[1][1] != i)
			{
				vector<vector<int>>data2 = data;
				vector<pair<int, int>>p;
				int memo = data[1][1];
				data2[1][1] = i;
				p.push_back(pair < int, int > {1, 1});
				while (p.size())
				{
					int x_ = p.back().first, y_ = p.back().second;
					p.pop_back();
					for (size_t j = 0; j < 4; j++)
					{
						if (memo == data2[x_ + dx[j]][y_ + dy[j]])
						{
							data2[x_ + dx[j]][y_ + dy[j]] = i;
							p.push_back(pair < int, int > {x_ + dx[j], y_ + dy[j]});
						}
					}
				}
				retur = max(retur, saiki(data2, n - 1,c));
				data2.clear();
			}
		}
		return retur;
	}
}

int main()
{
	while (cin >> h >> w >> c, h || w || c)
	{
		vector<vector<int>>data;
		data.resize(h+2);
		data[0].resize(w + 2);
		data[h + 1].resize(w + 2);
		for (size_t i = 1; i <= h; i++)
		{
			data[i].resize(w + 2);
			for (size_t j = 1; j <= w; j++)
			{
				cin >> data[i][j];
			}
		}
		int ans = saiki(data, 5, c);
		cout << (ans==1?1:ans-1) << endl;
	}
}

Measured memory_bytes: 1236