#include<iostream>
#include<queue>
using namespace std;

int x,y;

struct grid{
  char g[11][11];
  int count;
};

void DFS(grid& gr,char firstcell,char changecolor,int posx,int posy){

  //if(posx < 0 || posx >= x  || posy < 0 || posy >= y || gr.g[posx][posy] != firstcell){
  if(gr.g[posx][posy] != firstcell){
    return;
  }

  gr.g[posx][posy] = changecolor;

  DFS(gr,firstcell,changecolor,posx+1,posy);
  DFS(gr,firstcell,changecolor,posx-1,posy);
  DFS(gr,firstcell,changecolor,posx,posy+1);
  DFS(gr,firstcell,changecolor,posx,posy-1);
}

int main(){

  while(1){

    cin >> x >> y;

    if(x==0 && y==0){
      break;
    }

    grid start;

    for(int i=0; i<y; i++){
      for(int j=0; j<x; j++){
        cin >> start.g[j][i];
      }
    }

    char firstcell = start.g[0][0];

    bool flag = true;

    for(int i=0; i<y; i++){
      for(int j=0; j<x; j++){
        if(start.g[j][i]!=firstcell){
          flag = false;
          break;
        }
      }
      if(!flag){
        break;
      }
    }
    if(flag){
      cout << 0 << endl;
      continue;
    }

    start.count = 0;

    queue<grid> q;

    q.push(start);

    while(1){

      grid BeforeGrid = q.front();
      q.pop();

      BeforeGrid.count++;

      grid tmp = BeforeGrid;

      firstcell  = tmp.g[0][0];

      if(firstcell != 'R'){
          DFS(tmp,firstcell,'R',0,0);

          flag = true;
          for(int i=0; i<y; i++){
            for(int j=0; j<x; j++){
              if(tmp.g[j][i]!='R'){
                flag = false;
                break;
              }
            }
            if(!flag){
              break;
            }
          }
          if(flag){
            cout << tmp.count << endl;
            break;
          }

          q.push(tmp);
          tmp = BeforeGrid;
      }

      if(firstcell != 'G'){
          DFS(tmp,firstcell,'G',0,0);

          flag = true;
          for(int i=0; i<y; i++){
            for(int j=0; j<x; j++){
              if(tmp.g[j][i]!='G'){
                flag = false;
                break;
              }
            }
            if(!flag){
              break;
            }
          }
          if(flag){
            cout << tmp.count << endl;
            break;
          }

          q.push(tmp);
          tmp = BeforeGrid;
      }

      if(firstcell != 'B'){
          DFS(tmp,firstcell,'B',0,0);

          flag = true;
          for(int i=0; i<y; i++){
            for(int j=0; j<x; j++){
              if(tmp.g[j][i]!='B'){
                flag = false;
                break;
              }
            }
            if(!flag){
              break;
            }
          }
          if(flag){
            cout << tmp.count << endl;
            break;
          }

          q.push(tmp);
          tmp = BeforeGrid;
      }

    }






  }


    return 0;
}