Problem p00118 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
char f[105][105];
bool used[105][105];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int h, w;
void bfs(int i, int j) {
  char ret = f[i][j];
  queue<P> q;
  q.push(mp(i, j));
  used[i][j] = true;
  while (!q.empty()) {
    P a = q.front();
    q.pop();
    for (int k = 0; k < 4; k++) {
      int nx = a.fi + dx[k], ny = a.sec + dy[k];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (used[nx][ny])
        continue;
      if (f[nx][ny] != ret)
        continue;
      used[nx][ny] = true;
      q.push(mp(nx, ny));
    }
  }
  return;
}
int main() {
  while (1) {
    memset(used, false, sizeof(used));
    scanf("%d %d", &h, &w);
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; i++)
      scanf("%s", f[i]);
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!used[i][j]) {
          bfs(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
char f[105][105];
bool used[105][105];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int h, w;
void bfs(int i, int j) {
  char ret = f[i][j];
  queue<P> q;
  q.push(mp(i, j));
  while (!q.empty()) {
    P a = q.front();
    q.pop();
    used[a.fi][a.sec] = true;
    for (int k = 0; k < 4; k++) {
      int nx = a.fi + dx[k], ny = a.sec + dy[k];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (used[nx][ny])
        continue;
      if (f[nx][ny] != ret)
        continue;
      q.push(mp(nx, ny));
    }
  }
  return;
}
int main() {
  while (1) {
    memset(used, false, sizeof(used));
    scanf("%d %d", &h, &w);
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; i++)
      scanf("%s", f[i]);
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!used[i][j]) {
          bfs(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}