Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  char A;
  int B[100][100];
  int wcnt = 0;
  int wcnt2 = 0;
  int ans = 0;
  int i, j;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      cin >> A;
      if (A == '#')
        B[i][j] = 0;
      else
        B[i][j] = 2000;
    }
  }
  for (i = 0; i < H; i++) {
    wcnt = B[i][0];
    wcnt2 = B[i][W - 1];
    for (j = 0; j < W; j++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[i][W - j - 1] > wcnt2) {
        B[i][W - j - 1] = wcnt2 + 1;
      }
      wcnt2 = B[i][W - j - 1];
    }
  }
  for (j = 0; j < W; j++) {
    wcnt = B[0][j];
    wcnt2 = B[H - 1][j];
    for (i = 0; i < H; i++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[H - i - 1][j] > wcnt2) {
        B[H - i - 1][j] = wcnt2 + 1;
      }
      wcnt2 = B[H - i - 1][j];
    }
  }
  for (i = 0; i < H; i++) {
    wcnt = B[i][0];
    wcnt2 = B[i][W - 1];
    for (j = 0; j < W; j++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[i][W - j - 1] > wcnt2) {
        B[i][W - j - 1] = wcnt2 + 1;
      }
      wcnt2 = B[i][W - j - 1];
    }
  }
  for (j = 0; j < W; j++) {
    wcnt = B[0][j];
    wcnt2 = B[H - 1][j];
    for (i = 0; i < H; i++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[H - i - 1][j] > wcnt2) {
        B[H - i - 1][j] = wcnt2 + 1;
      }
      wcnt2 = B[H - i - 1][j];
      if (ans < B[H - i - 1][j])
        ans = B[H - i - 1][j];
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  char A;
  int B[1000][1000];
  int wcnt = 0;
  int wcnt2 = 0;
  int ans = 0;
  int i, j;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      cin >> A;
      if (A == '#')
        B[i][j] = 0;
      else
        B[i][j] = 2000;
    }
  }
  for (i = 0; i < H; i++) {
    wcnt = B[i][0];
    wcnt2 = B[i][W - 1];
    for (j = 0; j < W; j++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[i][W - j - 1] > wcnt2) {
        B[i][W - j - 1] = wcnt2 + 1;
      }
      wcnt2 = B[i][W - j - 1];
    }
  }
  for (j = 0; j < W; j++) {
    wcnt = B[0][j];
    wcnt2 = B[H - 1][j];
    for (i = 0; i < H; i++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[H - i - 1][j] > wcnt2) {
        B[H - i - 1][j] = wcnt2 + 1;
      }
      wcnt2 = B[H - i - 1][j];
    }
  }
  for (i = 0; i < H; i++) {
    wcnt = B[i][0];
    wcnt2 = B[i][W - 1];
    for (j = 0; j < W; j++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[i][W - j - 1] > wcnt2) {
        B[i][W - j - 1] = wcnt2 + 1;
      }
      wcnt2 = B[i][W - j - 1];
    }
  }
  for (j = 0; j < W; j++) {
    wcnt = B[0][j];
    wcnt2 = B[H - 1][j];
    for (i = 0; i < H; i++) {
      if (B[i][j] > wcnt) {
        B[i][j] = wcnt + 1;
      }
      wcnt = B[i][j];
      if (B[H - i - 1][j] > wcnt2) {
        B[H - i - 1][j] = wcnt2 + 1;
      }
      wcnt2 = B[H - i - 1][j];
      if (ans < B[H - i - 1][j])
        ans = B[H - i - 1][j];
    }
  }
  cout << ans;
  return 0;
}