Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<double, Int> pdp;
#define A first
#define B second
#define pb(x) push_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\n", __LINE__)
#define Itype(a) typeof(a.begin())
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (Itype(t) i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
template <typename T> void getMin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void getMax(T &a, T b) {
  if (a < b)
    a = b;
}
const int inf = 1000000007;

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char A[128][128];
int d[128][128];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  cin >> H >> W;
  queue<pii> q;
  FORO(i, H) {
    cin >> A[i];
    FORO(j, W) {
      d[i][j] = inf;
      if (A[i][j] == '#') {
        d[i][j] = 0;
        q.push(pii(i, j));
      }
    }
  }

  int ans = 0;
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    getMax(ans, d[p.A][p.B]);
    // cout << p.A << " " << p.B << endl;
    for (int r = 0; r < 4; r++) {
      int nx = p.B + dx[r];
      int ny = p.A + dy[r];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (d[ny][nx] < inf)
        continue;
      d[ny][nx] = d[p.A][p.B] + 1;
      q.push(pii(ny, nx));
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef int Int;
typedef pair<Int, Int> pii;
typedef pair<Int, double> pid;
typedef pair<double, double> pdd;
typedef pair<Int, pii> pip;
typedef pair<double, Int> pdp;
#define A first
#define B second
#define pb(x) push_back(x)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (x).size()
#define CLR(x) memset(x, 0, sizeof x)
#define pdebug() printf("%d\n", __LINE__)
#define Itype(a) typeof(a.begin())
#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define FORO(i, n) REP(i, 0, (int)n - 1)
#define FORI(i, n) REP(i, 1, (int)n)
#define FORIT(i, t) for (Itype(t) i = t.begin(); i != t.end(); i++)
#define eps 1e-6
#define sqr(x) ((x) * (x))
#define dist(_a, _b) sqrt(sqr(_a.A - _b.A) + sqr(_a.B - _b.B))
#define norm(_a) sqrt(sqr(_a.A) + sqr(_a.B))
template <typename T> void getMin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void getMax(T &a, T b) {
  if (a < b)
    a = b;
}
const int inf = 1000000007;

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char A[1024][1024];
int d[1024][1024];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int H, W;
  cin >> H >> W;
  queue<pii> q;
  FORO(i, H) {
    cin >> A[i];
    FORO(j, W) {
      d[i][j] = inf;
      if (A[i][j] == '#') {
        d[i][j] = 0;
        q.push(pii(i, j));
      }
    }
  }

  int ans = 0;
  while (!q.empty()) {
    pii p = q.front();
    q.pop();
    getMax(ans, d[p.A][p.B]);
    // cout << p.A << " " << p.B << endl;
    for (int r = 0; r < 4; r++) {
      int nx = p.B + dx[r];
      int ny = p.A + dy[r];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (d[ny][nx] < inf)
        continue;
      d[ny][nx] = d[p.A][p.B] + 1;
      q.push(pii(ny, nx));
    }
  }

  cout << ans << endl;

  return 0;
}