alexjercan/bugnet huggingface dataset:
Problem p00242 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
bool desc(pair<int, string> p1, pair<int, string> p2) {
  if (p1.first == p2.first)
    return p1.second < p2.second;
  return p1.first > p2.first;
}
int main() {
  int n;
  string str, s, k;
  bool f = 0;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    cin.ignore();
    map<string, int> m;
    vector<pair<int, string>> v;
    rep(i, n) {
      getline(cin, str);
      stringstream ss(str);
      while (!ss.eof()) {
        ss >> s;
        m[s]++;
      }
    }
    cin >> k;
    bool f = 0;
    auto it = m.lower_bound(k);
    if (it == m.end())
      f = 1;
    else {
      string u = it->first;
      while (1) {
        if (u[0] != k[0])
          break;
        v.push_back(make_pair(it->second, it->first));
        it++;
        if (it == m.end())
          break;
        u = it->first;
      }
    }
    if (f || v.empty())
      cout << "NA" << endl;
    else {
      sort(v.begin(), v.end(), desc);
      cout << v[0].second;
      rep2(i, 1, v.size()) {
        if (i == 5)
          break;
        cout << " " << v[i].second;
      }
      cout << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
bool desc(pair<int, string> p1, pair<int, string> p2) {
  if (p1.first == p2.first)
    return p1.second < p2.second;
  return p1.first > p2.first;
}
int main() {
  int n;
  string str, s, k;
  bool f = 0;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    cin.ignore();
    map<string, int> m;
    vector<pair<int, string>> v;
    rep(i, n) {
      getline(cin, str);
      stringstream ss(str);
      while (!ss.eof()) {
        ss >> s;
        m[s]++;
      }
    }
    cin >> k;
    auto it = m.lower_bound(k);
    string u = it->first;
    while (it != m.end()) {
      if (u[0] != k[0])
        break;
      v.push_back(make_pair(it->second, it->first));
      it++;
      u = it->first;
    }
    if (v.empty())
      cout << "NA" << endl;
    else {
      sort(v.begin(), v.end(), desc);
      cout << v[0].second;
      rep2(i, 1, v.size()) {
        if (i == 5)
          break;
        cout << " " << v[i].second;
      }
      cout << endl;
    }
  }
}
Problem p00242 (C++) — buggy submission, verdict: Time Limit Exceeded

//*
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

int main() {
  int N;
  char K;
  int i;
  std::string in_str;
  std::string dstr;
  std::cin >> N;
  std::cin.ignore();
  bool ef = false;
  while (N) {
    std::map<std::string, int> dmap;
    std::vector<std::pair<int, std::string>> vecs;
    std::stringstream ss;
    for (i = 0; i < N; i++) {
      std::getline(std::cin, in_str);
      ss << in_str << " ";
    }
    while (ss >> in_str) {
      if (!dmap.count(in_str)) {
        dmap.insert(std::map<std::string, int>::value_type(in_str, 0));
      } else {
        dmap[in_str]--;
      }
    }
    std::cin >> K;
    std::cin.ignore();
    std::map<std::string, int>::iterator it;
    for (it = dmap.begin(); it != dmap.end(); it++) {
      if ((*it).first[0] == K) {
        vecs.push_back(std::pair<int, std::string>((*it).second, (*it).first));
      }
    }
    if (vecs.empty()) {
      std::cout << "NA" << std::endl;
    } else {
      std::sort(vecs.begin(), vecs.end());
      std::cout << vecs[0].second;
      for (i = 1; i < 5 && i < vecs.size(); i++) {
        std::cout << " " << vecs[i].second;
      }
      std::cout << std::endl;
    }
    std::cin >> N;
    std::cin.ignore();
  }
}
//*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() {
  char K;
  std::string in_str;
  std::string dstr;
  std::getline(std::cin, in_str);
  bool ef = false;
  while (in_str[0] != '0') {
    std::cin >> K;
    std::stringstream ss;
    ss << in_str;
    std::vector<std::string> vecs;
    while (ss >> in_str) {
      vecs.push_back(in_str);
    }

    std::sort(vecs.begin(), vecs.end(), [](std::string x, std::string y) {
      int i;
      for (i = 0; i < x.length() && i < y.length(); i++) {
        if (x[i] < y[i]) {
          return true;
        } else if (x[i] > y[i]) {
          return false;
        }
      }
      if (x.length() == i) {
        return true;
      }
      return false;
    });
    int i;
    ef = false;
    for (i = 0; !ef && i < vecs.size(); i++) {
      if (vecs[i][0] == K) {
        std::cout << vecs[i];
        dstr = vecs[i];
        for (int j = i + 1; j < vecs.size() && j < i + 5 && vecs[j][0] == K;
             j++) {
          if (dstr != vecs[j]) {
            std::cout << " " << vecs[j];
            dstr = vecs[j];
          } else {
            i++;
          }
        }
        std::cout << std::endl;
        ef = true;
        break;
      }
    }
    if (!ef) {
      std::cout << "NA" << std::endl;
    }
    std::getline(std::cin, in_str);
    std::getline(std::cin, in_str);
  }
}
Problem p00242 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string str;
  int n;
  while (cin >> n && n) {
    map<string, int> data;
    vector<pair<int, string>> v;
    char c;
    bool flg = false;
    getchar();
    while (n--) {
      getline(cin, str);
      stringstream ss(str);
      while (ss >> str)
        data[str]++;
    }
    cin >> c;
    for (map<string, int>::iterator i = data.begin(), l = data.end(); i != l;
         i++) {
      if (c == i->first[0]) {
        v.push_back(make_pair(-i->second, i->first));
      }
    }
    sort(v.begin(), v.end());

    if (v.empty())
      cout << "NA" << endl;
    else {
      for (int i = 0, l = v.size(); i < l && i < 5; i++) {
        if (flg)
          cout << " ";
        else
          flg = true;
        cout << v[i].second;
      }
      cout << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  string str;
  int n, m;
  map<string, int> data;
  vector<pair<string, int>> v;
  while (cin >> n && n) {
    char c;
    bool ok = false, flg = false;
    getchar();
    while (n--) {
      getline(cin, str);
      stringstream ss(str);
      while (ss >> str)
        data[str]++;
    }
    for (map<string, int>::iterator i = data.begin(), l = data.end(); i != l;
         i++) {
      v.push_back(make_pair(i->first, i->second));
    }
    sort(v.begin(), v.end());

    cin >> c;
    for (int i = 0, l = v.size(); i < l; i++) {
      if (c == v[i].first[0]) {
        ok = true, m = i;
        break;
      }
    }
    if (!ok)
      cout << "NA" << endl;
    else {
      for (int i = m; c == v[i].first[0] && i < m + 5; i++, flg = true) {
        if (flg)
          cout << " ";
        cout << v[i].first;
      }
      cout << endl;
    }
    data.clear();
    v.clear();
  }
}
Problem p00243 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
  fld[y][x] = c;
  for (int d = 0; d < 4; d++) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
      continue;
    change(ny, nx, c, f);
  }
}

