Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h> //JuniorMonster a.k.a Sho10
#define ll long long int
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout << s, 0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define CODE_START                                                             \
  ios_base::sync_with_stdio();                                                 \
  cin.tie();                                                                   \
  cout.tie();
using namespace std;
ll n, m, ans = 0, dist[21][21];
char a[21][21];
bool viz[21][21];
vector<pair<ll, ll>> v;
int32_t main() {
  CODE_START;
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '.') {
        v.pb(mp(i, j));
      }
    }
  for (ll i = 0; i < v.size(); i++)
    for (ll j = i + 1; j < v.size(); j++) {
      queue<ll> q1;
      queue<ll> q2;
      q1.push(v[i].f);
      q2.push(v[i].s);
      for (ll c = 0; c < v.size(); c++) {
        viz[v[c].f][v[c].s] = 0;
      }
      dist[v[i].f][v[i].s] = 0;
      while (!q1.empty()) {
        ll x, y;
        x = q1.front();
        y = q2.front();
        viz[x][y] = 1;
        if (x == v[j].f && y == v[j].s) {
          break;
        }
        if (x - 1 >= 0 && a[x - 1][y] == '.' && viz[x - 1][y] == 0) {
          q1.push(x - 1);
          q2.push(y);
          dist[x - 1][y] = dist[x][y] + 1;
        }
        if (x + 1 < n && a[x + 1][y] == '.' && viz[x + 1][y] == 0) {
          q1.push(x + 1);
          q2.push(y);
          dist[x + 1][y] = dist[x][y] + 1;
        }
        if (y - 1 >= 0 && a[x][y - 1] == '.' && viz[x][y - 1] == 0) {
          q1.push(x);
          q2.push(y - 1);
          dist[x][y - 1] = dist[x][y] + 1;
        }
        if (y + 1 < m && a[x][y + 1] == '.' && viz[x][y + 1] == 0) {
          q1.push(x);
          q2.push(y + 1);
          dist[x][y + 1] = dist[x][y] + 1;
        }
        q1.pop();
        q2.pop();
      }
      ans = max(ans, dist[v[j].f][v[j].s]);
    }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h> //JuniorMonster a.k.a Sho10
#define ll long long int
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout << s, 0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define CODE_START                                                             \
  ios_base::sync_with_stdio();                                                 \
  cin.tie();                                                                   \
  cout.tie();
using namespace std;
ll n, m, ans = 0, dist[21][21];
char a[21][21];
bool viz[21][21];
vector<pair<ll, ll>> v;
int32_t main() {
  CODE_START;
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == '.') {
        v.pb(mp(i, j));
      }
    }
  if (v.size() == n * m) {
    cout << n + m - 2 << endl;
    return 0;
  }
  for (ll i = 0; i < v.size(); i++)
    for (ll j = i + 1; j < v.size(); j++) {
      queue<ll> q1;
      queue<ll> q2;
      q1.push(v[i].f);
      q2.push(v[i].s);
      for (ll c = 0; c < v.size(); c++) {
        viz[v[c].f][v[c].s] = 0;
      }
      dist[v[i].f][v[i].s] = 0;
      while (!q1.empty()) {
        ll x, y;
        x = q1.front();
        y = q2.front();
        viz[x][y] = 1;
        if (x == v[j].f && y == v[j].s) {
          break;
        }
        if (x - 1 >= 0 && a[x - 1][y] == '.' && viz[x - 1][y] == 0) {
          q1.push(x - 1);
          q2.push(y);
          dist[x - 1][y] = dist[x][y] + 1;
        }
        if (x + 1 < n && a[x + 1][y] == '.' && viz[x + 1][y] == 0) {
          q1.push(x + 1);
          q2.push(y);
          dist[x + 1][y] = dist[x][y] + 1;
        }
        if (y - 1 >= 0 && a[x][y - 1] == '.' && viz[x][y - 1] == 0) {
          q1.push(x);
          q2.push(y - 1);
          dist[x][y - 1] = dist[x][y] + 1;
        }
        if (y + 1 < m && a[x][y + 1] == '.' && viz[x][y + 1] == 0) {
          q1.push(x);
          q2.push(y + 1);
          dist[x][y + 1] = dist[x][y] + 1;
        }
        q1.pop();
        q2.pop();
      }
      ans = max(ans, dist[v[j].f][v[j].s]);
    }
  cout << ans << endl;
}