Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout << x << endl, 0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 1e5 + 50;
const int mod = 1e9 + 7;
using namespace std;
int n, m, i, j, t, p, mx, viz[25][25], dx[5], dy[5];
char c[25][25];
queue<pair<pair<int, int>, int>> q;
pair<pair<int, int>, int> x;
bool ok(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m && !viz[x][y] && c[x][y] == '.')
    return 1;
  return 0;
}
int main() {
  // freopen("sol.in","r",stdin);
  // freopen("sol.out","w",stdout);
  // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  dx[0] = 1, dy[0] = 0, dx[1] = -1, dy[1] = 0, dx[2] = 0, dy[2] = 1, dx[3] = 0,
  dy[3] = -1;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (c[i][j] == '#')
        continue;
      for (t = 1; t <= n; t++)
        for (p = 1; p <= m; p++)
          viz[t][p] = 0;
      q.push(mkp(mkp(i, j), 0));
      viz[i][j] = 1;
      while (!q.empty()) {
        x = q.front();
        q.pop();
        mx = max(mx, x.sc);
        for (t = 0; t < 4; t++) {
          if (ok(x.fr.fr + dx[t], x.fr.sc + dy[t])) {
            q.push(mkp(mkp(x.fr.fr + dx[t], x.fr.sc + dy[t]), x.sc + 1));
            viz[x.fr.fr + dx[t]][x.fr.sc + dy[t]] = 1;
          }
        }
      }
    }
  }
  cout << mx << endl;
  return 0;
}


Fixed submission:
// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout << x << endl, 0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 1e5 + 50;
const int mod = 1e9 + 7;
using namespace std;
int n, m, i, j, t, p, mx, viz[25][25], dx[5], dy[5];
char c[25][25];
queue<pair<pair<int, int>, int>> q;
pair<pair<int, int>, int> x;
bool ok(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= m && !viz[x][y] && c[x][y] == '.')
    return 1;
  return 0;
}
int main() {
  // freopen("sol.in","r",stdin);
  // freopen("sol.out","w",stdout);
  // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  dx[0] = 1, dy[0] = 0, dx[1] = -1, dy[1] = 0, dx[2] = 0, dy[2] = 1, dx[3] = 0,
  dy[3] = -1;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (c[i][j] == '#')
        continue;
      for (t = 1; t <= n; t++)
        for (p = 1; p <= m; p++)
          viz[t][p] = 0;
      q.push(mkp(mkp(i, j), 0));
      viz[i][j] = 1;
      while (!q.empty()) {
        x = q.front();
        q.pop();
        mx = max(mx, x.sc);
        for (t = 0; t < 4; t++) {
          if (ok(x.fr.fr + dx[t], x.fr.sc + dy[t])) {
            q.push(mkp(mkp(x.fr.fr + dx[t], x.fr.sc + dy[t]), x.sc + 1));
            viz[x.fr.fr + dx[t]][x.fr.sc + dy[t]] = 1;
          }
        }
      }
    }
  }
  cout << mx << endl;
  return 0;
}