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

#include <iostream>
using namespace std;
#include <set>
#include <utility>
#include <vector>
#define ll long long
vector<pair<ll, ll>> v[1000001];
ll dist[1000001];
char c[1001][1001];
int main() {
  ll n, m;
  cin >> n >> m;
  ll l, r;
  cin >> l >> r;
  ll s = ((l - 1) * m) + r;
  cin >> l >> r;
  ll e = ((l - 1) * m) + r;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (c[i][j] == '.') {
        for (ll k = i - 2; k <= i + 2; k++) {
          for (ll h = j - 2; h <= j + 2; h++) {
            if (k >= 1 && k <= n && h >= 1 && h <= m) {
              if (c[k][h] == '.') {
                if (abs(i - k) + abs(j - h) == 1)
                  v[(i - 1) * m + j].push_back({(k - 1) * m + h, 0});
                else if (abs(i - k) + abs(j - h) > 1)
                  v[(i - 1) * m + j].push_back({(k - 1) * m + h, 1});
              }
            }
          }
        }
      }
    }
  }

  for (ll i = 0; i <= n * m; i++)
    dist[i] = 1e18;
  dist[s] = 0;
  set<pair<ll, ll>> pq;
  pair<ll, ll> p, x;
  pq.insert({s, 0});
  while (!pq.empty()) {
    p = *pq.begin();
    pq.erase(pq.begin());
    if (dist[p.first] != p.second)
      continue;
    for (auto it : v[p.first]) {
      if (dist[it.first] > dist[p.first] + it.second) {
        dist[it.first] = dist[p.first] + it.second;
        pq.insert({it.first, dist[it.first]});
      }
    }
  }
  if (dist[e] == 1e18)
    cout << -1 << endl;
  else
    cout << dist[e] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#include <set>
#include <utility>
#include <vector>
#define ll long long
vector<pair<ll, ll>> v[1000001];
char c[1001][1001];
int main() {
  ll n, m;
  cin >> n >> m;
  ll l, r;
  cin >> l >> r;
  ll s = l * (r - 1) + r;
  cin >> l >> r;
  ll e = l * (r - 1) + r;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      cin >> c[i][j];
    }
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= m; j++) {
      if (c[i][j] == '.') {
        for (ll k = i - 2; k <= i + 2; k++) {
          for (ll h = j - 2; h <= j + 2; h++) {
            if (k >= 1 && k <= n && h >= 1 && h <= m) {
              if (c[k][h] == '.') {
                if (abs(i - k) + abs(j - h) == 1)
                  v[i * (j - 1) + j].push_back({k * (h - 1) + h, 0});
                else if (abs(i - k) + abs(j - h) > 1)
                  v[i * (j - 1) + j].push_back({k * (h - 1) + h, 1});
              }
            }
          }
        }
      }
    }
  }
  ll dist[n * m + 1];
  for (ll i = 0; i <= n * m; i++)
    dist[i] = 1e18;
  dist[s] = 0;
  set<pair<ll, ll>> pq;
  pair<ll, ll> p, x;
  pq.insert({s, 0});
  while (!pq.empty()) {
    p = *pq.begin();
    pq.erase(pq.begin());
    if (dist[p.first] != p.second)
      continue;
    for (auto it : v[p.first]) {

      if (dist[it.first] > dist[p.first] + it.second) {
        dist[it.first] = dist[p.first] + it.second;
        pq.insert({it.first, dist[it.first]});
      }
    }
  }
  if (dist[e] == 1e18)
    cout << -1 << endl;
  else
    cout << dist[e] << endl;
  return 0;
}