bool dfs(int t = 0) {
  bool ok = true;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      if (fld[0][0] != fld[i][j])
        ok = false;
    }
  if (ok)
    return true;
  if (t == lim)
    return false;

  int _fld[10][10];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      _fld[i][j] = fld[i][j];
    }

  bool ret = false;
  for (int k = 0; k < 3; k++)
    if (k != fld[0][0]) {
      change(0, 0, k, fld[0][0]);
      ret |= dfs(t + 1);
      for (int i = 0; i < H; i++)
        for (int j = 0; j < W; j++) {
          fld[i][j] = _fld[i][j];
        }
    }

  return ret;
}
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c;
        cin >> c;
        switch (c) {
        case 'R':
          fld[i][j] = 0;
          break;
        case 'G':
          fld[i][j] = 1;
          break;
        case 'B':
          fld[i][j] = 2;
          break;
        }
      }
    }

    for (lim = 0;; lim++)
      if (dfs())
        break;
    cout << lim << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
  fld[y][x] = c;
  bool update = false;
  for (int d = 0; d < 4; d++) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
      continue;
    change(ny, nx, c, f);
    update = true;
  }

  return update;
}

bool dfs(int t = 0) {
  bool ok = true;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      if (fld[0][0] != fld[i][j])
        ok = false;
    }
  if (ok)
    return true;
  if (t == lim)
    return false;

  int _fld[10][10];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      _fld[i][j] = fld[i][j];
    }

  bool ret = false;
  for (int k = 0; k < 3; k++)
    if (k != fld[0][0]) {
      if (change(0, 0, k, fld[0][0]))
        ret |= dfs(t + 1);
      for (int i = 0; i < H; i++)
        for (int j = 0; j < W; j++) {
          fld[i][j] = _fld[i][j];
        }
    }

  return ret;
}
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c;
        cin >> c;
        switch (c) {
        case 'R':
          fld[i][j] = 0;
          break;
        case 'G':
          fld[i][j] = 1;
          break;
        case 'B':
          fld[i][j] = 2;
          break;
        }
      }
    }

    for (lim = 0;; lim++)
      if (dfs())
        break;
    cout << lim << endl;
  }

  return 0;
}
Problem p00243 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <utility>
using namespace std;

