#include<iostream>
#include<queue>
using namespace std;
 
struct grid{
  char g[11][11];
  int count;
};
 
void DFS(grid& gr,char f,char c,int x,int y){
 
  if(gr.g[x][y] != f){
    return;
  }
 
  gr.g[x][y] = c;
 
  DFS(gr,f,c,x+1,y);
  DFS(gr,f,c,x-1,y);
  DFS(gr,f,c,x,y+1);
  DFS(gr,f,c,x,y-1);
}
 
int main(){
    int x,y;
    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 fc = 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]!=fc){
                    flag = false;
                    break;
                }
            }
            if(!flag){
                break;
            }
        }
        if(flag){
            cout << 0 << endl;
            continue;
        }
 
        start.count = 0;
 
        queue<grid> q;
 
        q.push(start);
 
        while(1){
 
            grid bg = q.front();
            q.pop();
 
            bg.count++;
 
            grid tmp = bg;
 
            fc  = tmp.g[0][0];
 
            if(fc != 'R'){
                DFS(tmp,fc,'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 = bg;
            }
 
            if(fc != 'G'){
                DFS(tmp,fc,'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 = bg;
            }
 
            if(fc != 'B'){
                DFS(tmp,fc,'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 = bg;
            }
        }
    }
    return 0;
}


Measured memory_bytes: 18604