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

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

using namespace std;

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

#define all(v) v.begin(), v.end()
#define INF 1 << 30
#define mp make_pair
#define pb push_back

#define fi first
#define se second

#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;

typedef long long ll;

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

int main() {
  while (1) {
    int w, h;
    int aa = 0;
    int bb = 0;
    string a[55];
    string b[55];
    cin >> w >> h;
    if (w == 0)
      break;
    rep(i, h) {
      cin >> a[i];
      b[i] = a[i];
    }
    bool f = true;
    rep(y, h) {
      rep(x, w) {
        if (a[y][x] == 'W' || a[y][x] == 'B')
          f = false;
      }
    }
    if (f) {
      cout << 0 << " " << 0 << endl;
      continue;
    }
    rep(y, h) {
      rep(x, w) {
        if (a[y][x] == 'W') {

          queue<pii> que;
          que.push(mp(x, y));

          while (!que.empty()) {
            int yy = que.front().se;
            int xx = que.front().fi;
            a[yy][xx] = 'w';
            que.pop();
            rep(i, 4) {
              if (yy + dy[i] >= 0 && xx + dx[i] >= 0 && yy + dy[i] < h &&
                  xx + dx[i] < w &&

                  a[yy + dy[i]][xx + dx[i]] == '.') {
                que.push(mp(xx + dx[i], yy + dy[i]));
              }
            }
          }
        }
      }
    }
    rep(y, h) {
      rep(x, w) {
        if (a[y][x] == '.') {
          bb++;
        }
      }
    }
    rep(y, h) {
      rep(x, w) {
        if (b[y][x] == 'B') {

          queue<pii> que;
          que.push(mp(x, y));

          while (!que.empty()) {
            int yy = que.front().se;
            int xx = que.front().fi;
            b[yy][xx] = 'b';
            que.pop();
            rep(i, 4) {
              if (yy + dy[i] >= 0 && xx + dx[i] >= 0 && yy + dy[i] < h &&
                  xx + dx[i] < w &&

                  b[yy + dy[i]][xx + dx[i]] == '.') {
                que.push(mp(xx + dx[i], yy + dy[i]));
              }
            }
          }
        }
      }
    }
    rep(y, h) {
      rep(x, w) {
        if (b[y][x] == '.') {
          aa++;
        }
      }
    }
    cout << bb << " " << aa << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

#define all(v) v.begin(), v.end()
#define INF 1 << 30
#define mp make_pair
#define pb push_back

#define fi first
#define se second

#define each(it, c) for (auto it = (c).begin(); it != (c).end(); it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;

typedef long long ll;

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

int main() {
  while (1) {
    int w, h;
    int aa = 0;
    int bb = 0;
    string a[55];
    string b[55];
    cin >> w >> h;
    if (w == 0)
      break;
    rep(i, h) {
      cin >> a[i];
      b[i] = a[i];
    }
    bool f = true;
    rep(y, h) {
      rep(x, w) {
        if (a[y][x] == 'W' || a[y][x] == 'B')
          f = false;
      }
    }
    if (f) {
      cout << 0 << " " << 0 << endl;
      continue;
    }
    rep(y, h) {
      rep(x, w) {
        if (a[y][x] == 'W') {

          queue<pii> que;
          que.push(mp(x, y));

          while (!que.empty()) {
            int yy = que.front().se;
            int xx = que.front().fi;
            a[yy][xx] = 'w';
            que.pop();
            rep(i, 4) {
              if (a[yy + dy[i]][xx + dx[i]] == '.') {
                que.push(mp(xx + dx[i], yy + dy[i]));
              }
            }
          }
        }
      }
    }
    rep(y, h) {
      rep(x, w) {
        if (a[y][x] == '.') {
          bb++;
        }
      }
    }
    rep(y, h) {
      rep(x, w) {
        if (b[y][x] == 'B') {

          queue<pii> que;
          que.push(mp(x, y));

          while (!que.empty()) {
            int yy = que.front().se;
            int xx = que.front().fi;
            b[yy][xx] = 'b';
            que.pop();
            rep(i, 4) {
              if (b[yy + dy[i]][xx + dx[i]] == '.') {
                que.push(mp(xx + dx[i], yy + dy[i]));
              }
            }
          }
        }
      }
    }
    rep(y, h) {
      rep(x, w) {
        if (b[y][x] == '.') {
          aa++;
        }
      }
    }
    cout << bb << " " << aa << endl;
  }
  return 0;
}