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

#include <stdio.h>
#include <string.h>

int width, height;
int map[50][50];
char input_map[50][51];

int main(void) {
  int i, j, k;
  int black, white;
  while (1) {
    scanf("%d%d", &width, &height);
    if (width == 0 && height == 0)
      break;
    memset(map, 0, sizeof(map));
    for (i = 0; i < height; i++) {
      scanf("%s", input_map[i]);
      for (j = 0; j < width; j++) {
        if (input_map[i][j] == 'B')
          map[i][j] = 5;
        else if (input_map[i][j] == 'W')
          map[i][j] = 6;
      }
    }
    for (k = 0; k < width * height; k++) {
      for (i = 0; i < height; i++) {
        for (j = 0; j < width; j++) {
          if ((map[i][j] & 4) == 0) {
            if (i > 0)
              map[i][j] |= map[i - 1][j] & 3;
            if (j > 0)
              map[i][j] |= map[i][j - 1] & 3;
            if (i < height - 1)
              map[i][j] |= map[i + 1][j] & 3;
            if (j < width - 1)
              map[i][j] |= map[i][j + 1] & 3;
          }
        }
      }
    }
    black = white = 0;
    for (i = 0; i < height; i++) {
      for (j = 0; j < width; j++) {
        if (map[i][j] == 1)
          black++;
        else if (map[i][j] == 2)
          white++;
      }
    }
    printf("%d %d\\n", black, white);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXH 64
#define MAXW 64

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

char a[MAXH][MAXW];
int w, h;

int main(void) {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[i][j];
      }
    }
    int white = 0, black = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (a[i][j] != '.')
          continue;
        bool used[MAXH][MAXW];
        for (int k = 0; k < h; k++) {
          for (int l = 0; l < w; l++) {
            used[k][l] = false;
          }
        }
        queue<int> X, Y;
        X.push(j);
        Y.push(i);
        bool white_flag = false, black_flag = false;
        while (!X.empty()) {
          int x = X.front();
          X.pop();
          int y = Y.front();
          Y.pop();
          used[y][x] = true;
          for (int k = 0; k < 4; k++) {
            int nx = x + dx[k];
            int ny = y + dy[k];
            if (0 <= nx && nx < w && 0 <= ny && ny < h) {
              if (a[ny][nx] == 'W')
                white_flag = true;
              else if (a[ny][nx] == 'B')
                black_flag = true;
              else if (!used[ny][nx]) {
                X.push(nx);
                Y.push(ny);
              }
            }
          }
        }
        if (white_flag && !black_flag)
          white++;
        if (!white_flag && black_flag)
          black++;
      }
    }
    cout << black << " " << white << endl;
  }
  return 0;
}