Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl

const ll INF = 1e9 + 5, N = 1e3 + 5, mod = 1e9 + 7, M = 1e6 + 5;

char a[N][N];

int main() {
  speed;
  int n, m;
  cin >> n >> m;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  int d[N][N];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      d[i][j] = INF;
    }
  }
  set<pair<int, pair<int, int>>> q;
  d[x1][y1] = 0;
  q.insert({0, {x1, y1}});
  while (q.size()) {
    int v1 = q.begin()->second.first, v2 = q.begin()->second.second;
    q.erase(q.begin());
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (i != 0 && j != 0)
          continue;
        if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
          continue;
        if (a[v1 + i][v2 + j] == '#')
          continue;
        if (d[v1][v2] < d[v1 + i][v2 + j]) {
          q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
          d[v1 + i][v2 + j] = d[v1][v2];
          q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
          continue;
        if (a[v1 + i][v2 + j] == '#')
          continue;
        if (d[v1][v2] + 1 < d[v1 + i][v2 + j]) {
          q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
          d[v1 + i][v2 + j] = d[v1][v2] + 1;
          q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
        }
      }
    }
  }
  if (d[x2][y2] == INF)
    d[x2][y2] = -1;
  cout << d[x2][y2] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define bug cout << "ok" << endl

const ll INF = 1e9 + 5, N = 1e2 + 5, mod = 1e9 + 7, M = 1e6 + 5;

char a[N][N];

int main() {
  speed;
  int n, m;
  cin >> n >> m;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  int d[N][N];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      d[i][j] = INF;
    }
  }
  set<pair<int, pair<int, int>>> q;
  d[x1][y1] = 0;
  q.insert({0, {x1, y1}});
  while (q.size()) {
    int v1 = q.begin()->second.first, v2 = q.begin()->second.second;
    q.erase(q.begin());
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (i != 0 && j != 0)
          continue;
        if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
          continue;
        if (a[v1 + i][v2 + j] == '#')
          continue;
        if (d[v1][v2] < d[v1 + i][v2 + j]) {
          q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
          d[v1 + i][v2 + j] = d[v1][v2];
          q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (v1 + i < 1 || v1 + i > n || v2 + j < 1 || v2 + j > m)
          continue;
        if (a[v1 + i][v2 + j] == '#')
          continue;
        if (d[v1][v2] + 1 < d[v1 + i][v2 + j]) {
          q.erase({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
          d[v1 + i][v2 + j] = d[v1][v2] + 1;
          q.insert({d[v1 + i][v2 + j], {v1 + i, v2 + j}});
        }
      }
    }
  }
  if (d[x2][y2] == INF)
    d[x2][y2] = -1;
  cout << d[x2][y2] << endl;
}