Problem p02803 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;
const int mod = 1000000007;

int bfs(int c, int r);
vector<vector<int>> M(20, vector<int>(20, 0));
int H, W;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main() {

  cin >> H >> W;
  vector<pair<int, int>> P;
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == '.') {
        M[i][j] = 1;
        P.push_back(make_pair(i, j));
      }
    }
  }

  /*
  for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
          cout << M[i][j] << ' ';
      }
      cout << '\n';
  }
  */

  int ans = 0;
  for (int i = 0; i < P.size(); i++) {
    int dist = bfs(P[i].first, P[i].second);
    ans = max(dist, ans);
  }
  cout << ans << '\n';
}

int bfs(int c, int r) {

  vector<vector<int>> visited(20, vector<int>(20, -1));

  queue<pair<int, int>> q;
  q.push(make_pair(c, r));
  visited[c][r] = 0;

  int next_c, next_r;
  int max_dist = 0;
  pair<int, int> point;
  while (!q.empty()) {

    point = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      next_c = point.first + dx[i];
      next_r = point.second + dy[i];

      if (next_c < 0 || H < next_c || next_r < 0 || W < next_r) {
        continue;
      }
      if (visited[next_c][next_r] != -1)
        continue;
      if (M[next_c][next_r] == 0)
        continue;

      visited[next_c][next_r] = visited[point.first][point.second] + 1;
      q.push(make_pair(next_c, next_r));
      max_dist = max(visited[next_c][next_r], max_dist);
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      max_dist = max(visited[i][j], max_dist);
      //     cout << visited[i][j] << ' ';
    }
    // cout << '\n';
  }
  // cout << "max_dist : " << max_dist << '\n';

  return max_dist;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;
const int mod = 1000000007;

int bfs(int c, int r);
vector<vector<int>> M(20, vector<int>(20, 0));
int H, W;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};

int main() {

  cin >> H >> W;
  vector<pair<int, int>> P;
  for (int i = 0; i < H; i++) {
    string S;
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == '.') {
        M[i][j] = 1;
        P.push_back(make_pair(i, j));
      }
    }
  }

  /*
  for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
          cout << M[i][j] << ' ';
      }
      cout << '\n';
  }
  */

  int ans = 0;
  for (int i = 0; i < P.size(); i++) {
    int dist = bfs(P[i].first, P[i].second);
    ans = max(dist, ans);
  }
  cout << ans << '\n';
}

int bfs(int c, int r) {

  vector<vector<int>> visited(20, vector<int>(20, -1));

  queue<pair<int, int>> q;
  q.push(make_pair(c, r));
  visited[c][r] = 0;

  int next_c, next_r;
  int max_dist = 0;
  pair<int, int> point;
  while (!q.empty()) {

    point = q.front();
    q.pop();

    for (int i = 0; i < 4; i++) {
      next_c = point.first + dx[i];
      next_r = point.second + dy[i];

      if (next_c < 0 || H <= next_c || next_r < 0 || W <= next_r) {
        continue;
      }
      if (visited[next_c][next_r] != -1)
        continue;
      if (M[next_c][next_r] == 0)
        continue;

      visited[next_c][next_r] = visited[point.first][point.second] + 1;
      q.push(make_pair(next_c, next_r));
      max_dist = max(visited[next_c][next_r], max_dist);
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      max_dist = max(visited[i][j], max_dist);
      //     cout << visited[i][j] << ' ';
    }
    // cout << '\n';
  }
  // cout << "max_dist : " << max_dist << '\n';

  return max_dist;
}