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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int h, w;
  cin >> h >> w;
  int srow, scol, erow, ecol;
  cin >> srow >> scol;
  cin >> erow >> ecol;
  --srow, --scol, --erow, --ecol;
  string grid[h];
  for (int i = 0; i < h; ++i) {
    cin >> grid[i];
  }
  //	bool ok = false;
  deque<pair<int, int>> dq;
  int dist[h][w];
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      dist[i][j] = 1e7;
    }
  }
  dist[srow][scol] = 0;
  dq.push_back({srow, scol});
  while (!dq.empty()) {
    pair<int, int> cur_node = dq.front();
    dq.pop_front();
    int row = cur_node.first;
    int col = cur_node.second;
    //		if(row == erow && col == ecol){
    //			ok = true;
    //			break;
    //		}
    for (int dx = -2; dx <= 2; ++dx) {
      for (int dy = -2; dy <= 2; ++dy) {
        int nrow = row + dx;
        int ncol = col + dy;
        //				if(nrow == row && ncol == col)
        //					continue;
        if (nrow < 0 || ncol < 0 || nrow >= h || ncol >= w || (!dx && !dy) ||
            grid[nrow][ncol] == '#')
          continue;
        int w = 1;
        //				if(grid[nrow][ncol] == '#')
        //					continue;
        //				if((abs(nrow-row) == 1 && abs(ncol-col) == 0)
        //|| (abs(ncol-col) == 1 && abs(nrow-row) == 0)) 					w = 0;
        if ((abs(dx) == 1 && dy == 0) || (abs(dy) == 1 && dx == 0))
          w = 0;
        if (dist[nrow][ncol] > dist[row][col] + w) {
          dist[nrow][ncol] = dist[row][col] + w;
          if (w == 0)
            dq.push_front({nrow, ncol});
          else
            dq.push_back({nrow, ncol});
        }
      }
    }
  }
  //	if(!ok)
  //		dist[erow][ecol] = -1;
  //	cout << dist[erow][ecol] << "\\n";
  cout << (dist[erow][ecol] == 1e7 ? -1 : dist[erow][ecol]);
  return 0;
}

Error: WA

Fixed submission:

/*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<long long> vll;
typedef vector<int> vi;

#define io ios_base::sync_with_stdio(false)
#define pb push_back

#define eb emplace_back
#define mod 1000000007
#define PI 2 * acos(0.0)
#define all(r) (r).begin(), (r).end()
#define dbg(a) cout << #a << " ->->->-> " << a << "\\n"
#define inf 1000000000000000000
#define N 2000
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};

int n, m, sx, sy, dx, dy;
ll dp[N][N];
int ck[N][N];

//=============================================ASIFAZAD==============================================//

int32_t main() {
  io;
  int x, y, sx, sy, dx, dy;
  cin >> x >> y >> sx >> sy >> dx >> dy;
  sx--;
  sy--;
  dx--;
  dy--;
  string inp[x];
  for (int i = 0; i < x; i++)
    cin >> inp[i];
  int dist[x][y];
  for (int i = 0; i < x; i++)
    for (int j = 0; j < x; j++)
      dist[i][j] = 10000000;
  dist[sx][sy] = 0;
  deque<pii> d;
  d.pb({sx, sy});
  bool ok = 0;
  while (!d.empty()) {
    pii act = d.front();
    d.pop_front();
    if (act.first == dx && act.second == dy) {
      ok = 1;
      break;
    }
    for (int a = -2; a <= 2; a++) {
      for (int b = -2; b <= 2; b++) {
        int ax = act.first + a, ay = act.second + b;
        if (ax >= x || ay >= y || ax < 0 || ay < 0 || (!a && !b) ||
            inp[ax][ay] == '#')
          continue;
        int w = 1;
        if ((abs(a) == 1 && b == 0) || (abs(b) == 1 && a == 0))
          w = 0;
        //                dbg(dist[act.first][act.second]);
        //                dbg(w);
        //                dbg(act.first);
        //                dbg(act.second);
        if (dist[act.first][act.second] + w < dist[ax][ay]) {
          dist[ax][ay] = dist[act.first][act.second] + w;
          (w ? d.push_back({ax, ay}) : d.push_front({ax, ay}));
        }
      }
    }
  }
  //    dbg(dx);
  //    dbg(dy);
  //    dbg(dist[dx][dy]);
  cout << (ok ? dist[dx][dy] : -1);
  return 0;
}