Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

// in-short-use macros

#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007

// container-use

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpl;
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define rep(i, a, b) for (int i = a; i <= b; i++)

// main function here
int a[25][25] = {0};
int n, m;
int d[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int bfs(int i, int j) {
  queue<pair<int, int>> q;
  int c = 0;
  int vis[25][25] = {0}, dis[25][25] = {0};
  q.push({i, j});
  vis[i][j] = 1;
  while (!q.empty()) {
    pair<int, int> el = q.front();
    int x = el.ff, y = el.ss;
    for (int f = 0; f < 4; f++) {
      int newdx = d[f][0] + x, newdy = d[f][1] + y;
      if (newdx < 0 || newdx >= n || newdy < 0 || newdy >= m)
        continue;
      if (!vis[newdx][newdy] && a[newdx][newdy] != -1) {
        vis[newdx][newdy] = 1;
        dis[newdx][newdy] = dis[x][y] + 1;
        // cout<<newdx<<" "<<newdy<<" "<<dis[newdx][newdy]<<"\n";
        c = max(c, dis[newdx][newdy]);
        q.push({newdx, newdy});
      }
    }
    q.pop();
  }
  return c;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("C:/Users/Saurav Kumar Singh/Desktop/cp/input.txt", "r", stdin);
  freopen("C:/Users/Saurav Kumar Singh/Desktop/cp/output.txt", "w", stdout);
#endif
  cin >> n >> m;
  int q = 0;
  for (int i = 0; i < n; i++) {
    string c;
    cin >> c;
    for (int j = 0; j < m; j++) {
      if (c[j] == '#')
        a[i][j] = -1;
    }
  }
  // cout<<bfs(2,0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == -1)
        continue;
      // cout<<bfs(i,j)<<"\n";
      q = max(q, bfs(i, j));
    }
  }
  cout << q << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// in-short-use macros

#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007

// container-use

typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpl;
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeof((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define rep(i, a, b) for (int i = a; i <= b; i++)

// main function here
int a[25][25] = {0};
int n, m;
int d[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int bfs(int i, int j) {
  queue<pair<int, int>> q;
  int c = 0;
  int vis[25][25] = {0}, dis[25][25] = {0};
  q.push({i, j});
  vis[i][j] = 1;
  while (!q.empty()) {
    pair<int, int> el = q.front();
    int x = el.ff, y = el.ss;
    for (int f = 0; f < 4; f++) {
      int newdx = d[f][0] + x, newdy = d[f][1] + y;
      if (newdx < 0 || newdx >= n || newdy < 0 || newdy >= m)
        continue;
      if (!vis[newdx][newdy] && a[newdx][newdy] != -1) {
        vis[newdx][newdy] = 1;
        dis[newdx][newdy] = dis[x][y] + 1;
        // cout<<newdx<<" "<<newdy<<" "<<dis[newdx][newdy]<<"\n";
        c = max(c, dis[newdx][newdy]);
        q.push({newdx, newdy});
      }
    }
    q.pop();
  }
  return c;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  int q = 0;
  for (int i = 0; i < n; i++) {
    string c;
    cin >> c;
    for (int j = 0; j < m; j++) {
      if (c[j] == '#')
        a[i][j] = -1;
    }
  }
  // cout<<bfs(2,0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == -1)
        continue;
      // cout<<bfs(i,j)<<"\n";
      q = max(q, bfs(i, j));
    }
  }
  cout << q << "\n";
  return 0;
}