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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

char a[110][110];

int ans = 0;
int h, w;

void Solution(int y, int x, char z) {
  a[y][x] = '0';

  if (a[y + 1][x] == z) {
    Solution(y + 1, x, z);
  }

  if (a[y - 1][x] == z) {
    Solution(y - 1, x, z);
  }

  if (a[y][x + 1] == z) {
    Solution(y, x + 1, z);
  }

  if (a[y][x - 1] == z) {
    Solution(y, x - 1, z);
  }
}

int main() {
  while (cin >> h >> w, h) {
    ans = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        a[i][j] = '0';
      }
    }

    if (h == 0) {
      break;
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != '0') {
          Solution(i, j, a[i][j]);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

char a[101][101];

int ans = 0;
int h, w;

void Solution(int y, int x, char z) {
  a[y][x] = '0';

  if (a[y + 1][x] == z) {
    Solution(y + 1, x, z);
  }

  if (a[y - 1][x] == z) {
    Solution(y - 1, x, z);
  }

  if (a[y][x + 1] == z) {
    Solution(y, x + 1, z);
  }

  if (a[y][x - 1] == z) {
    Solution(y, x - 1, z);
  }
}

int main() {
  while (cin >> h >> w && (h | w)) {
    ans = 0;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        a[i][j] = '0';
      }
    }

    if (h == 0) {
      break;
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != '0') {
          Solution(i, j, a[i][j]);
          ans++;
        }
      }
    }

    cout << ans << endl;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int farm[101][101];
int h, w;

