Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define SIZE 25
ll H, W;
string arr[SIZE];
ll vis[SIZE][SIZE];
ll bfs(ll a, ll b, ll x, ll y) {
  queue<pair<pll, ll>> q;
  q.push(mp(mp(a, b), 0));
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      vis[i][j] = false;
      if (arr[i][j] == '#') {
        vis[i][j] = true;
      }
    }
  }
  vis[a][b] = true;
  ll ans = -1;
  while (!q.empty()) {
    pair<pll, ll> u = q.front();
    q.pop();
    ll i = u.first.first;
    ll j = u.first.second;
    ll d = u.second;
    // cout<<i<<" "<<j<<" "<<d<<endl;
    if (i == x && j == y) {
      ans = d;
      // cout<<ans<<endl;
      break;
    }
    if (i + 1 < H && vis[i + 1][j] == false) {
      vis[i + 1][j] = true;
      q.push(mp(mp(i + 1, j), d + 1));
    }
    if (j + 1 < W && vis[i][j + 1] == false) {
      vis[i][j + 1] = true;
      q.push(mp(mp(i, j + 1), d + 1));
    }
    if (i - 1 >= 0 && vis[i - 1][j] == false) {
      vis[i - 1][j] = true;
      q.push(mp(mp(i - 1, j), d + 1));
    }
    if (j - 1 >= 0 && vis[i][j - 1] == false) {
      vis[i][j - 1] = true;
      q.push(mp(mp(i, j - 1), d + 1));
    }
  }
  return ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> H >> W;
  for (ll i = 0; i < H; ++i) {
    string str;
    cin >> str;
    arr[i] = str;
  }
  ll ans = 0;
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      for (ll k = 0; k < H; ++k) {
        for (ll l = 0; l < W; ++l) {
          if (arr[i][j] != '#' && arr[k][l] != '#') {
            ans = max(ans, bfs(i, j, k, l));
            // cout<<i<<","<<j<<" "<<k<<","<<l<<" "<<ans<<endl;
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define SIZE 25
ll H, W;
string arr[SIZE];
ll vis[SIZE][SIZE];
ll bfs(ll a, ll b, ll x, ll y) {
  queue<pair<pll, ll>> q;
  q.push(mp(mp(a, b), 0));
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      vis[i][j] = false;
      if (arr[i][j] == '#') {
        vis[i][j] = true;
      }
    }
  }
  vis[a][b] = true;
  ll ans = -1;
  while (!q.empty()) {
    pair<pll, ll> u = q.front();
    q.pop();
    ll i = u.first.first;
    ll j = u.first.second;
    ll d = u.second;
    // cout<<i<<" "<<j<<" "<<d<<endl;
    if (i == x && j == y) {
      ans = d;
      // cout<<ans<<endl;
      break;
    }
    if (i + 1 < H && vis[i + 1][j] == false) {
      vis[i + 1][j] = true;
      q.push(mp(mp(i + 1, j), d + 1));
    }
    if (j + 1 < W && vis[i][j + 1] == false) {
      vis[i][j + 1] = true;
      q.push(mp(mp(i, j + 1), d + 1));
    }
    if (i - 1 >= 0 && vis[i - 1][j] == false) {
      vis[i - 1][j] = true;
      q.push(mp(mp(i - 1, j), d + 1));
    }
    if (j - 1 >= 0 && vis[i][j - 1] == false) {
      vis[i][j - 1] = true;
      q.push(mp(mp(i, j - 1), d + 1));
    }
  }
  return ans;
}
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> H >> W;
  for (ll i = 0; i < H; ++i) {
    string str;
    cin >> str;
    arr[i] = str;
  }
  ll ans = 0;
  for (ll i = 0; i < H; ++i) {
    for (ll j = 0; j < W; ++j) {
      for (ll k = 0; k < H; ++k) {
        for (ll l = 0; l < W; ++l) {
          if (arr[i][j] != '#' && arr[k][l] != '#') {
            ans = max(ans, bfs(i, j, k, l));
            // cout<<i<<","<<j<<" "<<k<<","<<l<<" "<<ans<<endl;
          }
        }
      }
    }
  }
  cout << ans << endl;
}