Problem p02803 (C++)

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

using namespace std;

typedef long long tint;

const tint INF = 2e14 + 1;
const long double PI = 3.141592;
const int MOD = 998244353;

#define forsn(i, s, n) for (int i = s; i < int(n); i++)
#define forn(i, n) forsn(i, 0, n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define NACHO                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};

bool esValido(int n, int m, int x, int y) {
  return (0 <= x && x < n && 0 <= y && y < m);
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  forn(i, n) cin >> a[i];
  int maxi = 0;
  forn(i, n) {
    forn(j, n) {
      if (a[i][j] == '#')
        continue;
      vector<vector<int>> distance(n, vector<int>(m));
      distance[i][j] = 0;
      vector<vector<bool>> visited(n, vector<bool>(m, 0));
      visited[i][j] = 1;
      queue<pair<int, int>> q;
      q.push({i, j});
      while (!q.empty()) {
        auto s = q.front();
        q.pop();
        forn(k, 4) {
          int x = s.first + dx[k];
          int y = s.second + dy[k];
          if (esValido(n, m, x, y) && !visited[x][y] && a[x][y] != '#') {
            visited[x][y] = 1;
            distance[x][y] = distance[s.first][s.second] + 1;
            maxi = max(maxi, distance[x][y]);
            q.push({x, y});
          }
        }
      }
    }
  }
  cout << maxi << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long tint;

const tint INF = 2e14 + 1;
const long double PI = 3.141592;
const int MOD = 998244353;

#define forsn(i, s, n) for (int i = s; i < int(n); i++)
#define forn(i, n) forsn(i, 0, n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define NACHO                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};

bool esValido(int n, int m, int x, int y) {
  return (0 <= x && x < n && 0 <= y && y < m);
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<string> a(n);
  forn(i, n) cin >> a[i];
  int maxi = 0;
  forn(i, n) {
    forn(j, m) {
      if (a[i][j] == '#')
        continue;
      vector<vector<int>> distance(n, vector<int>(m));
      distance[i][j] = 0;
      vector<vector<bool>> visited(n, vector<bool>(m, 0));
      visited[i][j] = 1;
      queue<pair<int, int>> q;
      q.push({i, j});
      while (!q.empty()) {
        auto s = q.front();
        q.pop();
        forn(k, 4) {
          int x = s.first + dx[k];
          int y = s.second + dy[k];
          if (esValido(n, m, x, y) && !visited[x][y] && a[x][y] != '#') {
            visited[x][y] = 1;
            distance[x][y] = distance[s.first][s.second] + 1;
            maxi = max(maxi, distance[x][y]);
            q.push({x, y});
          }
        }
      }
    }
  }
  cout << maxi << "\n";
}