Problem p03725 (C++)

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

#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define MP make_pair
#define test cout << "test" << endl;
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e18;
const ll zero = 0;
using P = pair<ll, ll>;
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void pmod(ll &a, ll b) { a = (a + b) % MOD; }
void pmod(ll &a, ll b, ll c) { a = (b + c) % MOD; }
void qmod(ll &a, ll b) { a = (a * b) % MOD; }
void qmod(ll &a, ll b, ll c) { a = (b * c) % MOD; }
void ans1(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void ans2(bool x) {
  if (x)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void ans3(bool x) {
  if (x)
    cout << "Yay!" << endl;
  else
    cout << ":(" << endl;
}
void ans(bool x, ll y, ll z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
void ans(bool x, string y, string z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
ll gcd(ll x, ll y) {
  ll r;
  while ((r = x % y) != 0) {
    x = y;
    y = r;
  }
  return y;
}

struct z {
  ll x;
  ll y;
  ll l;
};

int main() {
  ll i, j, o;
  ll res = 0, res1 = INF, res2 = -INF, buf = 0;
  bool judge = true;
  ll h, w, k;
  cin >> h >> w >> k;
  ll a[h][w];
  ll buf1, buf2;
  for (i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (j = 0; j < w; j++) {
      if (s[j] == '#')
        a[i][j] = 0;
      else if (s[i] == '.')
        a[i][j] = 1;
      else {
        a[i][j] = 1;
        buf1 = i;
        buf2 = j;
      }
    }
  }
  queue<struct z> p;
  vector<vector<ll>> d(h, vector<ll>(w, 0));
  p.push({buf1, buf2, zero});
  while (!p.empty()) {
    ll x = p.front().x;
    ll y = p.front().y;
    ll l = p.front().l;
    if (l == k + 1)
      break;
    if (d[x][y] == 0 && a[x][y] == 1) {
      if (x == 0 || x == h - 1 || y == 0 || y == w - 1) {
        res1 = 1;
        break;
      }
      d[x][y] = 1;
      if (x <= h - 2)
        p.push({x + 1, y, l + 1});
      if (y <= w - 2)
        p.push({x, y + 1, l + 1});
      if (x >= 1)
        p.push({x - 1, y, l + 1});
      if (y >= 1)
        p.push({x, y - 1, l + 1});
      chmin(res1, (min(min(h - 1 - x, x), min(w - 1 - y, y)) + k - 1) / k + 1);
    }
    p.pop();
  }
  cout << res1 << endl;

  return 0;
}

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

#define ALL(a) (a).begin(), (a).end()
#define ALLR(a) (a).rbegin(), (a).rend()
#define spa << " " <<
#define MP make_pair
#define test cout << "test" << endl;
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e18;
const ll zero = 0;
using P = pair<ll, ll>;
void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
void chmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
void pmod(ll &a, ll b) { a = (a + b) % MOD; }
void pmod(ll &a, ll b, ll c) { a = (b + c) % MOD; }
void qmod(ll &a, ll b) { a = (a * b) % MOD; }
void qmod(ll &a, ll b, ll c) { a = (b * c) % MOD; }
void ans1(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
void ans2(bool x) {
  if (x)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void ans3(bool x) {
  if (x)
    cout << "Yay!" << endl;
  else
    cout << ":(" << endl;
}
void ans(bool x, ll y, ll z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
void ans(bool x, string y, string z) {
  if (x)
    cout << y << endl;
  else
    cout << z << endl;
}
ll gcd(ll x, ll y) {
  ll r;
  while ((r = x % y) != 0) {
    x = y;
    y = r;
  }
  return y;
}

struct z {
  ll x;
  ll y;
  ll l;
};

int main() {
  ll i, j, o;
  ll res = 0, res1 = INF, res2 = -INF, buf = 0;
  bool judge = true;
  ll h, w, k;
  cin >> h >> w >> k;
  ll a[h][w];
  ll buf1, buf2;
  for (i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (j = 0; j < w; j++) {
      if (s[j] == '#')
        a[i][j] = 0;
      else if (s[j] == '.')
        a[i][j] = 1;
      else {
        a[i][j] = 1;
        buf1 = i;
        buf2 = j;
      }
    }
  }
  queue<struct z> p;
  vector<vector<ll>> d(h, vector<ll>(w, 0));
  p.push({buf1, buf2, zero});
  while (!p.empty()) {
    ll x = p.front().x;
    ll y = p.front().y;
    ll l = p.front().l;
    if (l == k + 1)
      break;
    if (d[x][y] == 0 && a[x][y] == 1) {
      if (x == 0 || x == h - 1 || y == 0 || y == w - 1) {
        res1 = 1;
        break;
      }
      d[x][y] = 1;
      if (x <= h - 2)
        p.push({x + 1, y, l + 1});
      if (y <= w - 2)
        p.push({x, y + 1, l + 1});
      if (x >= 1)
        p.push({x - 1, y, l + 1});
      if (y >= 1)
        p.push({x, y - 1, l + 1});
      chmin(res1, (min(min(h - 1 - x, x), min(w - 1 - y, y)) + k - 1) / k + 1);
    }
    p.pop();
  }
  cout << res1 << endl;

  return 0;
}