#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

int di[4] = {1, 0, -1, 0};
int dj[4] = {0, -1, 0, 1};

int x, y;

void dfs(int i, int j, char cur, char next, vector<vector<char>> &vec){
    
    vec[i][j] = next;
    for(int h = 0; h < 4; h++){
        int ni = i + di[h];
        int nj = j + dj[h];
        if(ni < 0 || y <= ni || nj < 0 || x <= nj) continue;

        if(vec[ni][nj] == cur) dfs(ni, nj, cur, next, vec);
    }
}



signed main(){
    
    while(1){
    cin >> x >> y;
    if(x == 0) break;
    vector<vector<char>> g(y, vector<char> (x));
    for(int i = 0; i < y; i++){
        for(int j = 0; j < x; j++){
            cin >> g[i][j];
       }
    }
    
    set<vector<vector<char>>> st;
    queue<pair<int, vector<vector<char>>>> que;
    que.push({0, g});
    st.insert(g);
    
    while(1){
        
        auto tmp = que.front();
        auto vec = tmp.second;
        int cost = tmp.first;
        que.pop();
            
        /*cout << endl;
        for(int i = 0; i < y; i++){
            for(int j = 0; j < x; j++){
                cout << vec[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
        */

        bool flag = true;
        char cur = vec[0][0];
        for(int i = 0; i < y; i++){
            for(int j = 0; j < x; j++){
                if(vec[i][j] != cur) flag = false;
            }
        }

        if(flag){
            cout << cost << endl;
            break;
        }
        
        if(cur != 'R'){
            auto vec2 = vec;
            dfs(0, 0, cur, 'R', vec2);

            if(st.find(vec2) == st.end()){
                que.push({cost + 1, vec2});
                st.insert(vec2);
            }
        }
        
        if(cur != 'B'){
            auto vec2 = vec;
            dfs(0, 0, cur, 'B', vec2);
            if(st.find(vec2) == st.end()){

                que.push({cost + 1, vec2});
                st.insert(vec2);
            }
        }
        
        if(cur != 'G'){
            auto vec2 = vec;
            dfs(0, 0, cur, 'G', vec2);
            if(st.find(vec2) == st.end()){

                que.push({cost + 1, vec2});
                st.insert(vec2);
            }
        }
        
    }
    }
    return 0;
}

