#include <bits/stdc++.h>
using namespace std;

int x,y;

void DFS(array<array<char,11>,11>& v,char base,char c,int xp,int yp){

    if(xp < 0 || yp < 0 || xp >= x || yp >= y || v[xp][yp]!=base){
        return;
    }

    v[xp][yp] = c;

    DFS(v,base,c,xp+1,yp);
    DFS(v,base,c,xp,yp+1);
    DFS(v,base,c,xp-1,yp);
    DFS(v,base,c,xp,yp-1);

}

int main() {

    while(1){

        //scanf(" %d %d",&x,&y);

        cin >> x >> y;

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

        array<array<char,11>,11> v;

        //v.cs.resize(static_cast<unsigned>(x));

        for(int i=0;i<y;i++){
            for(int j=0;j<x;j++){
                //char c;
                /*scanf(" %c",&c);
                v.cs[j][i] = c;*/
                cin >> v[j][i];
                //v.cs[j][i] = c;
            }
        }

        bool flag = true;

        for(int i=0;i<y;i++){
            for(int j=0;j<x;j++){
                if(v[0][0] != v[j][i]){
                    flag = false;
                    break;
                }
            }
        }

        if(flag){
            cout << 0 << endl;
            continue;
        }

        queue<pair<array<array<char,11>,11>,int > > q;

        q.push(make_pair(v,0));

        while(!q.empty()){

            int count = q.front().second;

            v = q.front().first;

            /*for(int i=0;i<y;i++){
                for(int j=0;j<x;j++){
                    cout << v.cs[i][j] << ' ';
                }cout << endl;
            }cout << endl;*/

            q.pop();

            count++;

            array<array<char,11>,11> tmp;

            //tmp.resize(static_cast<unsigned>(x));

            /*for(int i=0;i<y;i++){
                tmp[i] += v.cs[i];
            }*/

            tmp = v;

            if(v[0][0] != 'R'){

                DFS(v,v[0][0],'R',0,0);

                flag = true;

                for(int i=0;i<y;i++){
                    for(int j=0;j<x;j++){
                        if('R' != v[j][i]){
                            flag = false;
                            break;
                        }
                    }
                }

                if(flag){
                    cout << count << endl;
                    break;
                }

                q.push(make_pair(v,count));
                /*for(int i=0;i<y;i++){
                    v[i] = tmp[i];
                }*/
                v = tmp;
            }

            if(v[0][0]  != 'G'){

                DFS(v,v[0][0],'G',0,0);

                flag = true;

                for(int i=0;i<y;i++){
                    for(int j=0;j<x;j++){
                        if('G' != v[j][i]){
                            flag = false;
                            break;
                        }
                    }
                }

                if(flag){
                    cout << count << endl;
                    break;
                }

                q.push(make_pair(v,count));
                /*for(int i=0;i<y;i++){
                    v.cs[i] = tmp[i];
                }*/
                v = tmp;
            }

            if(v[0][0]  != 'B'){

                //char base = v.cs[0][0];
                DFS(v,v[0][0],'B',0,0);

                flag = true;

                for(int i=0;i<y;i++){
                    for(int j=0;j<x;j++){
                        if('B' != v[j][i]){
                            flag = false;
                            break;
                        }
                    }
                }

                if(flag){
                    cout << count << endl;
                    break;
                }

                q.push(make_pair(v,count));
            }

        }



    }

    return 0;
}

Measured memory_bytes: 18696