Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> ta(n + 2, vector<int>(m + 2, 1));
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < m; j++) {
      if (str[j] == '.') {
        ta[i + 1][j + 1] = 0;
      } else {
        ta[i + 1][j + 1] = 1;
      }
    }
  }
  int res = 0;
  vector<vector<int>> hou = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x = i + 1, y = j + 1;
      if (ta[x][y] == 1)
        continue;
      vector<vector<bool>> ju(n + 2, vector<bool>(m + 2));
      ju[x][y] = true;
      vector<vector<int>> dist(n + 2, vector<int>(m + 2, 1 << 29));
      dist[x][y] = 0;
      stack<pair<int, int>> st;
      st.push(make_pair(x, y));
      while (st.size()) {
        pair<int, int> p0 = st.top();
        st.pop();
        int cx = p0.first, cy = p0.second;
        int d = dist[cx][cy];
        for (vector<int> &h : hou) {
          int nx = cx + h[0], ny = cy + h[1];
          if (ta[nx][ny] == 1)
            continue;
          if (ju[nx][ny] == 1)
            continue;
          if (dist[nx][ny] < (d + 1))
            continue;
          dist[nx][ny] = d + 1;
          st.push(make_pair(nx, ny));
        }
      }
      for (int ii = 0; ii < n; ii++) {
        for (int jj = 0; jj < m; jj++) {
          if (dist[ii + 1][jj + 1] > (1 << 20))
            continue;
          res = max(res, dist[ii + 1][jj + 1]);
        }
      }
    }
  }
  cout << res << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

  int n, m;
  cin >> n >> m;
  vector<vector<int>> ta(n + 2, vector<int>(m + 2, 1));
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < m; j++) {
      if (str[j] == '.') {
        ta[i + 1][j + 1] = 0;
      } else {
        ta[i + 1][j + 1] = 1;
      }
    }
  }
  int res = 0;
  vector<vector<int>> hou = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int x = i + 1, y = j + 1;
      if (ta[x][y] == 1)
        continue;
      vector<vector<bool>> ju(n + 2, vector<bool>(m + 2));
      ju[x][y] = true;
      vector<vector<int>> dist(n + 2, vector<int>(m + 2, 1 << 29));
      dist[x][y] = 0;
      stack<pair<int, int>> st;
      st.push(make_pair(x, y));
      while (st.size()) {
        pair<int, int> p0 = st.top();
        st.pop();
        int cx = p0.first, cy = p0.second;
        int d = dist[cx][cy];
        for (vector<int> &h : hou) {
          int nx = cx + h[0], ny = cy + h[1];
          if (ta[nx][ny] == 1)
            continue;
          if (ju[nx][ny] == 1)
            continue;
          if (dist[nx][ny] <= (d + 1))
            continue;
          dist[nx][ny] = d + 1;
          st.push(make_pair(nx, ny));
        }
      }
      for (int ii = 0; ii < n; ii++) {
        for (int jj = 0; jj < m; jj++) {
          if (dist[ii + 1][jj + 1] > (1 << 20))
            continue;
          res = max(res, dist[ii + 1][jj + 1]);
        }
      }
    }
  }
  cout << res << "\n";
  return 0;
}