Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define fout(x) cout << fixed << setprecision(10) << x << endl
// #define int long long int
const int MOD = 1000000007;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 0.000000001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

auto print = [](auto &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
};

int bfs(vector<vector<char>> &s, int i, int j, int h, int w) {
  queue<tuple<int, int, int>> q;
  vector<vector<bool>> vis(h, vector<bool>(w, false));
  q.push(make_tuple(j, i, 0));
  int ret = 0;
  vis[i][j] = true;
  while (!q.empty()) {
    int x, y, d;
    tie(x, y, d) = q.front();
    q.pop();
    ret = max(ret, d);
    rep(k, 4) {
      if (x + dx[k] < 0 || x + dx[k] > w - 1)
        continue;
      if (y + dy[k] < 0 || y + dy[k] > h - 1)
        continue;
      if (s[y + dy[k]][x + dx[k]] == '#')
        continue;
      if (vis[y + dy[k]][x + dx[k]])
        continue;
      q.push(make_tuple(x + dx[k], y + dy[k], d + 1));
      vis[y][x] = true;
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ans = 0;
  int h, w;
  cin >> h >> w;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];
  rep(i, h) rep(j, w) {
    if (s[i][j] == '#')
      continue;
    ans = max(ans, bfs(s, i, j, h, w));
  }
  out(ans);
  return 0;
}


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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define fout(x) cout << fixed << setprecision(10) << x << endl
// #define int long long int
const int MOD = 1000000007;
const ll LINF = (ll)1e18 - 1;
const int INF = 1e9 - 1;
const double EPS = 0.000000001;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

auto print = [](auto &v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
};

int bfs(vector<vector<char>> &s, int i, int j, int h, int w) {
  queue<tuple<int, int, int>> q;
  vector<vector<bool>> vis(h, vector<bool>(w, false));
  q.push(make_tuple(j, i, 0));
  int ret = 0;
  vis[i][j] = true;
  while (!q.empty()) {
    int x, y, d;
    tie(x, y, d) = q.front();
    q.pop();
    ret = max(ret, d);
    rep(k, 4) {
      if (x + dx[k] < 0 || x + dx[k] > w - 1)
        continue;
      if (y + dy[k] < 0 || y + dy[k] > h - 1)
        continue;
      if (s[y + dy[k]][x + dx[k]] == '#')
        continue;
      if (vis[y + dy[k]][x + dx[k]])
        continue;
      q.push(make_tuple(x + dx[k], y + dy[k], d + 1));
      vis[y + dy[k]][x + dx[k]] = true;
    }
  }
  return ret;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ans = 0;
  int h, w;
  cin >> h >> w;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];
  rep(i, h) rep(j, w) {
    if (s[i][j] == '#')
      continue;
    ans = max(ans, bfs(s, i, j, h, w));
  }
  out(ans);
  return 0;
}