Problem p03053 (C++)

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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define ull unsigned long long
#define ld long double

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) rep2(i, 0, n)
#define rep1(i, n) rep1(i, 1, n + 1)

// #define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
// #define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend() //

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

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

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);
  //    cout << std::setfill('0') << std::setw(2);

  long long H;
  std::cin >> H;
  long long W;
  std::cin >> W;
  std::vector<std::string> S(W);
  for (int i = 0; i < W; i++) {
    std::cin >> S[i];
  }

  VV<int> r(H, (V<int>(W, INF)));
  queue<pii> q;
  rep(i, H) rep(j, W) {
    if (S[i][j] == '#') {
      r[i][j] = 0;
      q.emplace(i, j);
    }
  }
  int ans = 0;
  while (!q.empty()) {
    pii p = q.front();
    int x = p.second, y = p.first;
    q.pop();
    auto update = [&](int x, int y, int d) {
      if (r[y][x] != INF)
        return;
      r[y][x] = d;
      ans = max(ans, d);
      q.push(make_pair(y, x));
    };
    rep(i, 4) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (xx < 0 || yy < 0 || xx >= W || yy >= H)
        continue;
      if (S[yy][xx] == '#')
        continue;
      update(xx, yy, r[y][x] + 1);
    }
  }
  cout << ans << endl;
  return 0;
}


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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

#define ull unsigned long long
#define ld long double

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) rep2(i, 0, n)
#define rep1(i, n) rep1(i, 1, n + 1)

// #define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
// #define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend() //

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

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

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(15);
  //    cout << std::setfill('0') << std::setw(2);

  long long H;
  std::cin >> H;
  long long W;
  std::cin >> W;
  std::vector<std::string> S(H);
  for (int i = 0; i < H; i++) {
    std::cin >> S[i];
  }

  VV<int> r(H, (V<int>(W, INF)));
  queue<pii> q;
  rep(i, H) rep(j, W) {
    if (S[i][j] == '#') {
      r[i][j] = 0;
      q.emplace(i, j);
    }
  }
  int ans = 0;
  while (!q.empty()) {
    pii p = q.front();
    int x = p.second, y = p.first;
    q.pop();
    auto update = [&](int x, int y, int d) {
      if (r[y][x] != INF)
        return;
      r[y][x] = d;
      ans = max(ans, d);
      q.push(make_pair(y, x));
    };
    rep(i, 4) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (xx < 0 || yy < 0 || xx >= W || yy >= H)
        continue;
      if (S[yy][xx] == '#')
        continue;
      update(xx, yy, r[y][x] + 1);
    }
  }
  cout << ans << endl;
  return 0;
}