Language: C++

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
#include <utility>
inline int getInt() { int s; scanf("%d", &s); return s; }

#define SIZE 10
typedef array<array<int, SIZE>, SIZE> Table;

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

void fillColor(Table &state, int from, int to) {
	if (from==to) return;
	vector<pair<int,int>> stack;
	stack.emplace_back(1,1);
	while (!stack.empty()) {
		int x,y; tie(x, y) = stack.back();
		stack.pop_back();
		REP(i, 4) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (state[ny][nx] == from) {
				stack.emplace_back(nx,ny);
				state[ny][nx] = to;
			}
		}
	}
}

int countColor(Table & state) {
	vector<pair<int, int>> stack;
	stack.emplace_back(1, 1);
	int cnt = 0;
	while (!stack.empty()) {
		int x,y; tie(x, y) = stack.back();
		stack.pop_back();

		REP(i, 4) {
			int nx = x + dx[i];
			int ny = y + dy[i];
			if (state[ny][nx] == g_c) {
				stack.emplace_back(nx, ny);
				cnt++;
				state[ny][nx] = -1;
			}
		}
	}
	return cnt;
}

int recursion(int depth, Table state) {
	if (depth > 5) return countColor(state);
	
	int cnt = 1;
	REP(i, 6) {
		if(depth==5 && i != g_c) continue;
		Table ntable = state;
		ntable[1][1] = i;
		fillColor(ntable, state[1][1], i);
		cnt = max(recursion(depth+1, ntable),cnt);
	}
	return cnt;
}

int main() {
	while (true) {
		ll h,w;
		cin >> h >> w 
		if (h == 0) break;
		
		g_c = getInt() - 1;
		Table table;
		REP(y, SIZE) REP(x, SIZE) table[y][x] = -1;
		REP(y, h) REP(x, w) table[y + 1][x + 1] = getInt()-1;
		cout << recursion(1, table) << endl;
	}
}

Compiler output:
a.cc:10:9: error: 'array' does not name a type
   10 | typedef array<array<int, SIZE>, SIZE> Table;
      |         ^~~~~
a.cc:16:6: error: variable or field 'fillColor' declared void
   16 | void fillColor(Table &state, int from, int to) {
      |      ^~~~~~~~~
a.cc:16:16: error: 'Table' was not declared in this scope; did you mean 'mutable'?
   16 | void fillColor(Table &state, int from, int to) {
      |                ^~~~~
      |                mutable
a.cc:16:23: error: 'state' was not declared in this scope; did you mean 'static'?
   16 | void fillColor(Table &state, int from, int to) {
      |                       ^~~~~
      |                       static
a.cc:16:30: error: expected primary-expression before 'int'
   16 | void fillColor(Table &state, int from, int to) {
      |                              ^~~
a.cc:16:40: error: expected primary-expression before 'int'
   16 | void fillColor(Table &state, int from, int to) {
      |                                        ^~~
a.cc:34:16: error: 'Table' was not declared in this scope; did you mean 'mutable'?
   34 | int countColor(Table & state) {
      |                ^~~~~
      |                mutable
a.cc:34:24: error: 'state' was not declared in this scope; did you mean 'static'?
   34 | int countColor(Table & state) {
      |                        ^~~~~
      |                        static
a.cc:55:26: error: 'Table' has not been declared
   55 | int recursion(int depth, Table state) {
      |                          ^~~~~
a.cc: In function 'int recursion(int, int)':
a.cc:56:41: error: 'countColor' cannot be used as a function
   56 |         if (depth > 5) return countColor(state);
      |                               ~~~~~~~~~~^~~~~~~
a.cc:59:13: error: 'i' was not declared in this scope
   59 |         REP(i, 6) {
      |             ^
a.cc:59:9: error: 'REP' was not declared in this scope
   59 |         REP(i, 6) {
      |         ^~~
a.cc: In function 'int main()':
a.cc:71:17: error: 'll' was not declared in this scope
   71 |                 ll h,w;
      |                 ^~
a.cc:72:17: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
   72 |                 cin >> h >> w
      |                 ^~~
      |                 std::cin
In file included from a.cc:2:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:72:24: error: 'h' was not declared in this scope
   72 |                 cin >> h >> w
      |                        ^
a.cc:72:29: error: 'w' was not declared in this scope
   72 |                 cin >> h >> w
      |                             ^
a.cc:76:17: error: 'Table' was not declared in this scope; did you mean 'mutable'?
   76 |                 Table table;
      |                 ^~~~~
      |                 mutable
a.cc:77:21: error: 'y' was not declared in this scope; did you mean 'dy'?
   77 |                 REP(y, SIZE) REP(x, SIZE) table[y][x] = -1;
      |                     ^
      |                     dy
a.cc:77:17: error: 'REP' was not declared in this scope
   77 |                 REP(y, SIZE) REP(x, SIZE) table[y][x] = -1;
      |                 ^~~
a.cc:79:17: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   79 |                 cout << recursion(1, table) << endl;
      |                 ^~~~
      |                 std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:79:38: error: 'table' was not declared in this scope; did you mean 'mutable'?
   79 |                 cout << recursion(1, table) << endl;
      |                                      ^~~~~
      |                                      mutable
a.cc:79:48: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   79 |                 cout << recursion(1, table) << endl;
      |                                                ^~~~
      |                                                std::endl
In file included from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~