Problem p03725 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
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;
}

using ll = long long;
using P = pair<ll, ll>;

constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

class CClosedRooms {
public:
  void solve(istream &cin, ostream &cout) {
    SPEED;
    int h, w, k;
    cin >> h >> w >> k;
    char mat[h][w];
    vector<vector<int>> cost(h, vector<int>(w, INT_MAX / 2));
    int start = -1;
    deque<P> que;
    rep(i, h) rep(j, w) {
      cin >> mat[i][j];
      if (mat[i][j] == 'S') {
        que.emplace_back(i * w + j, 0);
        cost[i][j] = 0;
      }
    }
    while (!que.empty()) {
      auto p = que.front();
      que.pop_front();
      int i = p.first % w;
      int j = p.first / w;
      int c = p.second;
      if (c > cost[i][j])
        continue;

      if (i > 0 && mat[i - 1][j] == '.') {
        if (cost[i - 1][j] > c + 1) {
          cost[i - 1][j] = c + 1;
          que.emplace_back((i - 1) * w + j, c + 1);
        }
      }
      if (i < h - 1 && mat[i + 1][j] == '.') {
        if (cost[i + 1][j] > c + 1) {
          cost[i + 1][j] = c + 1;
          que.emplace_back((i + 1) * w + j, c + 1);
        }
      }
      if (j > 0 && mat[i][j - 1] == '.') {
        if (cost[i][j - 1] > c + 1) {
          cost[i][j - 1] = c + 1;
          que.emplace_back(i * w + j - 1, c + 1);
        }
      }
      if (j < w - 1 && mat[i][j + 1] == '.') {
        if (cost[i][j + 1] > c + 1) {
          cost[i][j + 1] = c + 1;
          que.emplace_back(i * w + j + 1, c + 1);
        }
      }
    }

    int distance = INT_MAX;
    rep(i, h) rep(j, w) {
      if (cost[i][j] <= k) {
        auto imin = min(i, h - 1 - i);
        auto jmin = min(j, w - 1 - j);
        chmin(distance, min(imin, jmin));
      }
    }

    cout << (distance + k - 1) / k + 1 << endl;
  }
};

signed main() {
  CClosedRooms solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
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;
}

using ll = long long;
using P = pair<ll, ll>;

constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

class CClosedRooms {
public:
  void solve(istream &cin, ostream &cout) {
    SPEED;
    int h, w, k;
    cin >> h >> w >> k;
    char mat[h][w];
    vector<vector<int>> cost(h, vector<int>(w, INT_MAX / 2));
    int start = -1;
    deque<P> que;
    rep(i, h) rep(j, w) {
      cin >> mat[i][j];
      if (mat[i][j] == 'S') {
        que.emplace_back(i * w + j, 0);
        cost[i][j] = 0;
      }
    }
    while (!que.empty()) {
      auto p = que.front();
      que.pop_front();
      int i = p.first / w;
      int j = p.first % w;
      int c = p.second;
      if (c > cost[i][j])
        continue;

      if (i > 0 && mat[i - 1][j] == '.') {
        if (cost[i - 1][j] > c + 1) {
          cost[i - 1][j] = c + 1;
          que.emplace_back((i - 1) * w + j, c + 1);
        }
      }
      if (i < h - 1 && mat[i + 1][j] == '.') {
        if (cost[i + 1][j] > c + 1) {
          cost[i + 1][j] = c + 1;
          que.emplace_back((i + 1) * w + j, c + 1);
        }
      }
      if (j > 0 && mat[i][j - 1] == '.') {
        if (cost[i][j - 1] > c + 1) {
          cost[i][j - 1] = c + 1;
          que.emplace_back(i * w + j - 1, c + 1);
        }
      }
      if (j < w - 1 && mat[i][j + 1] == '.') {
        if (cost[i][j + 1] > c + 1) {
          cost[i][j + 1] = c + 1;
          que.emplace_back(i * w + j + 1, c + 1);
        }
      }
    }

    int distance = INT_MAX;
    rep(i, h) rep(j, w) {
      if (cost[i][j] <= k) {
        auto imin = min(i, h - 1 - i);
        auto jmin = min(j, w - 1 - j);
        chmin(distance, min(imin, jmin));
      }
    }

    cout << (distance + k - 1) / k + 1 << endl;
  }
};

signed main() {
  CClosedRooms solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}