typedef pair<string, int> P;
int W, H, N, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
string rgb = "RGB";

bool check(const string &s) {
  char c = s[0];
  for (int i = 0; i < W * H; i++)
    if (s[i] != c)
      return false;
  return true;
}

string f(char c, string s) {
  char a = s[0];
  N = 0;
  queue<int> q;
  q.push(0);
  q.push(0);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    if (s[y * W + x] == c)
      continue;
    s[y * W + x] = c;
    N++;
    for (int i = 0; i < 4; i++) {
      int tx = x + dx[i], ty = y + dy[i];
      if (tx >= 0 && tx < W && ty >= 0 && ty < H && s[ty * W + tx] == a) {
        q.push(tx);
        q.push(ty);
      }
    }
  }
  return s;
}

int main() {
loop:
  stringstream ss;
  string s;
  cin >> W >> H;
  if (!W)
    return 0;

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      string tmp;
      cin >> tmp;
      ss << tmp;
    }
  }
  s = ss.str();
  queue<P> q;
  q.push(make_pair(s, 0));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    char c = p.first[0];
    for (int i = 0; i < 3; i++) {
      if (c != rgb[i]) {
        q.push(make_pair(f(rgb[i], p.first), p.second + 1));
        if (N == W * H) {
          cout << p.second << endl;
          goto loop;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <utility>
using namespace std;

typedef pair<string, int> P;
int W, H;
string rgb = "RGB";

bool check(const string &s) {
  char c = s[0];
  for (int i = 0; i < W * H; i++)
    if (s[i] != c)
      return false;
  return true;
}

string f(char c, string s) {
  char a = s[0];
  queue<int> q;
  q.push(0);
  q.push(0);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    s[y * W + x] = c;

    if (y < H - 1 && s[(y + 1) * W + x] == a) {
      q.push(x);
      q.push(y + 1);
    }
    if (x < W - 1 && s[y * W + x + 1] == a) {
      q.push(x + 1);
      q.push(y);
    }
  }
  return s;
}

int main() {
loop:
  stringstream ss;
  string s;
  cin >> W >> H;
  if (!W)
    return 0;

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      string tmp;
      cin >> tmp;
      ss << tmp;
    }
  }
  s = ss.str();
  queue<P> q;
  q.push(make_pair(s, 0));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (check(p.first)) {
      cout << p.second << endl;
      goto loop;
    }
    char c = p.first[0];
    for (int i = 0; i < 3; i++)
      if (c != rgb[i])
        q.push(make_pair(f(rgb[i], p.first), p.second + 1));
  }
}
Problem p00243 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <assert.h>
#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair

int n, m;
char f[16][16];
int g[32][16][16];

bool completed(int (*g)[16]) {
  rep(i, n) rep(j, m) if (g[i][j] != g[0][0]) return false;
  return true;
}

void rec(int (*g)[16], int x, int y, int from, int to) {
  if (g[x][y] != from)
    return;
  g[x][y] = to;
  if (x > 0)
    rec(g, x - 1, y, from, to);
  if (x < n - 1)
    rec(g, x + 1, y, from, to);
  if (y > 0)
    rec(g, x, y - 1, from, to);
  if (y < m - 1)
    rec(g, x, y + 1, from, to);
}

bool iddfs(int dep, int cur) {
  if (dep == cur)
    return completed(g[cur]);
  rep(k, 3) if (k != g[cur][0][0]) {
    rep(i, n) rep(j, m) g[cur + 1][i][j] = g[cur][i][j];
    rec(g[cur + 1], 0, 0, g[cur][0][0], k);
    if (iddfs(dep, cur + 1))
      return true;
  }
  return false;
}

