Language: C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;
struct state{
	char f[11][11];
	int c,l;
	state(){}
	state(int c,int l):c(c),l(l){};
};
char in[11][11];
state ne;
bool used[11][11];
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
int n,m;
int res;

void dfs(int next,int prev,int x,int y){
	if(x==0 && y==0){
		memset(used,false,sizeof(used));
	}
	ne.f[x][y]=next;
	used[x][y]=true;
	for(int i=0;i<4;i++){
		int nx=x+dx[i],ny=y+dy[i];
		if(nx>=0 && nx<n && ny>=0 && ny<m){
			if(!used[nx][ny]){
				used[nx][ny]=true;
				if(ne.f[nx][ny]==next){
				}
				if(ne.f[nx][ny]==prev){
					dfs(next,prev,nx,ny);
				}	
			}
		}
	}
}

void cnt(int prev,int x,int y){
	if(x==0 && y==0){
		ne.l=0;
		memset(used,false,sizeof(used));
	}
	ne.l++;
	used[x][y]=true;
	for(int i=0;i<4;i++){
		int nx=x+dx[i],ny=y+dy[i];
		if(nx>=0 && nx<n && ny>=0 && ny<m){
			if(!used[nx][ny]){
				used[nx][ny]=true;
				if(ne.f[nx][ny]==prev){
					cnt(prev,nx,ny);
				}	
			}
		}
	}
}

void bfs(state init){
	queue<state> que;
	map<state, bool> V;
	res=50;
	V[init]= true;
	que.push(init);
	while(que.size()){
		state p=que.front();
		que.pop();
		ne.c=p.c+1;
		for(int i=0;i<=2;i++){
			memcpy(ne.f,p.f,sizeof(ne.f));
			dfs(i,ne.f[0][0],0,0);
			cnt(ne.f[0][0],0,0);
			//if(!V[ne.f]){
				if(ne.l==n*m)res=min(res,p.c+1);
				else if(ne.l>p.l)que.push(ne);
			//	V[ne.f]=true;
			//}
		}
	}
}

int main(void){
	while(1){
		res=100;
		scanf("%d%d\n",&n,&m);
		if(n==0 && m==0)break;
		state init=state(0,1);
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cin >> in[j][i];
				if(in[j][i]=='R')init.f[j][i]=0;
				if(in[j][i]=='G')init.f[j][i]=1;
				if(in[j][i]=='B')init.f[j][i]=2;
			}
		}
		memcpy(ne.f,init.f,sizeof(ne.f));
		cnt(ne.f[0][0],0,0);
		if(ne.l==n*m)printf("0\n");
		else{
			bfs(init);
			printf("%d\n",res);
		}
	}
	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_function.h: In instantiation of 'constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = state]':
/usr/include/c++/14/bits/stl_map.h:511:32:   required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = state; _Tp = bool; _Compare = std::less<state>; _Alloc = std::allocator<std::pair<const state, bool> >; mapped_type = bool; key_type = state]'
  511 |         if (__i == end() || key_comp()(__k, (*__i).first))
      |                             ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
a.cc:68:8:   required from here
   68 |         V[init]= true;
      |               ^
/usr/include/c++/14/bits/stl_function.h:405:20: error: no match for 'operator<' (operand types are 'const state' and 'const state')
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/string:48:
/usr/include/c++/14/bits/stl_iterator.h:448:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  448 |     operator<(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:448:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const state' is not derived from 'const std::reverse_iterator<_Iterator>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  493 |     operator<(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const state' is not derived from 'const std::reverse_iterator<_Iterator>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1694:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1694 |     operator<(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1694:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const state' is not derived from 'const std::move_iterator<_IteratorL>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1760:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1760 |     operator<(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1760:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const state' is not derived from 'const std::move_iterator<_IteratorL>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~