Problem p03053 (C++)

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

#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
typedef vector<int> vint;
typedef pair<int, int> pint;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T, typename U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
struct SetUp {
  SetUp() {
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);
  };
} setUp;

int H, W;
bool board[1100000];
signed main() {
  cin >> H >> W;
  deque<pint> q;
  rep(i, H) {
    string A;
    cin >> A;
    rep(j, W) {
      board[i * W + j] = A[j] == '#';
      if (board[i * W + j]) {
        q.push_front({i * W + j, 0});
      }
    }
  }

  int ans = 0;
  while (!q.empty()) {
    cerr << endl;
    int cur = q.front().first;
    int dist = q.front().second;
    cerr << cur << endl;
    rep(i, q.size()) { cerr << q[i].first << " "; }
    q.pop_front();
    int cy = cur / W, cx = cur % W;
    chmax(ans, dist);
    rep(i, 4) {
      int ny = cy + DY[i], nx = cx + DX[i];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W) {
        continue;
      }
      if (board[ny * W + nx]) {
        continue;
      }
      board[ny * W + nx] = true;
      q.push_back({ny * W + nx, dist + 1});
    }
  }
  cout << ans << endl;
}

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

#define int long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define reps(i, n) for (int(i) = 1; (i) <= (n); (i)++)
#define all(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
typedef vector<int> vint;
typedef pair<int, int> pint;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T, typename U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T, typename U> inline bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
struct SetUp {
  SetUp() {
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);
  };
} setUp;

int H, W;
bool board[1100000];
signed main() {
  cin >> H >> W;
  deque<pint> q;
  rep(i, H) {
    string A;
    cin >> A;
    rep(j, W) {
      board[i * W + j] = A[j] == '#';
      if (board[i * W + j]) {
        q.push_front({i * W + j, 0});
      }
    }
  }

  int ans = 0;
  while (!q.empty()) {
    cerr << endl;
    int cur = q.front().first;
    int dist = q.front().second;
    q.pop_front();
    int cy = cur / W, cx = cur % W;
    chmax(ans, dist);
    rep(i, 4) {
      int ny = cy + DY[i], nx = cx + DX[i];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W) {
        continue;
      }
      if (board[ny * W + nx]) {
        continue;
      }
      board[ny * W + nx] = true;
      q.push_back({ny * W + nx, dist + 1});
    }
  }
  cout << ans << endl;
}