Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
using namespace std;
const int inf = 1e18 + 7;
const int N = 1e6 + 7;
const int md = 1e9 + 7;
int dist[900], vis[900];
char a[30][30];
int n, m;
int val[30][30];
std::vector<int> g[30];
int ok(int i, int j) {
  if ((i >= 0 && i < n) && (j >= 0 && j < m)) {
    if (a[i][j] == '.')
      return 1;
  }
  return 0;
}
void bfs(int src) {
  std::queue<int> q;
  int from;
  q.push(src);
  vis[src] = 1;
  dist[src] = 0;
  while (!q.empty()) {
    from = q.front();
    q.pop();
    for (auto &to : g[from]) {
      if (vis[to])
        continue;
      q.push(to);
      vis[to] = 1;
      dist[to] = dist[from] + 1;
    }
  }
}
void sol() {
  int nodes = 0;
  int ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.') {
        val[i][j] = nodes++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.') {
        if (ok(i + 1, j)) {
          g[val[i][j]].push_back(val[i + 1][j]);
        }
        if (ok(i, j - 1)) {
          g[val[i][j]].push_back(val[i][j - 1]);
        }
        if (ok(i - 1, j)) {
          g[val[i][j]].push_back(val[i - 1][j]);
        }
        if (ok(i, j + 1)) {
          g[val[i][j]].push_back(val[i][j + 1]);
        }
      }
    }
  }
  // dfs(0, 0);
  for (int i = 0; i < nodes; i++) {
    memset(dist, 0, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    bfs(i);
    for (int j = 0; j < nodes; j++) {
      ans = max(ans, dist[j]);
    }
  }
  cout << ans << "\n";
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin >> T; while(T--)
  sol();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
using namespace std;
const int inf = 1e18 + 7;
const int N = 1e6 + 7;
const int md = 1e9 + 7;
int dist[900], vis[900];
char a[30][30];
int n, m;
int val[30][30];
std::vector<int> g[900];
int ok(int i, int j) {
  if ((i >= 0 && i < n) && (j >= 0 && j < m)) {
    if (a[i][j] == '.')
      return 1;
  }
  return 0;
}
void bfs(int src) {
  std::queue<int> q;
  int from;
  q.push(src);
  vis[src] = 1;
  dist[src] = 0;
  while (!q.empty()) {
    from = q.front();
    q.pop();
    for (auto &to : g[from]) {
      if (vis[to])
        continue;
      q.push(to);
      vis[to] = 1;
      dist[to] = dist[from] + 1;
    }
  }
}
void sol() {
  int nodes = 0;
  int ans = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.') {
        val[i][j] = nodes++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.') {
        if (ok(i + 1, j)) {
          g[val[i][j]].push_back(val[i + 1][j]);
        }
        if (ok(i, j - 1)) {
          g[val[i][j]].push_back(val[i][j - 1]);
        }
        if (ok(i - 1, j)) {
          g[val[i][j]].push_back(val[i - 1][j]);
        }
        if (ok(i, j + 1)) {
          g[val[i][j]].push_back(val[i][j + 1]);
        }
      }
    }
  }
  // dfs(0, 0);
  for (int i = 0; i < nodes; i++) {
    memset(dist, 0, sizeof(dist));
    memset(vis, 0, sizeof(vis));
    bfs(i);
    for (int j = 0; j < nodes; j++) {
      ans = max(ans, dist[j]);
    }
  }
  cout << ans << "\n";
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  // cin >> T; while(T--)
  sol();
  return 0;
}