Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
confirm 0LL and 1LL
confirm cornercases such as 0
confirm times of cin < 10^6
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<double>;
using VecS = vector<string>;
using VecVec = vector<Vec>;
using Tree = vector<VecP>;
template <typename T> using Vec1 = vector<T>;
template <typename T> using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i))
#define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i))
#define REPR(i, m, n) for (ll(i) = (m)-1; (i) >= (n); --(i))
#define REPNR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> void co(const T n) { cout << n << endl; }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void coVec(const Vec &v) {
  for (ll i : v)
    cosp(i);
  cout << endl;
}
void sonic() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;

ll h, w;
VecS s;
VecVec dist;

void init() { rep(i, h) fill(all(dist[i]), LINF); }

void wfs(ll x, ll y) {
  queue<P> que;
  que.push({x * w + y, 0});
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    ll x1 = p.fs / w, y1 = p.fs % w;
    dist[x1][y1] = p.sc;
    if (x1 - 1 >= 0 && s[x1 - 1][y1] == '.' && dist[x1 - 1][y1] == LINF)
      que.push({(x1 - 1) * w + y1, p.sc + 1});
    if (y1 - 1 >= 0 && s[x1][y1 - 1] == '.' && dist[x1][y1 - 1] == LINF)
      que.push({x1 * w + y1 - 1, p.sc + 1});
    if (x1 + 1 < h && s[x1 + 1][y1] == '.' && dist[x1 + 1][y1] == LINF)
      que.push({(x1 + 1) * w + y1, p.sc + 1});
    if (y1 + 1 < w && s[x1][y1 + 1] == '.' && dist[x1][y1 + 1] == LINF)
      que.push({x1 * w + y1 + 1, p.sc + 1});
  }
}

ll maxDist() {
  ll res = 0;
  rep(i, h) { rep(j, w) if (dist[i][j] != LINF) chmax(res, dist[i][j]); }
  return res;
}

int main(void) {
  cin >> h >> w;
  s.resize(h);
  rep(i, h) cin >> s[i];
  dist.resize(h);
  rep(i, h) dist[i].resize(w);

  ll ans = 0;
  rep(i, h) {
    rep(j, w) if (s[i][j] == '.') {
      init();
      wfs(i, j);
      chmax(ans, maxDist());
    }
  }
  co(ans);

  return 0;
}


Fixed submission:
/*
confirm 0LL and 1LL
confirm cornercases such as 0
confirm times of cin < 10^6
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<double>;
using VecS = vector<string>;
using VecVec = vector<Vec>;
using Tree = vector<VecP>;
template <typename T> using Vec1 = vector<T>;
template <typename T> using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for (ll(i) = (m); (i) < (n); ++(i))
#define REPN(i, m, n) for (ll(i) = (m); (i) <= (n); ++(i))
#define REPR(i, m, n) for (ll(i) = (m)-1; (i) >= (n); --(i))
#define REPNR(i, m, n) for (ll(i) = (m); (i) >= (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> void co(const T n) { cout << n << endl; }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void coVec(const Vec &v) {
  for (ll i : v)
    cosp(i);
  cout << endl;
}
void sonic() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;

ll h, w;
VecS s;
VecVec dist;

void init() { rep(i, h) fill(all(dist[i]), LINF); }

void wfs(ll x, ll y) {
  queue<P> que;
  que.push({x * w + y, 0});
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    ll x1 = p.fs / w, y1 = p.fs % w;
    dist[x1][y1] = p.sc;
    if (x1 - 1 >= 0 && s[x1 - 1][y1] == '.' && dist[x1 - 1][y1] == LINF)
      que.push({(x1 - 1) * w + y1, p.sc + 1});
    if (y1 - 1 >= 0 && s[x1][y1 - 1] == '.' && dist[x1][y1 - 1] == LINF)
      que.push({x1 * w + y1 - 1, p.sc + 1});
    if (x1 + 1 < h && s[x1 + 1][y1] == '.' && dist[x1 + 1][y1] == LINF)
      que.push({(x1 + 1) * w + y1, p.sc + 1});
    if (y1 + 1 < w && s[x1][y1 + 1] == '.' && dist[x1][y1 + 1] == LINF)
      que.push({x1 * w + y1 + 1, p.sc + 1});
  }
}

ll maxDist() {
  ll res = 0;
  rep(i, h) { rep(j, w) if (dist[i][j] != LINF) chmax(res, dist[i][j]); }
  return res;
}

int main(void) {
  cin >> h >> w;
  s.resize(h);
  rep(i, h) cin >> s[i];
  dist.resize(h);
  rep(i, h) dist[i].resize(w);
  bool flg = true;
  rep(i, h) rep(j, w) if (s[i][j] == '#') {
    flg = false;
    break;
  }
  if (flg) {
    co(w + h - 2);
    return 0;
  }

  ll ans = 0;
  rep(i, h) {
    rep(j, w) if (s[i][j] == '.') {
      init();
      wfs(i, j);
      chmax(ans, maxDist());
    }
  }
  co(ans);

  return 0;
}