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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
typedef pair<ll, ll> LLP;
#define INF 1000000007
#define MOD 1000000007

#define ARRAY_MAX 60
int h, w;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int mp[ARRAY_MAX][ARRAY_MAX];
int B = 0, W = 0;

int dfs(int y, int x, int num, int sum, bool &white, bool &black) {
  // 今いるところをnumに置き換える
  // y座標、x座標、置き換える数字,
  mp[y][x] = num;
  int cnt = 0; // 今の位置から何回dfsを呼び出したか

  // 移動方向をループ
  REP(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (mp[ny][nx] == -2) {
      black = true;
    }
    if (mp[ny][nx] == -3) {
      white = true;
    }
    if (0 <= nx && nx < w && 0 <= ny && ny < h && mp[ny][nx] == -1) {
      cnt += dfs(ny, nx, num, sum, white, black);
      cnt++; // 自信の数をカウント
    }
  }
  return cnt;
}

int solve() {
  // B,W以外の領域に番号を振る
  int res = 0;
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (mp[y][x] == -1) {
        bool white = false, black = false;
        int sum =
            dfs(y, x, res, 1, white, black) + 1; // 孤立領域の面積のカウント
        res++;
        if (black == true && white == false) {
          B += sum;
        } else if (black == false && white == true) {
          W += sum;
        }
      }
    }
  }
  return res;
}

int main() {

  while (cin >> w >> h, h) {

    REP(i, ARRAY_MAX) {
      REP(j, ARRAY_MAX) {
        mp[i][j] = INF; // 適当な文字で初期化
      }
    }
    REP(i, h) {
      REP(j, w) {
        char c;
        cin >> c;
        if (c == '.') {
          mp[i][j] = -1;
        }
        if (c == 'B') {
          mp[i][j] = -2;
        }
        if (c == 'W') {
          mp[i][j] = -3;
        }
        // cin >> mp[i][j];
      }
    }

    solve();

    cout << B << " " << W << endl;
    B = W = 0;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
typedef pair<ll, ll> LLP;
#define INF 1000000007
#define MOD 1000000007

#define ARRAY_MAX 55
int h, w;
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
char mp[ARRAY_MAX][ARRAY_MAX];
int B = 0, W = 0;

int dfs(int y, int x, int num, int sum, bool &white, bool &black) {
  // 今いるところをnumに置き換える
  // y座標、x座標、置き換える数字,
  mp[y][x] = num;
  int cnt = 0; // 今の位置から何回dfsを呼び出したか

  // 移動方向をループ
  REP(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];
    if (mp[ny][nx] == 'B') {
      black = true;
    }
    if (mp[ny][nx] == 'W') {
      white = true;
    }
    if (0 <= nx && nx < w && 0 <= ny && ny < h && mp[ny][nx] == '.') {
      cnt += dfs(ny, nx, num, sum, white, black);
      cnt++; // 自信の数をカウント
    }
  }
  return cnt;
}

int solve() {
  // B,W以外の領域に番号を振る
  int res = '0';
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      if (mp[y][x] == '.') {
        bool white = false, black = false;
        int sum = dfs(y, x, res, 1, white, black) + 1;
        res++;
        if (black == true && white == false) {
          B += sum;
        } else if (black == false && white == true) {
          W += sum;
        }
      }
    }
  }
  return res;
}

int main() {

  while (cin >> w >> h, h) {

    REP(i, ARRAY_MAX) {
      REP(j, ARRAY_MAX) {
        mp[i][j] = '&'; // 適当な文字で初期化
      }
    }
    REP(i, h) {
      REP(j, w) { cin >> mp[i][j]; }
    }

    solve();

    cout << B << " " << W << endl;
    B = W = 0;
  }

  return 0;
}