int solve() {
  rep(i, n) rep(j, m) {
    if (f[i][j] == 'R')
      g[0][i][j] = 0;
    if (f[i][j] == 'G')
      g[0][i][j] = 1;
    if (f[i][j] == 'B')
      g[0][i][j] = 2;
  }
  rep(i, 32) if (iddfs(i, 0)) return i;
  assert(false);
  return -1;
}

int main() {
  for (;;) {
    scanf("%d%d", &m, &n);
    if (n == 0 && m == 0)
      return 0;
    rep(i, n) rep(j, m) scanf(" %c", f[i] + j);
    printf("%d\\n", solve());
  }
}

Error: MLE

Fixed submission:

#include <queue>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp make_pair

int n, m;
char f[16][16];

bool completed(const vector<vector<int>> &g) {
  rep(i, n) rep(j, m) if (g[i][j] != g[0][0]) return false;
  return true;
}

void rec(vector<vector<int>> &g, int x, int y, int from, int to) {
  if (g[x][y] != from)
    return;
  g[x][y] = to;
  if (x > 0)
    rec(g, x - 1, y, from, to);
  if (x < n - 1)
    rec(g, x + 1, y, from, to);
  if (y > 0)
    rec(g, x, y - 1, from, to);
  if (y < m - 1)
    rec(g, x, y + 1, from, to);
}

vector<vector<int>> draw(const vector<vector<int>> &g, int to) {
  vector<vector<int>> t(g);
  rec(t, 0, 0, g[0][0], to);
  return t;
}

int solve() {
  vector<vector<int>> g(n, vector<int>(m, 0));
  rep(i, n) rep(j, m) {
    if (f[i][j] == 'G')
      g[i][j] = 1;
    if (f[i][j] == 'B')
      g[i][j] = 2;
  }
  queue<pair<int, vector<vector<int>>>> q;
  q.push(mp(0, g));
  while (!q.empty()) {
    const pair<int, vector<vector<int>>> vv = q.front();
    q.pop();
    if (completed(vv.second))
      return vv.first;
    rep(i, 3) if (i != vv.second[0][0]) {
      q.push(mp(vv.first + 1, draw(vv.second, i)));
    }
  }
  return -1;
}

int main() {
  for (;;) {
    scanf("%d%d", &m, &n);
    if (n == 0 && m == 0)
      return 0;
    rep(i, n) rep(j, m) scanf(" %c", f[i] + j);
    printf("%d\\n", solve());
  }
}
Problem p00243 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int x, y, an, fc, dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};

void fill(int (*d)[10], int c, int cr, int fx, int fy) {
  int i;
  for (i = 0; i < 4; i++) {
    if (fx + dx[i] >= 0 && fx + dx[i] < x && fy + dy[i] >= 0 &&
        fy + dy[i] < y) {
      if (d[fy + dy[i]][fx + dx[i]] == c) {
        fc++;
        d[fy + dy[i]][fx + dx[i]] = cr;
        fill(d, c, cr, fx + dx[i], fy + dy[i]);
      }
    }
  }
}

int ans(int (*s)[10], int cr, int g) {
  int i, j, k, d[10][10];
  for (i = 1; i < 3; i++) {
    fc = 1;
    for (j = 0; j < y; j++)
      for (k = 0; k < x; k++)
        d[j][k] = s[j][k];
    d[0][0] = (cr + i) % 3;
    fill(d, cr, (cr + i) % 3, 0, 0);
    if (fc == x * y) {
      an = g;
      return 1;
    }
    if (g < an)
      ans(d, (cr + i) % 3, g + 1);
  }
  return 0;
}

