
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <deque>
#include <complex>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORE(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,a) FOR(i,0,a)
using namespace std;
typedef complex<int> cl;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1},INF=1<<30;
const char color[]{'R','G','B'};
int x,y,W,H;
char maps[10][10]={};
bool draw(bool same[10][10]){
	bool searched[10][10]={};
	bool ans=false;
	searched[0][0]=1;
	queue<cl> qu;
	qu.push(cl(0,0));
	while(!qu.empty()){
		int w=qu.front().real(),h=qu.front().imag();
		qu.pop();
		REP(i,4){
		int nx=w+dx[i],ny=h+dy[i];
			if(nx<W&&ny<H&&nx>=0&&ny>=0)
				if((maps[ny][nx]==maps[0][0]||same[ny][nx])&& !searched[ny][nx]){
					if(!same[ny][nx])
					ans=true;
					same[ny][nx]=true;
					qu.push(cl(nx,ny));
					searched[ny][nx]=true;
				}
		}
	}
	return ans;
}
int search(bool chan[10][10],int minium=INF){
	char f=maps[0][0];
	bool ans=true;
	REP(i,y) REP(j,x) if(!chan[i][j]) ans=false;
	if(ans) return 0;
	if(minium==0)
	return 1;
	REP(i,3){
		if(color[i]==maps[0][0]) continue;
		bool same[10][10]={};
		REP(j,10) REP(k,10) same[j][k]=chan[j][k];
		maps[0][0]=color[i];
		if(draw(same))
		minium=min(minium,search(same,minium-1));
		maps[0][0]=f;
	}
	return minium+1;
}
int main() {
	// your code goes here
	while(cin >> x >> y && x){
	W=x;
	H=y;
	REP(i,y) REP(j,x) cin >> maps[i][j];
	bool chan[10][10]={};
	chan[0][0]=1;
	draw(chan);
	cout << search(chan) << endl;
	}
	return 0;
}