Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end)                                                     \
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for (i64 i = 1; i <= n; ++i)
#define RREP(i, n) for (i64 i = n - 1; i >= 0; --i)
#define RREPS(i, n) for (i64 i = n; i >= 1; --i)

#define ALL(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \
  })(c))

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define pb push_back
#define mp make_pair
#define F first
#define S second

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename C> i64 SIZE(const C &c) {
  return static_cast<i64>(c.size());
}

template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
  return static_cast<i64>(N);
}

struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;

#define MAX_H 1000
#define MAX_W 1000
#define INF 1152921504606846976

typedef pair<i64, i64> P;

i64 H, W;
char G[MAX_H][MAX_W];
i64 d[MAX_H][MAX_W];
vector<P> start;

i64 dy[] = {1, 0, -1, 0};
i64 dx[] = {0, 1, 0, -1};

bool inside(i64 y, i64 x) { return (0 <= y < H && 0 <= x < H); }

void solve() {
  REP(y, H) {
    REP(x, W) { d[y][x] = INF; }
  }

  queue<P> que;
  for (auto it = start.begin(); it != start.end(); it++) {
    que.push(*it);
    d[(*it).F][(*it).S] = 0;
  }

  i64 ny, nx;
  while (!que.empty()) {
    P yx = que.front();
    que.pop();

    REP(i, 4) {
      ny = yx.F + dy[i];
      nx = yx.S + dx[i];
      if (inside(ny, nx) && d[ny][nx] == INF && G[ny][nx] == '.') {
        que.push(mp(ny, nx));
        d[ny][nx] = d[yx.F][yx.S] + 1;
        G[ny][nx] = 'o';
      }
    }
  }

  i64 maxd = 0;
  REP(y, H) {
    REP(x, W) {
      if (G[y][x] == 'o') {
        chmax(maxd, d[y][x]);
      }
    }
  }
  cout << maxd << endl;
}

int main() {
  cin >> H >> W;
  REP(y, H) {
    REP(x, W) {
      cin >> G[y][x];
      if (G[y][x] == '#') {
        start.pb(mp(y, x));
      }
    }
  }

  solve();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;

#define FOR(i, start, end)                                                     \
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) for (i64 i = 1; i <= n; ++i)
#define RREP(i, n) for (i64 i = n - 1; i >= 0; --i)
#define RREPS(i, n) for (i64 i = n; i >= 1; --i)

#define ALL(f, c, ...)                                                         \
  (([&](decltype((c)) cccc) {                                                  \
    return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__);               \
  })(c))

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

#define pb push_back
#define mp make_pair
#define F first
#define S second

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename C> i64 SIZE(const C &c) {
  return static_cast<i64>(c.size());
}

template <typename T, size_t N> i64 SIZE(const T (&)[N]) {
  return static_cast<i64>(N);
}

struct ProconInit {
  static constexpr int IOS_PREC = 15;
  static constexpr bool AUTOFLUSH = false;

  ProconInit() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(IOS_PREC);
    if (AUTOFLUSH)
      cout << unitbuf;
  }
} PROCON_INIT;

#define MAX_H 1000
#define MAX_W 1000
#define INF 1152921504606846976

typedef pair<i64, i64> P;

i64 H, W;
char G[MAX_H][MAX_W];
i64 d[MAX_H][MAX_W];
vector<P> start;

i64 dy[] = {1, 0, -1, 0};
i64 dx[] = {0, 1, 0, -1};

bool inside(i64 y, i64 x) { return (0 <= y && y < H && 0 <= x && x < W); }

void solve() {
  REP(y, H) {
    REP(x, W) { d[y][x] = INF; }
  }

  queue<P> que;
  for (auto it = start.begin(); it != start.end(); it++) {
    que.push(*it);
    d[(*it).F][(*it).S] = 0;
  }

  i64 ny, nx;
  while (!que.empty()) {
    P yx = que.front();
    que.pop();

    REP(i, 4) {
      ny = yx.F + dy[i];
      nx = yx.S + dx[i];
      if (inside(ny, nx) && d[ny][nx] == INF && G[ny][nx] == '.') {
        que.push(mp(ny, nx));
        d[ny][nx] = d[yx.F][yx.S] + 1;
        G[ny][nx] = 'o';
      }
    }
  }

  i64 maxd = 0;
  REP(y, H) {
    REP(x, W) {
      if (G[y][x] == 'o') {
        chmax(maxd, d[y][x]);
      }
    }
  }
  cout << maxd << endl;
}

int main() {
  cin >> H >> W;
  REP(y, H) {
    REP(x, W) {
      cin >> G[y][x];
      if (G[y][x] == '#') {
        start.pb(mp(y, x));
      }
    }
  }

  solve();

  return 0;
}