Language: C++

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

struct Grid{
char g[11][11];
int cnt;
};

void draw(Grid& grid,char f,char c,int x,int y){

if(grid.g[x][y] != f){
  return;
}

grid.g[x][y] = c;

draw(grid,f,c,x-1,y);
draw(grid,f,c,x+1,y);
draw(grid,f,c,x,y-1);
draw(grid,f,c,x,y+1);
}

int main(){
  int x,y;
  while(1){

      cin >> x >> y;

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

      Grid st;

      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.cnt = 0;

      queue<Grid> q;

      q.push(start);

      while(1){

          Grid bg = q.front();
          q.pop();

          bg.cnt++;

          Grid tmp = bg;

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

          if(fc != 'R'){
              draw(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.cnt << endl;
                  break;
              }

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

          if(fc != 'G'){
              draw(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.cnt << endl;
                  break;
              }

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

          if(fc != 'B'){
              draw(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.cnt << endl;
                  break;
              }

              q.push(tmp);
              tmp = bg;
          }
      }
  }
  return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:38:22: error: 'start' was not declared in this scope
   38 |               cin >> start.g[j][i];
      |                      ^~~~~
a.cc:42:17: error: 'start' was not declared in this scope
   42 |       char fc = start.g[0][0];
      |                 ^~~~~