void kubun(int k, int a, int b) {
  if (a<0 | a> w | b<0 | b> h)
    return;
  farm[a][b] = 0;
  if (k == farm[a - 1][b])
    kubun(farm[a - 1][b], a - 1, b);
  if (k == farm[a + 1][b])
    kubun(farm[a + 1][b], a + 1, b);
  if (k == farm[a][b - 1])
    kubun(farm[a][b - 1], a, b - 1);
  if (k == farm[a][b + 1])
    kubun(farm[a][b + 1], a, b + 1);
}
int main(void) {
  char c[101][101];
  int a, b;
  int ans = 0;
  while (1) {
    ans = 0;
    for (b = 0; b < 101; b++) {
      for (a = 0; a < 101; a++) {
        farm[a][b] = 0;
      }
    }
    scanf("%d %d", &h, &w);
    if (h == 0 && w == 0)
      break;
    for (b = 0; b < h; b++) {
      scanf("%s", c[b]);
      for (a = 0; a < w; a++) {
        if (c[b][a] == '@')
          farm[a][b] = 1;
        if (c[b][a] == '#')
          farm[a][b] = 2;
        if (c[b][a] == '*')
          farm[a][b] = 3;
      }
    }
    for (b = 0; b < h; b++) {
      for (a = 0; a < w; a++) {
        if (farm[a][b] == 0)
          ;
        else {
          ans++;
          kubun(farm[a][b], a, b);
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int farm[101][101];

void kubun(int k, int a, int b) {
  farm[a][b] = 0;
  if (k == farm[a - 1][b])
    kubun(farm[a - 1][b], a - 1, b);
  if (k == farm[a + 1][b])
    kubun(farm[a + 1][b], a + 1, b);
  if (k == farm[a][b - 1])
    kubun(farm[a][b - 1], a, b - 1);
  if (k == farm[a][b + 1])
    kubun(farm[a][b + 1], a, b + 1);
}
int main() {
  char c[101][101];
  int a, b;
  int h, w;
  int ans = 0;
  while (1) {
    ans = 0;
    for (b = 0; b < 101; b++) {
      for (a = 0; a < 101; a++) {
        farm[a][b] = 0;
      }
    }
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    for (b = 0; b < w; b++) {
      cin >> c[b];
      for (a = 0; a < h; a++) {
        if (c[b][a] == '@')
          farm[a][b] = 1;
        if (c[b][a] == '#')
          farm[a][b] = 2;
        if (c[b][a] == '*')
          farm[a][b] = 3;
      }
    }
    for (b = 0; b < w; b++) {
      for (a = 0; a < h; a++) {
        if (farm[a][b] == 0)
          ;
        else {
          ans++;
          kubun(farm[a][b], a, b);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

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

int h, w;
char f[105][105];
// string f[105];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void dfs(int y, int x, char c) {
  f[y][x] = '.';
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (nx >= 0 || nx < w || ny >= 0 || ny < h) {
      if (f[ny][nx] == c) {
        dfs(ny, nx, c);
      }
    }
  }
}

int main() {
  int cnt = 0;

  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      return 0;
    for (int i = 0; i < h; i++)
      cin >> f[i];

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char current = f[i][j];
        if (current != '.') {
          dfs(i, j, current);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
    cnt = 0;
  }
}

Error: 0

Fixed submission:

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

string f[105];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void dfs(int h, int w, char c) {
  f[h][w] = '.';
  for (int i = 0; i < 4; i++) {
    int nx = w + dx[i];
    int ny = h + dy[i];

    if (nx >= 0 || nx <= w || ny >= 0 || ny <= h) {
      if (f[ny][nx] == c)
        dfs(ny, nx, c);
    }
  }
}

int main() {
  int h, w, cnt = 0;

  for (int i = 0; i < 105; i++) {
    f[i] = "1";
  }

  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      return 0;
    for (int i = 0; i < h; i++)
      cin >> f[i];

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char current = f[i][j];
        if (current != '.') {
          dfs(i, j, current);
          cnt++;
        }
      }
    }
    cout << cnt << endl;
    cnt = 0;
  }
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

char map[101][101];
int a, b;

void dfs(int x, int y, char s) {
  if (x >= a || x < 0) {
    return;
  } else if (y >= b || y < 0) {
    return;
  }

  if (map[x][y] == s) {
    map[x][y] = 'x';
  } else {
    return;
  }

  dfs(x + 1, y, s);
  dfs(x - 1, y, s);
  dfs(x, y + 1, s);
  dfs(x, y - 1, s);
}

int main() {

  int ans;

  while (cin >> a >> b) {
    if (a == 0 && b == 0) {
      break;
    }
    ans = 0;

    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        cin >> map[i][j];
      }
    }

    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        if (map[i][j] != 'x') {
          dfs(i, j, map[i][j]);
          ans += 1;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

char map[101][101];
int a, b;

void dfs(int x, int y, char a) {
  if (x >= a || x < 0) {
    return;
  } else if (y >= b || y < 0) {
    return;
  }

  if (map[x][y] == a) {
    map[x][y] = 'x';
  } else {
    return;
  }

  dfs(x + 1, y, a);
  dfs(x - 1, y, a);
  dfs(x, y + 1, a);
  dfs(x, y - 1, a);
}

int main() {

  int ans;

  while (cin >> a >> b) {
    if (a == 0 && b == 0) {
      return 0;
    }
    ans = 0;

    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; i++) {
        cin >> map[i][j];
      }
    }

    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; i++) {
        if (map[i][j] != 'x') {
          dfs(i, j, map[i][j]);
          ans += 1;
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

struct UnionFind {
  vector<int> par;
  vector<int> rank;

  UnionFind() {}
  UnionFind(int n) { init(n); }

  void init(int n) {
    par.resize(n);
    REP(i, n) par[i] = i;
    rank.resize(n, 0);
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] > rank[y])
      swap(x, y);
    par[x] = y;
    if (rank[x] == rank[y])
      rank[x]++;
  }
};

int main() {
  int h, w;

  while (cin >> h >> w, h || w) {
    string str[100];
    REP(i, h) cin >> str[i];

    UnionFind uf(h * w);
    REP(i, h) REP(j, w) {
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};

      REP(k, 4) {
        int nx = j + dx[k], ny = i + dy[k];
        if (!(nx >= 0 && nx < w && ny >= 0 && ny < h))
          continue;
        if (str[i][j] != str[ny][nx])
          continue;
        uf.unite(i * w + j, ny * w + nx);
      }
    }

    set<int> s;
    REP(i, h * w) s.insert(uf.find(i));
    cout << s.size() << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

struct UnionFind {
  vector<int> par;
  vector<int> rank;

  UnionFind() {}
  UnionFind(int n) { init(n); }

  void init(int n) {
    par.resize(n);
    REP(i, n) par[i] = i;
    rank.resize(n, 0);
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] > rank[y])
      swap(x, y);
    par[x] = y;
    if (rank[x] == rank[y])
      rank[x]++;
  }
};

int main() {
  int h, w;
  UnionFind uf;

  while (cin >> h >> w, h || w) {
    string str[100];
    REP(i, h) cin >> str[i];

    uf.init(h * w);
    REP(i, h) REP(j, w) {
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};

      REP(k, 4) {
        int nx = j + dx[k], ny = i + dy[k];
        if (!(nx >= 0 && nx < w && ny >= 0 && ny < h))
          continue;
        if (str[i][j] != str[ny][nx])
          continue;
        uf.unite(i * w + j, ny * w + nx);
      }
    }

    set<int> s;
    REP(i, h * w) s.insert(uf.find(i));
    cout << s.size() << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

#define rep(x, to) for (int x = 0; x < to; x++)
#define rep2(x, from, to) for (int x = from; x < to; x++)

using namespace std;

typedef pair<int, int> mypair;
int h, w;
string bd[101];

void chil(int y, int x) {
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  char mrk = bd[y][x];
  stack<mypair> que;
  que.push(mypair(y, x));
  while (!que.empty()) {
    int cy = que.top().first;
    int cx = que.top().second;
    que.pop();
    if (bd[cy][cx] == 'C')
      continue;
    if (bd[cy][cx] == mrk)
      bd[cy][cx] = 'C';
    rep(i, 4) {
      if (cy + dy[i] < 0 || cy + dy[i] >= h)
        continue;
      if (cx + dx[i] < 0 || cx + dx[i] >= w)
        continue;
      if (bd[cy + dy[i]][cx + dx[i]] == mrk) {
        que.push(mypair(cy + dy[i], cx + dx[i]));
      }
    }
  }
  //	cout << cnq  << endl;
}

int main(void) {

  string ws;

  while (cin >> h >> w) {
    if (h == 0 && w == 0 || cin.eof())
      break;
    rep(i, h) cin >> bd[i];

    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (bd[i][j] == '#' || bd[i][j] == '*' || bd[i][j] == '@') {
        cnt++;
        chil(i, j);
      }
    }
    cout << cnt << endl;
    //		rep(i,h){ rep(j,w) cout << bd[i][j]; cout << endl; }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define rep(x, to) for (int x = 0; x < to; x++)
#define rep2(x, from, to) for (int x = from; x < to; x++)

using namespace std;
struct stk {
  int y, x;
  stk(int y, int x) : y(y), x(x) {}
};

void chil(int y, int x, int h, int w, vector<string> &bd) {
  int dx[] = {1, -1, 0, 0};
  int dy[] = {0, 0, 1, -1};
  int cnq = 0;
  char mrk = bd[y][x];
  queue<stk> que;
  que.push(stk(y, x));
  while (!que.empty()) {
    stk cr = que.front();
    que.pop();
    if (bd[cr.y][cr.x] == mrk)
      bd[cr.y][cr.x] = 'C';
    rep(i, 4) {
      if (cr.y + dy[i] < 0 || cr.y + dy[i] >= h)
        continue;
      if (cr.x + dx[i] < 0 || cr.x + dx[i] >= w)
        continue;
      if (bd[cr.y + dy[i]][cr.x + dx[i]] == mrk) {
        que.push(stk(cr.y + dy[i], cr.x + dx[i]));
      }
    }
  }
  //	cout << cnq  << endl;
}

int main(void) {

  int h, w;

  while (cin >> h >> w) {
    if (h == 0 && w == 0 || cin.eof())
      break;
    vector<string> bd(h + 1);
    rep(i, h) cin >> bd[i];
    int cnt = 0;
    rep(i, h) rep(j, w) {
      if (bd[i][j] == '#' || bd[i][j] == '*' || bd[i][j] == '@') {
        cnt++;
        chil(i, j, h, w, bd);
      }
    }
    cout << cnt << endl;
    //		rep(i,h){ rep(j,w) cout << bd[i][j]; cout << endl; }
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, l, n) for (lint i = l; i < n; i++)
#define rer(i, l, n) for (lint i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int f[110][110] = {}, h, w, ans;
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
string s[110];

void dfs(int y, int x, char c) {
  if (y < 0 || h <= y || x < 0 || w <= x || s[y][x] != c || f[y][x])
    return;
  f[y][x] = 1;
  rep(i, 0, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    dfs(ny, nx, c);
  }
  return;
}

int main() {
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    rep(i, 0, h) {
      cin >> s[i];
      rep(j, 0, w) { f[i][j] = 0; }
    }
    ans = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (f[i][j] == 0) {
          dfs(i, j, s[i][j]);
          ans++;
        }
      }
    }
    o(ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#define rep(i, l, n) for (lint i = l; i < n; i++)
#define rer(i, l, n) for (lint i = l; i <= n; i++)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define pb(a) push_back(a)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int f[110][110] = {}, h, w, ans;
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
string s[110];

void dfs(int y, int x, char c) {
  if (y < 0 || h <= y || x < 0 || w <= x)
    return;
  if (s[y][x] != c || f[y][x])
    return;
  f[y][x] = 1;
  rep(i, 0, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    dfs(ny, nx, c);
  }
  return;
}

int main() {
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    rep(i, 0, h) {
      rep(j, 0, w) {
        cin >> s[i][j];
        f[i][j] = 0;
      }
    }
    ans = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (f[i][j] == 0) {
          dfs(i, j, s[i][j]);
          ans++;
        }
      }
    }
    o(ans);
  }
}
Problem p00118 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  while (1) {
    int H, W;
    cin >> H >> W;
    if (!H)
      break;
    UnionFind uf(H * W);
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (j != W - 1 && field[i][j] == field[i][j + 1]) {
          uf.unionSet(i * W + j, i * W + j + 1);
        }
        if (i != H - 1 && field[i][j] == field[i + 1][j]) {
          uf.unionSet(i * W + j, (i + 1) * W + j);
        }
      }
    }
    set<int> aset;
    for (int i = 0; i < H * W; ++i) {
      aset.emplace(uf.root(i));
    }
    cout << aset.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int main() {
  while (1) {
    int H, W;
    cin >> H >> W;
    UnionFind uf(H * W);
    vector<string> field(H);
    for (int i = 0; i < H; ++i) {
      cin >> field[i];
    }
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        if (j != W - 1 && field[i][j] == field[i][j + 1]) {
          uf.unionSet(i * W + j, i * W + j + 1);
        }
        if (i != H - 1 && field[i][j] == field[i + 1][j]) {
          uf.unionSet(i * W + j, (i + 1) * W + j);
        }
      }
    }
    set<int> aset;
    for (int i = 0; i < H * W; ++i) {
      aset.emplace(uf.root(i));
    }
    cout << aset.size() << endl;
  }
  return 0;
}
Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <sstream>

const int MAX_W = 100;
const int MAX_H = 100;
int H, W;
char field[MAX_H][MAX_W] = {'.'};

void solve(int y, int x) {
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, 1, 0, -1};
  char c = field[y][x];
  field[y][x] = '.';

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 and ny >= 0 and nx < W and ny < H and field[ny][nx] == c) {
      solve(ny, nx);
    }
  }
}

