 #include <bits/stdc++.h>
using namespace std;
 #define REP(i,x,n) for(int i = x ; i < (int)(n) ; i++)
#define rep(i,n) REP(i,0,n)
 static const int dx[]={1,-1,0,0};
static const int dy[]={0,0,1,-1};
static const char color[]={'R','G','B'};
 int n,m;
int changeV;
char G[10][10];
bool used[10][10];

struct State{
  int push;
  char grid[10][10];
  State(){}
  State(int p, char g[][10]){
    push = p;
    rep(i,n) rep(j,m) grid[i][j] = g[i][j];
  }
};

bool check(){
  char tmp = G[0][0];
  rep(i,n){
    rep(j,m){
      if(G[i][j]!=tmp) return false;
    }
  }
  return true;
}

void dfs(int x, int y, int idx){
  used[x][y] = true;
  rep(k,4){
    int nx = x + dx[k];   int ny = y + dy[k];
    if(0<=nx&&nx<n && 0<=ny&&ny<m && G[x][y]==G[nx][ny] && !used[nx][ny]){
      dfs(nx,ny,idx);
    }
  }
  G[x][y] = color[idx];
}

int solve(){
  queue<State> q;
  q.push(State(0,G));
    while(!q.empty()){
    State st = q.front();
    q.pop();
    rep(k,3){
      if(st.grid[0][0]!=color[k]){
        memcpy(G,st.grid,sizeof(G));
        memset(used,false,sizeof(used));
        dfs(0,0,k);
        if(check()) return st.push + 1;
        if(G[0][0]==G[0][1] || G[0][0]==G[1][0]){
          q.push(State(st.push + 1, G));
        }
      }
    }
  }
  return -1;
}

int main(){
  while(cin >> m >> n && (n|m)){
    rep(i,n){
      rep(j,m){
        cin >> G[i][j];
      }
    }

    int ans = 0;
    if(!check()) ans = solve();
    cout << ans << endl;
  }
  return 0;
}