Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, x, n) for (int i = x; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
#define sp(p) cout << setprecision(16) << fixed << p << endl;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define SORT(a) sort(all(a))
#define RSORT(a) sort(rall(a))
#define UNIQ(a) unique(all(a))
#define VOUT(v, i)                                                             \
  cout << #v << ": ";                                                          \
  for (int i = 0; i < (int)v.size(); i++) {                                    \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << "\n";
#define vout(v) VOUT(v, z);
#define vmin(v) *min_element(all(v))
#define vmax(v) *max_element(all(v))
#define vsum(v) accumulate(all(v), 0LL);
#define MOUT(m, r, c)                                                          \
  rep(i, r) {                                                                  \
    rep(j, c) { cout << m[i][j] << " "; }                                      \
    cout << endl;                                                              \
  }
#define mout(m) MOUT(m, m.size(), m[0].size())
#define debg(a) cout << #a << " " << a << endl;
#define show(a)                                                                \
  for (cont & y : a) {                                                         \
    for (cont & x : y) {                                                       \
      cout << x << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
template <class T> inline int out(const T &t) {
  print(t);
  putchar('\n');
  return 0;
}
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return (gcd(b, a % b));
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
// const long long MOD=1000000007;
const long long INF = 1e18;
#define EPS (1e-7)
#define PI (acos(-1))
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<vector<ll>> num(1010, vector<ll>(1010, -1));
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
bool isRange(ll y, ll x, ll H, ll W) {
  return (0 <= y && y < H && 0 <= x && x < W);
}

void solve(long long H, long long W, std::vector<std::string> A) {
  ll val = 0;
  queue<P> q;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      if (A[i][j] == '#') {
        q.push(P(i, j));
        num[i][j] = 0;
      }
    }
  }
  while (q.size()) {
    P p = q.front();
    q.pop();
    for (ll i = 0; i < 4; i++) {
      ll ny = p.first + dy[i], nx = p.second + dx[i];
      if (isRange(ny, nx, H, W) && A[ny][nx] == '.' && num[ny][nx] == -1) {
        num[ny][nx] = num[p.first][p.second] + 1;
        q.push(P(ny, nx));
        chmax(val, num[ny][nx]);
      }
    }
  }
  // MOUT(num, H, W);
  cout << val << "\n";
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;
  scanf("%lld", &W);
  std::vector<std::string> A(W);
  for (int i = 0; i < W; i++) {
    std::cin >> A[i];
  }
  solve(H, W, std::move(A));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, x, n) for (int i = x; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
#define sp(p) cout << setprecision(16) << fixed << p << endl;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define SORT(a) sort(all(a))
#define RSORT(a) sort(rall(a))
#define UNIQ(a) unique(all(a))
#define VOUT(v, i)                                                             \
  cout << #v << ": ";                                                          \
  for (int i = 0; i < (int)v.size(); i++) {                                    \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << "\n";
#define vout(v) VOUT(v, z);
#define vmin(v) *min_element(all(v))
#define vmax(v) *max_element(all(v))
#define vsum(v) accumulate(all(v), 0LL);
#define MOUT(m, r, c)                                                          \
  rep(i, r) {                                                                  \
    rep(j, c) { cout << m[i][j] << " "; }                                      \
    cout << endl;                                                              \
  }
#define mout(m) MOUT(m, m.size(), m[0].size())
#define debg(a) cout << #a << " " << a << endl;
#define show(a)                                                                \
  for (cont & y : a) {                                                         \
    for (cont & x : y) {                                                       \
      cout << x << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
template <class T> inline int out(const T &t) {
  print(t);
  putchar('\n');
  return 0;
}
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return (gcd(b, a % b));
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
// const long long MOD=1000000007;
const long long INF = 1e18;
#define EPS (1e-7)
#define PI (acos(-1))
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vector<vector<ll>> num(1010, vector<ll>(1010, -1));
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
bool isRange(ll y, ll x, ll H, ll W) {
  return (0 <= y && y < H && 0 <= x && x < W);
}

void solve(long long H, long long W, std::vector<std::string> A) {
  ll val = 0;
  queue<P> q;
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      if (A[i][j] == '#') {
        q.push(P(i, j));
        num[i][j] = 0;
      }
    }
  }
  while (q.size()) {
    P p = q.front();
    q.pop();
    for (ll i = 0; i < 4; i++) {
      ll ny = p.first + dy[i], nx = p.second + dx[i];
      if (isRange(ny, nx, H, W) && A[ny][nx] == '.' && num[ny][nx] == -1) {
        num[ny][nx] = num[p.first][p.second] + 1;
        q.push(P(ny, nx));
        chmax(val, num[ny][nx]);
      }
    }
  }
  // MOUT(num, H, W);
  cout << val << "\n";
}

int main() {
  long long H;
  scanf("%lld", &H);
  long long W;
  scanf("%lld", &W);
  std::vector<std::string> A(H);
  for (int i = 0; i < H; i++) {
    std::cin >> A[i];
  }
  solve(H, W, std::move(A));
  return 0;
}