Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 999999999999;
int dh[] = {-1, 0, 0, 1};
int dw[] = {0, -1, 1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        continue;
      }
      vector<vector<bool>> done(h, vector<bool>(w, false));
      queue<pair<int, P>> q;
      q.push(make_pair(0, P(i, j)));
      while (!q.empty()) {
        pair<int, P> p = q.front();
        q.pop();
        int cost = p.first;
        // cout<<cost<<" ";
        ans = max(ans, cost);
        int u = p.second.first;
        int v = p.second.second;
        done[u][v] = true;
        for (int d = 0; d < 4; d++) {
          if (u + dh[d] >= 0 && u + dh[d] < h && v + dw[d] >= 0 &&
              v + dw[d] < w && !done[u + dh[d]][v + dw[d]] &&
              s[u + dh[d]][v + dw[d]] == '.') {
            q.push(make_pair(cost + 1, P(u + dh[d], v + dw[d])));
          }
        }
      }
      // cout<<endl;
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 999999999999;
int dh[] = {-1, 0, 0, 1};
int dw[] = {0, -1, 1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  for (int i = 0; i < h; i++)
    cin >> s[i];

  int ans = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        continue;
      }
      vector<vector<bool>> done(h, vector<bool>(w, false));
      queue<pair<int, P>> q;
      q.push(make_pair(0, P(i, j)));
      while (!q.empty()) {
        pair<int, P> p = q.front();
        q.pop();
        int cost = p.first;
        // cout<<cost<<" ";
        ans = max(ans, cost);
        int u = p.second.first;
        int v = p.second.second;
        done[u][v] = true;
        for (int d = 0; d < 4; d++) {
          if (u + dh[d] >= 0 && u + dh[d] < h && v + dw[d] >= 0 &&
              v + dw[d] < w && !done[u + dh[d]][v + dw[d]] &&
              s[u + dh[d]][v + dw[d]] == '.') {
            done[u + dh[d]][v + dw[d]] = true;
            q.push(make_pair(cost + 1, P(u + dh[d], v + dw[d])));
          }
        }
      }
      // cout<<endl;
    }
  }

  cout << ans << endl;
}