Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7

#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

#define MAX_N 301
#define INF 10000000000001
long long d[MAX_N][MAX_N];
long long N;

void warshall_floyd() {
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main() {
  long long H;
  long long W;
  scanf("%lld", &H);
  vector<string> S(H - 1 + 1);
  scanf("%lld", &W);
  for (int i = 0; i <= H - 1; i++) {
    cin >> S[i];
  }

  for (int i = 0; i <= MAX_N - 1; i++) {
    for (int j = 0; j <= MAX_N - 1; j++) {
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  }
  rep(i, H) {
    rep(j, W) {
      if (i > 0 && S[i][j] == '.' && S[i - 1][j] == '.') {
        ll f = i * W + j, t = (i - 1) * W + j;
        d[f][t] = 1;
        d[t][f] = 1;
      }
      if (j > 0 && S[i][j] == '.' && S[i][j - 1] == '.') {
        ll f = i * W + j, t = i * W + j - 1;
        d[f][t] = 1;
        d[t][f] = 1;
      }
      if (i < H - 1 && S[i][j] == '.' && S[i + 1][j] == '.') {
        ll f = i * W + j, t = (i + 1) * W + j;
        d[f][t] = 1;
        d[t][f] = 1;
      }
      if (j < W - 1 && S[i][j] == '.' && S[i][j + 1] == '.') {
        ll f = i * W + j, t = i * W + j + 1;
        d[f][t] = 1;
        d[t][f] = 1;
      }
    }
  }
  N = H * W;
  warshall_floyd();
  ll res = 0;
  rep(i, H * W) {
    rep(j, H * W) {
      // cout << i/W << " " << i%W << "  " << j/W << " " << j%W << "  " <<
      // d[i][j] << endl;
      if (d[i][j] < INF)
        res = max(res, d[i][j]);
    }
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7

#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

#define MAX_N 401
#define INF 10000000000001
long long d[MAX_N][MAX_N];
long long N;

void warshall_floyd() {
  for (int k = 0; k < N; k++)
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main() {
  long long H;
  long long W;
  scanf("%lld", &H);
  vector<string> S(H - 1 + 1);
  scanf("%lld", &W);
  for (int i = 0; i <= H - 1; i++) {
    cin >> S[i];
  }

  for (int i = 0; i <= MAX_N - 1; i++) {
    for (int j = 0; j <= MAX_N - 1; j++) {
      if (i == j)
        d[i][j] = 0;
      else
        d[i][j] = INF;
    }
  }
  rep(i, H) {
    rep(j, W) {
      if (i > 0 && S[i][j] == '.' && S[i - 1][j] == '.') {
        ll f = i * W + j, t = (i - 1) * W + j;
        d[f][t] = 1;
        d[t][f] = 1;
      }
      if (j > 0 && S[i][j] == '.' && S[i][j - 1] == '.') {
        ll f = i * W + j, t = i * W + j - 1;
        d[f][t] = 1;
        d[t][f] = 1;
      }
      if (i < H - 1 && S[i][j] == '.' && S[i + 1][j] == '.') {
        ll f = i * W + j, t = (i + 1) * W + j;
        d[f][t] = 1;
        d[t][f] = 1;
      }
      if (j < W - 1 && S[i][j] == '.' && S[i][j + 1] == '.') {
        ll f = i * W + j, t = i * W + j + 1;
        d[f][t] = 1;
        d[t][f] = 1;
      }
    }
  }
  N = H * W;
  warshall_floyd();
  ll res = 0;
  rep(i, H * W) {
    rep(j, H * W) {
      // cout << i/W << " " << i%W << "  " << j/W << " " << j%W << "  " <<
      // d[i][j] << endl;
      if (d[i][j] < INF)
        res = max(res, d[i][j]);
    }
  }
  cout << res << endl;
  return 0;
}