Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 99999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m; i > n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl
#define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N)
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R))
#define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin();
#define mod 1000000007
#define MAX 5100000
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0,
                              flag = 0;
  string S, T, O;
  cin >> H >> W;
  Vec(Graph, string, H, "");
  rep(i, 0, H) cin >> Graph[i];
  DV(dist, lli, H, W, -1);
  queue<pair<int, int>> que;
  rep(i, 0, H) rep(j, 0, W) {
    if (Graph[i][j] == '#') {
      dist[i][j] = 0;
      que.push(pair<int, int>(i, j));
    }
  }
  while (!que.empty()) {
    auto Xq = que.front();
    que.pop();
    rep(i, 0, 4) {
      auto nx = Xq.first + dx[i];
      auto ny = Xq.second + dy[i];
      if (nx < 0 || nx > W - 1 || ny < 0 || ny > H - 1)
        continue;
      if (dist[nx][ny] == -1) {
        dist[nx][ny] = dist[Xq.first][Xq.second] + 1;
        que.push(pair<int, int>(nx, ny));
      }
    }
  }
  rep(i, 0, H) rep(j, 0, W) sum = max(sum, dist[i][j]);
  Out(sum);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 99999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m; i > n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl
#define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N)
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R))
#define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin();
#define mod 1000000007
#define MAX 5100000
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0,
                              flag = 0;
  string S, T, O;
  cin >> H >> W;
  Vec(Graph, string, H, "");
  rep(i, 0, H) cin >> Graph[i];
  DV(dist, lli, H, W, -1);
  queue<pair<int, int>> que;
  rep(i, 0, H) rep(j, 0, W) {
    if (Graph[i][j] == '#') {
      dist[i][j] = 0;
      que.push(pair<int, int>(i, j));
    }
  }
  while (!que.empty()) {
    auto Xq = que.front();
    que.pop();
    rep(i, 0, 4) {
      auto nx = Xq.first + dx[i];
      auto ny = Xq.second + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (dist[nx][ny] == -1) {
        dist[nx][ny] = dist[Xq.first][Xq.second] + 1;
        que.push(pair<int, int>(nx, ny));
      }
    }
  }
  rep(i, 0, H) rep(j, 0, W) sum = max(sum, dist[i][j]);
  Out(sum);
}