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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
int main() {
  ll h, w;
  cin >> h >> w;
  ll dx[4] = {1, 0, -1, 0};
  ll dy[4] = {0, 1, 0, -1};
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  char s[1002][1003];
  ll ans[1002][1002];
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> s[i][j];
      ans[i][j] = -1;
    }
  }
  queue<pair<ll, ll>> q, r;
  q.push(make_pair(ch, cw));
  ans[ch][cw] = 0;
  while (q.size() != 0) {
    while (q.size() != 0) {
      pair<ll, ll> now = q.front();
      q.pop();
      ll nowh = now.first;
      ll noww = now.second;
      r.push(make_pair(nowh, noww));
      for (ll i = 0; i < 4; i++) {
        ll nexh = nowh + dx[i];
        ll nexw = noww + dy[i];
        if (nexh < 0 || nexw < 0 || nexh == h || nexw == w) {
          continue;
        }
        if (s[nexh][nexw] == '#') {
          continue;
        }
        if (ans[nexh][nexw] != -1) {
          continue;
        }
        ans[nexh][nexw] = ans[nowh][noww];
        q.push(make_pair(nexh, nexw));
        r.push(make_pair(nexh, nexw));
      }
    }
    while (r.size() != 0) {
      pair<ll, ll> now = r.front();
      r.pop();
      ll nowh = now.first;
      ll noww = now.second;
      for (ll i = -2; i < 3; i++) {
        for (ll j = -2; j < 3; j++) {
          if (i == 0 && j == 0) {
            continue;
          }
          ll nexh = nowh + i;
          ll nexw = noww + j;
          if (nexh < 0 || nexw < 0 || nexh >= h || nexw >= w) {
            continue;
          }
          if (s[nexh][nexw] == '#') {
            continue;
          }
          if (ans[nexh][nexw] != -1) {
            continue;
          }
          ans[nexh][nexw] = ans[nowh][noww] + 1;
          if (nexh == dh && nexw == dw) {
            cout << ans[nexh][nexw];
            return 0;
          }
          q.push(make_pair(nexh, nexw));
        }
      }
    }
  }
  cout << ans[dh][dw] << endl;
  // your code goes here
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007
int main() {
  ll h, w;
  cin >> h >> w;
  ll dx[4] = {1, 0, -1, 0};
  ll dy[4] = {0, 1, 0, -1};
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  char s[1002][1003];
  ll ans[1002][1002];
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      cin >> s[i][j];
      ans[i][j] = -1;
    }
  }
  queue<pair<ll, ll>> q, r;
  q.push(make_pair(ch, cw));
  bool ne = true;
  ans[ch][cw] = 0;
  r.push(make_pair(ch, cw));
  while (q.size() != 0) {
    while (q.size() != 0) {
      pair<ll, ll> now = q.front();
      q.pop();
      ll nowh = now.first;
      ll noww = now.second;
      for (ll i = 0; i < 4; i++) {
        ll nexh = nowh + dx[i];
        ll nexw = noww + dy[i];
        if (nexh < 0 || nexw < 0 || nexh == h || nexw == w) {
          continue;
        }
        if (s[nexh][nexw] == '#') {
          continue;
        }
        if (ans[nexh][nexw] != -1) {
          continue;
        }
        ans[nexh][nexw] = ans[nowh][noww];
        q.push(make_pair(nexh, nexw));
        r.push(make_pair(nexh, nexw));
      }
    }
    while (r.size() != 0) {
      pair<ll, ll> now = r.front();
      r.pop();
      ll nowh = now.first;
      ll noww = now.second;
      for (ll i = -2; i < 3; i++) {
        for (ll j = -2; j < 3; j++) {
          if (i == 0 && j == 0) {
            continue;
          }
          ll nexh = nowh + i;
          ll nexw = noww + j;
          if (nexh < 0 || nexw < 0 || nexh == h || nexw == w) {
            continue;
          }
          if (s[nexh][nexw] == '#') {
            continue;
          }
          if (ans[nexh][nexw] != -1) {
            continue;
          }
          ans[nexh][nexw] = ans[nowh][noww] + 1;
          q.push(make_pair(nexh, nexw));
        }
      }
    }
  }
  cout << ans[dh][dw] << endl;
  // your code goes here
  return 0;
}