Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
    Mbak Sana Dong
    Mbak Chaeyoung Dong
*/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int r, c;
char grid[202][202];
int dist[22][22];
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  cin >> r >> c;
  getchar();
  vector<pair<int, int>> vi;
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      scanf("%c", &grid[i][j]);
      vi.push_back({i, j});
    }
    getchar();
  }
  int sz = vi.size();
  int chaeng = 0;
  for (i = 0; i < sz; i++) {
    x = vi[i].first;
    y = vi[i].second;
    if (grid[x][y] == '#')
      continue;
    for (j = i + 1; j < sz; j++) {
      int endx = vi[j].first;
      int endy = vi[j].second;
      if (grid[endx][endy] == '#')
        continue;
      for (int a = 1; a <= r; a++)
        for (int b = 1; b <= c; b++)
          dist[a][b] = 1e9;
      set<pair<int, pair<int, int>>> s;
      s.insert({0, {x, y}});
      dist[x][y] = 0;
      while (!s.empty()) {
        int now = (*s.begin()).first;
        int xx = (*s.begin()).second.first;
        int yy = (*s.begin()).second.second;
        s.erase(s.begin());
        if (xx + 1 <= r) {
          if (grid[xx + 1][yy] == '.') {
            if (dist[xx + 1][yy] > now + 1) {
              dist[xx + 1][yy] = now + 1;
              s.insert({now + 1, {xx + 1, yy}});
            }
          }
        }
        if (xx - 1 > 0) {
          if (grid[xx - 1][yy] == '.') {
            if (dist[xx - 1][yy] > now + 1) {
              dist[xx - 1][yy] = now + 1;
              s.insert({now + 1, {xx - 1, yy}});
            }
          }
        }

        if (yy + 1 <= c) {
          if (grid[xx][yy + 1] == '.') {
            if (dist[xx][yy + 1] > now + 1) {
              dist[xx][yy + 1] = now + 1;
              s.insert({now + 1, {xx, yy + 1}});
            }
          }
        }
        if (yy - 1 > 0) {
          if (grid[xx][yy - 1] == '.') {
            if (dist[xx][yy - 1] > now + 1) {
              dist[xx][yy - 1] = now + 1;
              s.insert({now + 1, {xx, yy - 1}});
            }
          }
        }
      }
      if (dist[endx][endy] != 1e9) {
        chaeng = max(chaeng, dist[endx][endy]);
      }
    }
  }
  cout << chaeng << endl;
  return 0;
}


Fixed submission:
/*
    Mbak Sana Dong
    Mbak Chaeyoung Dong
*/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int r, c;
char grid[202][202];
int dist[22][22];
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  cin >> r >> c;
  getchar();
  vector<pair<int, int>> vi;
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      scanf("%c", &grid[i][j]);
      vi.push_back({i, j});
    }
    getchar();
  }
  int sz = vi.size();
  int chaeng = 0;
  for (i = 0; i < sz; i++) {
    x = vi[i].first;
    y = vi[i].second;
    if (grid[x][y] == '#')
      continue;
    for (j = i + 1; j < sz; j++) {
      int endx = vi[j].first;
      int endy = vi[j].second;
      if (grid[endx][endy] == '#')
        continue;
      for (int a = 1; a <= r; a++)
        for (int b = 1; b <= c; b++)
          dist[a][b] = 1e9;
      set<pair<int, pair<int, int>>> s;
      s.insert({0, {x, y}});
      dist[x][y] = 0;
      while (!s.empty()) {
        if (dist[endx][endy] < 1e9)
          break;
        int now = (*s.begin()).first;
        int xx = (*s.begin()).second.first;
        int yy = (*s.begin()).second.second;
        s.erase(s.begin());
        if (xx + 1 <= r) {
          if (grid[xx + 1][yy] == '.') {
            if (dist[xx + 1][yy] > now + 1) {
              dist[xx + 1][yy] = now + 1;
              s.insert({now + 1, {xx + 1, yy}});
            }
          }
        }
        if (xx - 1 > 0) {
          if (grid[xx - 1][yy] == '.') {
            if (dist[xx - 1][yy] > now + 1) {
              dist[xx - 1][yy] = now + 1;
              s.insert({now + 1, {xx - 1, yy}});
            }
          }
        }

        if (yy + 1 <= c) {
          if (grid[xx][yy + 1] == '.') {
            if (dist[xx][yy + 1] > now + 1) {
              dist[xx][yy + 1] = now + 1;
              s.insert({now + 1, {xx, yy + 1}});
            }
          }
        }
        if (yy - 1 > 0) {
          if (grid[xx][yy - 1] == '.') {
            if (dist[xx][yy - 1] > now + 1) {
              dist[xx][yy - 1] = now + 1;
              s.insert({now + 1, {xx, yy - 1}});
            }
          }
        }
      }
      if (dist[endx][endy] != 1e9) {
        chaeng = max(chaeng, dist[endx][endy]);
      }
    }
  }
  cout << chaeng << endl;
  return 0;
}