Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
// #include <intrin.h>

#define fore(i, a, b) for (int i = a; i < (b); i++)
#define fr(i, a, b) for (int i = a - 1; i >= b; i--)
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define N 300005
#define inf 2000000000
#define MOD (ll)1e15
#define __builtin_popcount(x) __popcnt(x)

using namespace std;

typedef long long ll;
typedef pair<int, int> pr;
typedef pair<pair<int, int>, int> tup;

inline void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<char>> g(n, vector<char>(m));
  fore(i, 0, n) fore(j, 0, m) { cin >> g[i][j]; }

  int otv = 0;

  fore(i, 0, n) {
    fore(j, 0, m) {
      fore(k, 0, n) {
        fore(h, 0, m) {
          if (g[i][j] != '#' && g[k][h] != '#') {
            vector<vector<bool>> used(n, vector<bool>(m, 0));
            queue<tup> q;
            q.push({{i, j}, 0});

            while (q.size()) {
              tup v = q.front();
              q.pop();

              if (v.first.first < 0 || v.first.first >= n ||
                  v.first.second < 0 || v.first.second >= m ||
                  used[v.first.first][v.first.second] ||
                  g[v.first.first][v.first.second] == '#') {
                continue;
              }

              if (v.first.first == k && v.first.second == h) {
                otv = max(otv, v.second);
                break;
              }

              q.push({{v.first.first + 1, v.first.second}, v.second + 1});
              q.push({{v.first.first - 1, v.first.second}, v.second + 1});
              q.push({{v.first.first, v.first.second + 1}, v.second + 1});
              q.push({{v.first.first, v.first.second - 1}, v.second + 1});
            }
          }
        }
      }
    }
  }
  cout << otv;
}

int main() {
  fast;
#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
// #include <intrin.h>

#define fore(i, a, b) for (int i = a; i < (b); i++)
#define fr(i, a, b) for (int i = a - 1; i >= b; i--)
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define N 300005
#define inf 2000000000
#define MOD (ll)1e15
#define __builtin_popcount(x) __popcnt(x)

using namespace std;

typedef long long ll;
typedef pair<int, int> pr;
typedef pair<pair<int, int>, int> tup;

inline void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<char>> g(n, vector<char>(m));
  fore(i, 0, n) fore(j, 0, m) { cin >> g[i][j]; }

  int otv = 0;

  fore(i, 0, n) {
    fore(j, 0, m) {
      fore(k, 0, n) {
        fore(h, 0, m) {
          if (g[i][j] != '#' && g[k][h] != '#') {
            vector<vector<bool>> used(n, vector<bool>(m, 0));
            queue<tup> q;
            q.push({{i, j}, 0});

            while (q.size()) {
              tup v = q.front();
              q.pop();

              if (v.first.first < 0 || v.first.first >= n ||
                  v.first.second < 0 || v.first.second >= m ||
                  used[v.first.first][v.first.second] ||
                  g[v.first.first][v.first.second] == '#') {
                continue;
              }
              used[v.first.first][v.first.second] = 1;

              if (v.first.first == k && v.first.second == h) {
                otv = max(otv, v.second);
                break;
              }

              q.push({{v.first.first + 1, v.first.second}, v.second + 1});
              q.push({{v.first.first - 1, v.first.second}, v.second + 1});
              q.push({{v.first.first, v.first.second + 1}, v.second + 1});
              q.push({{v.first.first, v.first.second - 1}, v.second + 1});
            }
          }
        }
      }
    }
  }
  cout << otv;
}

int main() {
  fast;
#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  solve();
}