void print(int w, int h) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cout << field[i][j];
    }
    std::cout << std::endl;
  }

  std::cout << std::endl;
}

int main() {
  while (std::cin >> H >> W, H) {
    for (int i = 0; i < H; i++) {
      std::cin >> field[i];
    }

    int count = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (field[i][j] != '.') {
          solve(i, j);
          count++;
        }
      }
    }
    std::cout << count << std::endl;
  }

  return 0;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <sstream>

const int MAX_W = 200;
const int MAX_H = 200;
char field[MAX_H][MAX_W] = {'.'};

void solve(int y, int x, int h, int w) {
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, 1, 0, -1};
  char c = field[y][x];
  field[y][x] = '.';

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 and ny >= 0 and nx < w and ny < h and field[ny][nx] == c) {
      solve(ny, nx, h, w);
    }
  }
}

void print(int w, int h) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cout << field[i][j];
    }
    std::cout << std::endl;
  }

  std::cout << std::endl;
}

int main() {
  std::string line;
  int w = 0;
  int h = 0;
  std::getline(std::cin, line);
  std::stringstream ss(line);
  std::string buf;
  std::getline(ss, buf, ' ');
  h = atoi(buf.c_str());
  std::getline(ss, buf, ' ');
  w = atoi(buf.c_str());
  int i = 0;
  while (getline(std::cin, line)) {
    if (line == "0 0") {
      break;
    }

    for (int j = 0; j < line.length(); j++) {
      field[i][j] = line[j];
    }

    i++;
  }

  int result = 0;
  for (i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] != '.') {
        // print(w, h);
        solve(i, j, h, w);
        result++;
      }
    }
  }

  std::cout << result << std::endl;
  return 0;
}