Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (int i = b - 1; i >= a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;

const ll INF = (1LL << 50);

set<pll> black;
vector<vector<bool>> used;
bool in_range(ll i, ll l, ll r) { return l <= i && i < r; }
void solve(long long H, long long W, std::vector<std::string> A) {
  used.resize(H);
  set<pll> tonari;
  tonari.insert(pll(1, 0));
  tonari.insert(pll(-1, 0));
  tonari.insert(pll(0, 1));
  tonari.insert(pll(0, -1));
  For(i, H) {
    used[i].resize(W);
    fill(used[i].begin(), used[i].end(), false);
  }
  For(i, H) For(j, W) {
    if (A[i][j] == '#') {
      black.insert(pll(i, j));
      used[i][j] = true;
    }
  }
  set<pll> next;
  ll ans = 0;
  while (!black.empty()) {
    next.clear();
    for (auto &&i : black) {
      for (auto &&j : tonari) {
        if (in_range(i.first + j.first, 0, H) &&
            in_range(i.second + j.second, 0, W) &&
            used[i.first + j.first][i.second + j.second] == false) {
          used[i.first + j.first][i.second + j.second] = true;
          next.insert(pll(i.first + j.first, i.second + j.second));
        }
      }
    }
    if (next.empty())
      break;
    ans++;
    dump(next.size());
    black = next;
  }
  cout << ans << endl;
}

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 < H; i++) {
    std::cin >> A[i];
  }
  solve(H, W, std::move(A));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr << #a << "=" << a << endl
#define dumparr(a, n) cerr << #a << "[" << n << "]=" << a[n] << endl
#else
#define dump(a)
#define dumparr(a, n)
#endif
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for (int i = b - 1; i >= a; i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> ppll;
typedef vector<ll> vll;

const ll INF = (1LL << 50);

set<pll> black;
vector<vector<bool>> used;
bool in_range(ll i, ll l, ll r) { return l <= i && i < r; }
void solve(long long H, long long W, std::vector<std::string> A) {
  used.resize(H);
  set<pll> tonari;
  tonari.insert(pll(1, 0));
  tonari.insert(pll(-1, 0));
  tonari.insert(pll(0, 1));
  tonari.insert(pll(0, -1));
  For(i, H) {
    used[i].resize(W);
    fill(used[i].begin(), used[i].end(), false);
  }
  For(i, H) For(j, W) {
    if (A[i][j] == '#') {
      black.insert(pll(i, j));
      used[i][j] = true;
    }
  }
  set<pll> next;
  ll ans = 0;
  while (!black.empty()) {
    next.clear();
    for (auto &&i : black) {
      for (auto &&j : tonari) {
        if (in_range(i.first + j.first, 0, H) &&
            in_range(i.second + j.second, 0, W) &&
            used[i.first + j.first][i.second + j.second] == false) {
          used[i.first + j.first][i.second + j.second] = true;
          next.insert(pll(i.first + j.first, i.second + j.second));
        }
      }
    }
    if (next.empty())
      break;
    ans++;
    dump(next.size());
    black = next;
  }
  cout << ans << endl;
}

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;
}