Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx,
        int gy) {
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  int d[N][M]; // 距離の配列
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      d[i][j] = INF; // 初期化
    }
  }
  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  // キューが空になるまでループ
  while (que.size()) {
    P p = que.front();
    que.pop(); // 先頭を取り出す
    if (p.fs == gx && p.sc == gy) {
      break; // 探索終わり
    }
    for (int i = 0; i < 4; i++) {
      int nx = p.fs + dx[i], ny = p.sc + dy[i];
      // nx,nyが進めるマスか、行ったことのないマスかどうかを判定
      if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));           // 追加
        d[nx][ny] = d[p.fs][p.sc] + 1; // 距離を確定
      }
    }
  }
  return d[gx][gy];
}

int main() {
  int h, w, i, j;

  cin >> h;
  cin >> w;
  vector<vector<char>> field(h, vector<char>(w));
  int res = 0;
  int sx, sy, gx, gy;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> field[i][j];
    }
  }
  int ans = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (field[i][j] == '.') {
        sx = i;
        sy = j;
      }
      for (int k = 0; k < h; k++) {
        for (int l = 0; l < w; l++) {
          if (field[k][l] == '.') {
            gx = k;
            gy = l;
            ans = max(ans, bfs(h, w, field, sx, sy, gx, gy));
          }
        }
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx,
        int gy) {
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  int d[N][M]; // 距離の配列
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      d[i][j] = INF; // 初期化
    }
  }
  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  // キューが空になるまでループ
  while (que.size()) {
    P p = que.front();
    que.pop(); // 先頭を取り出す
    if (p.fs == gx && p.sc == gy) {
      break; // 探索終わり
    }
    for (int i = 0; i < 4; i++) {
      int nx = p.fs + dx[i], ny = p.sc + dy[i];
      // nx,nyが進めるマスか、行ったことのないマスかどうかを判定
      if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));           // 追加
        d[nx][ny] = d[p.fs][p.sc] + 1; // 距離を確定
      }
    }
  }
  return d[gx][gy];
}

int main() {
  int h, w, i, j;

  cin >> h;
  cin >> w;
  vector<vector<char>> field(h, vector<char>(w));
  int res = 0;
  int sx, sy, gx, gy;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      cin >> field[i][j];
    }
  }
  int ans = 0;
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {
      if (field[i][j] == '.') {
        sx = i;
        sy = j;
      }
      for (int k = 0; k < h; k++) {
        for (int l = 0; l < w; l++) {
          if (field[k][l] == '.' && field[i][j] == '.') {
            gx = k;
            gy = l;
            ans = max(ans, bfs(h, w, field, sx, sy, gx, gy));
          }
        }
      }
    }
  }

  cout << ans << endl;
}