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

#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define feach(it, l) for (auto it = l.begin(); it != l.end(); ++it)
#define fall(a) a.begin(), a.end()
#define sz(x) (int)x.size()
#define szs(x) (int)s.length()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef queue<int> qin;
typedef deque<ll> dqll;
typedef deque<int> dqint;
typedef priority_queue<ll> pqll;
typedef priority_queue<int> pqin;
typedef map<ll, ll> mpll;
typedef map<int, int> mpin;
typedef pair<ll, ll> pll;
typedef pair<int, int> pin;
const ll MOD = 1000000007;
const long double PI = 3.1415926535897932384626433832795;
ll pwr(ll b, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1) {
      res *= b;
      p--;
    } else {
      b *= b;
      p >>= 1;
    }
  }
  return res;
}
const int maxn = 1e3;
int h, w, sh, sw, th, tw;
char M[maxn][maxn];
int dist[maxn][maxn];
int dix[4] = {1, -1, 0, 0};
int diy[4] = {0, 0, 1, -1};
bool isValid(int a, int b) {
  return (a >= 0 && a < h) && (b >= 0 && b < w) && M[a][b] == '.';
}
void dfs() {
  F(i, 0, h, 1) { F(j, 0, w, 1) dist[i][j] = 1e9; }
  deque<pin> D;
  dist[sw][sh] = 0;
  D.push_front(mkp(sw, sh));
  while (!D.empty()) {
    pin q = D.front();
    D.pop_front();
    F(i, 0, 4, 1) {
      if (isValid(q.fs + dix[i], q.ss + diy[i]) &&
          dist[q.fs + dix[i]][q.ss + diy[i]] > dist[q.fs][q.ss]) {
        dist[q.fs + dix[i]][q.ss + diy[i]] = dist[q.fs][q.ss];
        D.push_front(mkp(q.fs + dix[i], q.ss + diy[i]));
      }
    }

    F(i, -2, 3, 1) {
      F(j, -2, 3, 1) {
        if (isValid(q.fs + i, q.ss + j) &&
            dist[q.fs + i][q.ss + j] > dist[q.fs][q.ss] + 1) {
          dist[q.fs + i][q.ss + j] = dist[q.fs][q.ss] + 1;
          D.pb(mkp(q.fs + i, q.ss + j));
        }
      }
    }
  }
}
int main() {
  /*
  freopen ("input.txt","r","stdin");
  freopen ("output.txt","w","stdout");
  */
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> h >> w >> sw >> sh >> tw >> th;
  --sw;
  --sh;
  --tw;
  --th;
  // Taking matrix as input
  F(i, 0, h, 1) { F(j, 0, w, 1) cin >> M[i][j]; }
  dfs();
  if (dist[tw][th] == 1e9)
    cout << -1;
  else
    cout << dist[tw][th];
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define F(i, j, k, in) for (int i = j; i < k; i += in)
#define DF(i, j, k, in) for (int i = j; i >= k; i -= in)
#define feach(it, l) for (auto it = l.begin(); it != l.end(); ++it)
#define fall(a) a.begin(), a.end()
#define sz(x) (int)x.size()
#define szs(x) (int)s.length()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef queue<int> qin;
typedef deque<ll> dqll;
typedef deque<int> dqint;
typedef priority_queue<ll> pqll;
typedef priority_queue<int> pqin;
typedef map<ll, ll> mpll;
typedef map<int, int> mpin;
typedef pair<ll, ll> pll;
typedef pair<int, int> pin;
const ll MOD = 1000000007;
const long double PI = 3.1415926535897932384626433832795;
ll pwr(ll b, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1) {
      res *= b;
      p--;
    } else {
      b *= b;
      p >>= 1;
    }
  }
  return res;
}
const int maxn = 1e3;
int h, w, sh, sw, th, tw;
char M[maxn][maxn];
int dist[maxn][maxn];
int dix[4] = {1, -1, 0, 0};
int diy[4] = {0, 0, 1, -1};
bool isValid(int a, int b) {
  return (a >= 0 && a < h) && (b >= 0 && b < w) && M[a][b] == '.';
}
void dfs() {
  F(i, 0, h, 1) { F(j, 0, w, 1) dist[i][j] = 1e9; }
  deque<pin> D;
  dist[sw][sh] = 0;
  D.push_front(mkp(sw, sh));
  while (!D.empty()) {
    pin q = D.front();
    D.pop_front();
    F(i, 0, 4, 1) {
      if (dist[q.fs + dix[i]][q.ss + diy[i]] > dist[q.fs][q.ss] &&
          isValid(q.fs + dix[i], q.ss + diy[i])) {
        dist[q.fs + dix[i]][q.ss + diy[i]] = dist[q.fs][q.ss];
        D.push_front(mkp(q.fs + dix[i], q.ss + diy[i]));
      }
    }

    F(i, -2, 3, 1) {
      F(j, -2, 3, 1) {
        if (dist[q.fs + i][q.ss + j] > dist[q.fs][q.ss] + 1 &&
            isValid(q.fs + i, q.ss + j)) {
          dist[q.fs + i][q.ss + j] = dist[q.fs][q.ss] + 1;
          D.pb(mkp(q.fs + i, q.ss + j));
        }
      }
    }
  }
}
int main() {
  /*
  freopen ("input.txt","r","stdin");
  freopen ("output.txt","w","stdout");
  */
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  cin >> h >> w >> sw >> sh >> tw >> th;
  --sw;
  --sh;
  --tw;
  --th;
  // Taking matrix as input
  F(i, 0, h, 1) { F(j, 0, w, 1) cin >> M[i][j]; }
  dfs();
  if (dist[tw][th] == 1e9)
    cout << -1;
  else
    cout << dist[tw][th];
  return 0;
}