Problem p01139 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int A[60][60];
int B[60][60];
int x, y;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dfs(int x, int y) {
  A[x][y] = 3;
  int ans = 0;
  REP(i, 4) {
    int xx = dx[i] + x;
    int yy = dy[i] + y;
    if (A[xx][yy] == 0) {
      ans += dfs(xx, yy);
    }
  }
  return ans + 1;
}

int dfs2(int x, int y) {
  B[x][y] = 3;
  int ans = 0;
  REP(i, 4) {
    int xx = dx[i] + x;
    int yy = dy[i] + y;
    if (B[xx][yy] == 0) {
      ans += dfs2(xx, yy);
    }
  }
  return ans + 1;
}

int main() {
  int W, H;
  while (cin >> W >> H, W + H) {
    REP(i, 30) {
      REP(j, 30) {
        A[i][j] = 1;
        B[i][j] = 1;
      }
    }
    REP(i, H) {
      string s;
      cin >> s;
      REP(j, W) {
        if (s[j] == '.') {
          A[i + 1][j + 1] = 0;
          B[i + 1][j + 1] = 0;
        }
        if (s[j] == 'B') {
          A[i + 1][j + 1] = 2;
        }
        if (s[j] == 'W') {
          B[i + 1][j + 1] = 2;
        }
      }
    }
    REP(i, H + 1) {
      REP(j, W + 1) {
        if (A[i][j] == 2) {
          REP(k, 4) {
            int x = i + dx[k];
            int y = j + dy[k];
            if (A[x][y] == 0) {
              dfs(x, y);
            }
          }
        }
      }
    }
    REP(i, H + 1) {
      REP(j, W + 1) {
        if (B[i][j] == 2) {
          REP(k, 4) {
            int x = i + dx[k];
            int y = j + dy[k];
            if (B[x][y] == 0) {
              dfs2(x, y);
            }
          }
        }
      }
    }
    int w = 0;
    int b = 0;
    REP(i, H + 1) {
      REP(j, W + 1) {
        if (A[i][j] == 3 && B[i][j] != 3)
          b++;
        if (A[i][j] != 3 && B[i][j] == 3)
          w++;
      }
    }
    cout << b << " " << w << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int A[60][60];
int B[60][60];
int x, y;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dfs(int x, int y) {
  A[x][y] = 3;
  int ans = 0;
  REP(i, 4) {
    int xx = dx[i] + x;
    int yy = dy[i] + y;
    if (A[xx][yy] == 0) {
      ans += dfs(xx, yy);
    }
  }
  return ans + 1;
}

int dfs2(int x, int y) {
  B[x][y] = 3;
  int ans = 0;
  REP(i, 4) {
    int xx = dx[i] + x;
    int yy = dy[i] + y;
    if (B[xx][yy] == 0) {
      ans += dfs2(xx, yy);
    }
  }
  return ans + 1;
}

int main() {
  int W, H;
  while (cin >> W >> H, W + H) {
    REP(i, 60) {
      REP(j, 60) {
        A[i][j] = 1;
        B[i][j] = 1;
      }
    }
    REP(i, H) {
      string s;
      cin >> s;
      REP(j, W) {
        if (s[j] == '.') {
          A[i + 1][j + 1] = 0;
          B[i + 1][j + 1] = 0;
        }
        if (s[j] == 'B') {
          A[i + 1][j + 1] = 2;
        }
        if (s[j] == 'W') {
          B[i + 1][j + 1] = 2;
        }
      }
    }
    REP(i, H + 1) {
      REP(j, W + 1) {
        if (A[i][j] == 2) {
          REP(k, 4) {
            int x = i + dx[k];
            int y = j + dy[k];
            if (A[x][y] == 0) {
              dfs(x, y);
            }
          }
        }
      }
    }
    REP(i, H + 1) {
      REP(j, W + 1) {
        if (B[i][j] == 2) {
          REP(k, 4) {
            int x = i + dx[k];
            int y = j + dy[k];
            if (B[x][y] == 0) {
              dfs2(x, y);
            }
          }
        }
      }
    }
    int w = 0;
    int b = 0;
    REP(i, H + 1) {
      REP(j, W + 1) {
        if (A[i][j] == 3 && B[i][j] != 3)
          b++;
        if (A[i][j] != 3 && B[i][j] == 3)
          w++;
      }
    }
    cout << b << " " << w << endl;
  }
  return 0;
}