int main() {
  char c;
  int i, j, s[10][10];
  while (cin >> x >> y && x + y > 0) {
    for (i = 0; i < y; i++)
      for (j = 0; j < x; j++) {
        cin >> c;
        if (c == 'R')
          s[i][j] = 0;
        if (c == 'G')
          s[i][j] = 1;
        if (c == 'B')
          s[i][j] = 2;
      }
    an = 100;
    ans(s, s[0][0], 0);
    cout << an << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int x, y, an, fc, dx[4] = {1, 0, -1, 0}, dy[4] = {0, -1, 0, 1};

void fill(int (*d)[10], int c, int cr, int fx, int fy) {
  int i;
  for (i = 0; i < 4; i++) {
    if (fx + dx[i] >= 0 && fx + dx[i] < x && fy + dy[i] >= 0 &&
        fy + dy[i] < y) {
      if (d[fy + dy[i]][fx + dx[i]] == c) {
        fc++;
        d[fy + dy[i]][fx + dx[i]] = cr;
        fill(d, c, cr, fx + dx[i], fy + dy[i]);
      }
    }
  }
}

int ans(int (*s)[10], int cr, int g) {
  int i, j, k, d[10][10];
  for (i = 1; i < 3; i++) {
    fc = 1;
    for (j = 0; j < y; j++)
      for (k = 0; k < x; k++)
        d[j][k] = s[j][k];
    d[0][0] = (cr + i) % 3;
    fill(d, cr, (cr + i) % 3, 0, 0);
    if (fc == x * y) {
      an = g;
      return 1;
    }
    if (g < an)
      ans(d, (cr + i) % 3, g + 1);
  }
  return 0;
}

int main() {
  char c;
  int i, j, s[10][10];
  while (cin >> x >> y && x + y > 0) {
    for (i = 0; i < y; i++)
      for (j = 0; j < x; j++) {
        cin >> c;
        if (c == 'r')
          s[i][j] = 0;
        if (c == 'g')
          s[i][j] = 1;
        if (c == 'b')
          s[i][j] = 2;
      }
    an = 100;
    ans(s, s[0][0], 0);
    cout << an << endl;
  }

  return 0;
}
Problem p00243 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
  fld[y][x] = c;
  int cnt = 1;
  for (int d = 0; d < 4; d++) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
      continue;
    cnt += change(ny, nx, c, f);
  }
  return cnt;
}

bool dfs(int t = 0, int sum = 1) {
  if (lim == t) {
    bool ok = true;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (fld[0][0] != fld[i][j])
          ok = false;
      }
    return ok;
  }

  int _fld[10][10];
  memcpy(_fld, fld, sizeof(_fld));

  bool ret = false;
  for (int k = 0; k < 3; k++)
    if (k != fld[0][0]) {
      int la = change(0, 0, k, fld[0][0]);
      if (la >= sum)
        ret |= dfs(t + 1, la);
      memcpy(fld, _fld, sizeof(fld));
    }

  return ret;
}
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c;
        cin >> c;
        switch (c) {
        case 'R':
          fld[i][j] = 0;
          break;
        case 'G':
          fld[i][j] = 1;
          break;
        case 'B':
          fld[i][j] = 2;
          break;
        }
      }
    }

    for (lim = 0;; lim++)
      if (dfs())
        break;
    cout << lim << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int H, W;
int fld[10][10];
int lim;
bool change(int y, int x, int c, int f) {
  fld[y][x] = c;
  int cnt = 1;
  for (int d = 0; d < 4; d++) {
    int ny = y + dy[d], nx = x + dx[d];
    if (ny < 0 || ny >= H || nx < 0 || nx >= W || fld[ny][nx] != f)
      continue;
    cnt += change(ny, nx, c, f);
  }
  return cnt;
}

bool dfs(int t = 0, int sum = 1) {
  if (lim == t) {
    bool ok = true;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++) {
        if (fld[0][0] != fld[i][j])
          ok = false;
      }
    return ok;
  }

  int _fld[10][10];
  memcpy(_fld, fld, sizeof(_fld));

  bool ret = false;
  for (int k = 0; k < 3; k++)
    if (k != fld[0][0]) {
      int la = change(0, 0, k, fld[0][0]);
      if (la > sum)
        ret |= dfs(t + 1, la);
      memcpy(fld, _fld, sizeof(fld));
    }

  return ret;
}
int main() {
  while (cin >> W >> H, W || H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        char c;
        cin >> c;
        switch (c) {
        case 'R':
          fld[i][j] = 0;
          break;
        case 'G':
          fld[i][j] = 1;
          break;
        case 'B':
          fld[i][j] = 2;
          break;
        }
      }
    }

    for (lim = 0;; lim++)
      if (dfs())
        break;
    cout << lim << endl;
  }

  return 0;
}