Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
#define PI (acos(-1))
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------
const ll MX = 2020, MY = 2020;
bool map2d[MX][MY]; // 行けるならtrue
ll depth[MX][MY];
ll dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};

ll bfs(ll sx, ll sy) {
  if (!map2d[sx][sy])
    return 0;
  rep(i, MX) rep(j, MY) depth[i][j] = 0; // 初期化する
  if (depth[sx][sy])
    return 0;
  deque<P> dq;
  dq.emplace_back(sx, sy);
  depth[sx][sy] = 1; // departure: depth=1
  while (!dq.empty()) {
    ll vx = dq.front().first, vy = dq.front().second;
    dq.pop_front();
    rep(i, 4) {
      ll x = vx + dx[i], y = vy + dy[i];
      if (x < 0 or x >= MX or y < 0 or y >= MY)
        continue;
      if (!map2d[x][y])
        continue;
      if (depth[x][y] != 0)
        continue;
      depth[x][y] = depth[vx][vy] + 1;
      dq.emplace_back(x, y);
    }
  }
  ll ct = 0;
  rep(i, MX) rep(j, MY) maxi(ct, depth[i][j]);
  return ct - 1; // 行けたノード数を返す、深さなどでも可
}
//--MAIN-----------------------------------
void Main() {
  ll H, W;
  cin >> H >> W;
  rep(i, H) {
    rep(j, W) {
      char c;
      cin >> c;
      if (c == '.')
        map2d[i][j] = 1;
    }
  }
  ll ans = 0;
  rep(i, MX) rep(j, MY) maxi(ans, bfs(i, j));
  cout << ans << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------


Fixed submission:
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
#define PI (acos(-1))
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------
const ll MX = 20, MY = 20;
bool map2d[MX][MY]; // 行けるならtrue
ll depth[MX][MY];
ll dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};

ll bfs(ll sx, ll sy) {
  if (!map2d[sx][sy])
    return 0;
  rep(i, MX) rep(j, MY) depth[i][j] = 0; // 初期化する
  if (depth[sx][sy])
    return 0;
  deque<P> dq;
  dq.emplace_back(sx, sy);
  depth[sx][sy] = 1; // departure: depth=1
  while (!dq.empty()) {
    ll vx = dq.front().first, vy = dq.front().second;
    dq.pop_front();
    rep(i, 4) {
      ll x = vx + dx[i], y = vy + dy[i];
      if (x < 0 or x >= MX or y < 0 or y >= MY)
        continue;
      if (!map2d[x][y])
        continue;
      if (depth[x][y] != 0)
        continue;
      depth[x][y] = depth[vx][vy] + 1;
      dq.emplace_back(x, y);
    }
  }
  ll ct = 0;
  rep(i, MX) rep(j, MY) maxi(ct, depth[i][j]);
  return ct - 1; // 行けたノード数を返す、深さなどでも可
}
//--MAIN-----------------------------------
void Main() {
  ll H, W;
  cin >> H >> W;
  rep(i, H) {
    rep(j, W) {
      char c;
      cin >> c;
      if (c == '.')
        map2d[i][j] = 1;
    }
  }
  ll ans = 0;
  rep(i, MX) rep(j, MY) maxi(ans, bfs(i, j));
  cout << ans << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------