Problem p02803 (C++)

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)
#define mp make_pair

int32 di[4] = {0, 1, 0, -1};
int32 dj[4] = {1, 0, -1, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w;
  cin >> h >> w;
  vector<string> s(h);
  REP(i, h) cin >> s[i];
  vector<vector<int32>> dist;
  int32 ans = 0;
  REP(si, h) REP(sj, h) {
    queue<pii> que;
    que.emplace(si, sj);
    dist = vector<vector<int32>>(h, vector<int32>(w, INF));
    dist[si][sj] = 0;
    while (!que.empty()) {
      int32 ci, cj;
      tie(ci, cj) = que.front();
      que.pop();
      REP(i, 4) {
        int32 ni = ci + di[i];
        int32 nj = cj + dj[i];
        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] > dist[ci][cj] + 1) {
          dist[ni][nj] = dist[ci][cj] + 1;
          que.emplace(ni, nj);
        }
      }
    }
    REP(i, h) REP(j, w) {
      if (dist[i][j] != INF && dist[i][j] > ans) {
        ans = dist[i][j];
      }
    }
  }
  ANS(ans);
  return 0;
}

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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9 + 7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define pii pair<int32, int32>
#define pll pair<int64, int64>
#define pb(a) push_back(a)
#define mp make_pair

int32 di[4] = {0, 1, 0, -1};
int32 dj[4] = {1, 0, -1, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w;
  cin >> h >> w;
  vector<string> s(h);
  REP(i, h) cin >> s[i];
  vector<vector<int32>> dist;
  int32 ans = 0;
  REP(si, h) REP(sj, w) {
    if (s[si][sj] == '#')
      continue;
    queue<pii> que;
    que.emplace(si, sj);
    dist = vector<vector<int32>>(h, vector<int32>(w, INF));
    dist[si][sj] = 0;
    while (!que.empty()) {
      int32 ci, cj;
      tie(ci, cj) = que.front();
      que.pop();
      REP(i, 4) {
        int32 ni = ci + di[i];
        int32 nj = cj + dj[i];
        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] > dist[ci][cj] + 1) {
          dist[ni][nj] = dist[ci][cj] + 1;
          que.emplace(ni, nj);
        }
      }
    }
    REP(i, h) REP(j, w) {
      if (dist[i][j] != INF && dist[i][j] > ans) {
        ans = dist[i][j];
      }
    }
  }
  ANS(ans);
  return 0;
}