Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e18;
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;
}
string get_str(string s) {
  s += ',';
  string ret = "";
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == ',')
      ret += "=%ld, ";
    else
      ret += s[i];
  }
  return ret;
}
#define dump(...)                                                              \
  printf(get_str(#__VA_ARGS__).c_str(), __VA_ARGS__);                          \
  cout << endl

#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

//******************************************************************************
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

#define P2 pair<int, pair<int, int>>
signed main() {
  // IOS();
  int H, W;
  cin >> H >> W;
  auto s = make_v<char>(H, W);
  auto dist = make_v<int>(H, W);
  fill_v(dist, INF);
  queue<P2> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s[i][j];
      if (s[i][j] == '#') {
        que.push({0, {i, j}});
        dist[i][j] = 0;
      }
    }
  }

  int dy[] = {1, 0, -1, 0};
  int dx[] = {0, -1, 0, 1};
  while (!que.empty()) {
    int i = que.front().se.fi;
    int j = que.front().se.se;
    int cost = que.front().fi;
    que.pop();
    if (dist[i][j] != cost)
      continue;
    rep(k, 0, 4) {
      int ni = i + dy[k];
      int nj = j + dx[k];
      if (ni < 0 || H <= ni || nj < 0 || W <= nj)
        continue;
      if (dist[ni][nj] < cost + 1)
        continue;
      dist[ni][nj] = cost + 1;
      que.push({cost + 1, {ni, nj}});
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      // cout<<dist[i][j]<<endl;
      chmax(ans, dist[i][j]);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e18;
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;
}
string get_str(string s) {
  s += ',';
  string ret = "";
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == ',')
      ret += "=%ld, ";
    else
      ret += s[i];
  }
  return ret;
}
#define dump(...)                                                              \
  printf(get_str(#__VA_ARGS__).c_str(), __VA_ARGS__);                          \
  cout << endl

#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

//******************************************************************************
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

#define P2 pair<int, pair<int, int>>
signed main() {
  // IOS();
  int H, W;
  cin >> H >> W;
  auto s = make_v<char>(H, W);
  auto dist = make_v<int>(H, W);
  fill_v(dist, INF);
  queue<P2> que;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s[i][j];
      if (s[i][j] == '#') {
        que.push({0, {i, j}});
        dist[i][j] = 0;
      }
    }
  }

  int dy[] = {1, 0, -1, 0};
  int dx[] = {0, -1, 0, 1};
  while (!que.empty()) {
    int i = que.front().se.fi;
    int j = que.front().se.se;
    int cost = que.front().fi;
    que.pop();
    if (dist[i][j] != cost)
      continue;
    rep(k, 0, 4) {
      int ni = i + dy[k];
      int nj = j + dx[k];
      if (ni < 0 || H <= ni || nj < 0 || W <= nj)
        continue;
      if (dist[ni][nj] <= cost + 1)
        continue;
      dist[ni][nj] = cost + 1;
      que.push({cost + 1, {ni, nj}});
    }
  }
  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      // cout<<dist[i][j]<<endl;
      chmax(ans, dist[i][j]);
    }
  }
  cout << ans << endl;
}