Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;
using vl = vector<ll>;

const ll MOD = 1e9 + 7;
const ll INF = 1e14;
const long double EPS = 1e-11;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (int i = (l); i < (r); i++)
#define reper(i, l, r) for (int i = (r)-1; i >= (l); i--)

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//

int main() {
  init();
  ll H, W;
  cin >> H >> W;
  vc<string> G(H);
  rep(i, H) cin >> G[i];
  vl dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};

  ll ans = 0;
  queue<pair<ll, ll>> q;
  vvc<ll> road(H, vc<ll>(W, INF));

  rep(i, H) rep(j, W) {
    if (G[i][j] == '#')
      continue;
    vvc<ll> dist;
    dist = road;
    dist[i][j] = 0;

    q.emplace(i, j);
    while (!q.empty()) {
      ll x, y;
      tie(x, y) = q.front();
      q.pop();

      rep(d, 4) {
        ll nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 || nx > H - 1 || ny < 0 || ny > W - 1)
          continue;
        if (G[nx][ny] == '#' || dist[nx][ny] <= dist[x][y])
          continue;

        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }

    ll tmp = 0;
    for (auto R : dist)
      for (auto r : R)
        if (r != INF)
          tmp = max(tmp, r);
    ans = max(ans, tmp);
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;
using vl = vector<ll>;

const ll MOD = 1e9 + 7;
const ll INF = 1e14;
const long double EPS = 1e-11;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (int i = (l); i < (r); i++)
#define reper(i, l, r) for (int i = (r)-1; i >= (l); i--)

void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
//--------------------------------------------------------------------------------//

int main() {
  init();
  ll H, W;
  cin >> H >> W;
  vc<string> G(H);
  rep(i, H) cin >> G[i];
  vl dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};

  ll ans = 0;
  queue<pair<ll, ll>> q;
  vvc<ll> road(H, vc<ll>(W, INF));

  rep(i, H) rep(j, W) {
    if (G[i][j] == '#')
      continue;
    vvc<ll> dist;
    dist = road;
    dist[i][j] = 0;

    q.emplace(i, j);
    while (!q.empty()) {
      ll x, y;
      tie(x, y) = q.front();
      q.pop();

      rep(d, 4) {
        ll nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 || nx > H - 1 || ny < 0 || ny > W - 1)
          continue;
        if (G[nx][ny] == '#' || dist[nx][ny] <= dist[x][y] + 1)
          continue;

        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }

    ll tmp = 0;
    for (auto R : dist)
      for (auto r : R)
        if (r != INF)
          tmp = max(tmp, r);
    ans = max(ans, tmp);
  }

  cout << ans << endl;
  return 0;
}