#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define rep(i, n) for(int i = 0; i< (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for(iter(c) i = (c).begin(); i != c.end(); ++i)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

int x, y;
int grid[10][10];

int dr[] = {1, 0, -1, 0};
int dc[] = {0, -1, 0, 1};

bool goal(){
  int a = grid[0][0];
  rep(i, y)rep(j, x) if(a != grid[i][j]) return false;
  return true;
}

inline bool inside(int r, int c){
  return 0 <= r && r < y && 0 <= c && c < x;
}

void change_color(int before, int after, int r, int c, bool change[10][10]){
  grid[r][c] = after;
  change[r][c] = true;
  rep(i, 4){
    int r2 = r + dr[i];
    int c2 = c + dc[i];
    if(inside(r2, c2) && grid[r2][c2] == before){
      change_color(before, after, r2, c2, change);
    }
  }
}

void undo_change(int before, bool change[10][10]){
  rep(i, y)rep(j, x){
    if(change[i][j]) {
      change[i][j] = false;
      grid[i][j] = before;
    }
  }
}


bool dfs(int depth, int limit){
  bool change[10][10];
  memset(change, false, sizeof(change));
  if(depth > limit) return false;
  /*cout << depth << " " << limit << " " << endl;
    rep(i, y) rep(j, x) cout << grid[i][j] << (j == x - 1 ? '\n' : ' ');*/
  if(goal()) return true;
  
  int before = grid[0][0];
  rep(after, 3){
    if(after == before) continue;
    change_color(before, after, 0, 0, change);
    if(dfs(depth + 1, limit)) return true;
    undo_change(before, change);
  }
  return false;
}

int main(){
  char ch;
  while(cin >> x >> y){
    if(x == 0 && y == 0) break;
    rep(i, y)rep(j, x){
      cin >> ch;
      if(ch == 'R') grid[i][j] = 0;
      if(ch == 'G') grid[i][j] = 1;
      if(ch == 'B') grid[i][j] = 2;
    }
    int depth = 0;
    while(!dfs(0, depth)){
      depth++;
    }
    cout << depth << endl;
  }
  return 0;
}