Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

char s[1005][1005];
int cnt[1005][1005];

int INF = 1001001001;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int H, W;

vector<vector<P>> X(100000);

void umeru(int h, int w, int d) {
  cnt[h][w] = d;
  X[d].push_back(make_pair(h, w));
  rep(i, 4) {
    int hn = h + dy[i];
    if (hn < 0 || hn >= H)
      continue;
    int wn = w + dx[i];
    if (wn < 0 || wn >= W)
      continue;
    if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
      umeru(hn, wn, d);
    }
  }
  return;
}

void warp(int d) {
  int ss = X[d].size();
  rep(k, ss) {
    int h = X[d][k].first;
    int w = X[d][k].second;
    for (int i = -2; i <= 2; i++) {
      int hn = h + i;
      if (hn < 0 || hn >= H)
        continue;
      for (int j = -2; j <= 2; j++) {
        int wn = w + j;
        if (wn < 0 || wn >= W)
          continue;
        if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
          umeru(hn, wn, d + 1);
        }
      }
    }
  }
  return;
}

int main() {
  cin >> H >> W;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  rep(i, H) {
    rep(j, W) { cin >> s[i][j]; }
  }

  rep(i, H) {
    rep(j, W) {
      if (s[i][j] == '#') {
        cnt[i][j] = -1;
      } else {
        cnt[i][j] = INF;
      }
    }
  }

  umeru(ch, cw, 0);

  /*rep(i, H){
      rep(j, W){
          cout << i << " " << j << " " << cnt[i][j] << endl;
      }
  }*/

  int cnt2 = 0;
  while (1) {
    warp(cnt2);
    if (cnt[dh][dw] != INF) {
      break;
    }
    if (X[cnt2 + 1].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    cnt2++;
  }

  /*rep(i, H){
      rep(j, W){
          cout << i << " " << j << " " << cnt[i][j] << endl;
      }
  }*/

  cout << cnt[dh][dw] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

char s[1005][1005];
int cnt[1005][1005];

int INF = 1001001001;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int H, W;

vector<vector<P>> X(1000000);

void umeru(int h, int w, int d) {
  cnt[h][w] = d;
  X[d].push_back(make_pair(h, w));
  rep(i, 4) {
    int hn = h + dy[i];
    if (hn < 0 || hn >= H)
      continue;
    int wn = w + dx[i];
    if (wn < 0 || wn >= W)
      continue;
    if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
      umeru(hn, wn, d);
    }
  }
  return;
}

void warp(int d) {
  int ss = X[d].size();
  rep(k, ss) {
    int h = X[d][k].first;
    int w = X[d][k].second;
    for (int i = -2; i <= 2; i++) {
      int hn = h + i;
      if (hn < 0 || hn >= H)
        continue;
      for (int j = -2; j <= 2; j++) {
        int wn = w + j;
        if (wn < 0 || wn >= W)
          continue;
        if (s[hn][wn] == '.' && cnt[hn][wn] == INF) {
          umeru(hn, wn, d + 1);
        }
      }
    }
  }
  return;
}

int main() {
  cin >> H >> W;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  rep(i, H) {
    rep(j, W) { cin >> s[i][j]; }
  }

  rep(i, H) {
    rep(j, W) {
      if (s[i][j] == '#') {
        cnt[i][j] = -1;
      } else {
        cnt[i][j] = INF;
      }
    }
  }

  umeru(ch, cw, 0);

  /*rep(i, H){
      rep(j, W){
          cout << i << " " << j << " " << cnt[i][j] << endl;
      }
  }*/

  int cnt2 = 0;
  while (1) {
    warp(cnt2);
    if (cnt[dh][dw] != INF) {
      break;
    }
    if (X[cnt2 + 1].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    cnt2++;
  }

  /*rep(i, H){
      rep(j, W){
          cout << i << " " << j << " " << cnt[i][j] << endl;
      }
  }*/

  cout << cnt[dh][dw] << endl;

  return 0;
}