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

#include <iostream>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef pair<int, pair<int, int>> piii;
const int N = 5e3 + 1;
int n, m, d1, d2, c1, c2;
bool avail[N][N], check = true;
priority_queue<piii, vector<piii>, greater<piii>> heap, loop;
int Dijkstra(int val, int x, int y) {
  avail[x][y] = true;
  heap.push({val, {x, y}});
  loop.push({val, {x, y}});
  while (!heap.empty()) {
    while (!heap.empty()) {
      piii temp = heap.top();
      heap.pop();
      int u = temp.se.fi, v = temp.se.se;
      loop.push({temp.fi, {u, v}});
      if (u == d1 && v == d2)
        return temp.fi;
      if (!avail[u - 1][v]) {
        avail[u - 1][v] = true;
        heap.push({temp.fi, {u - 1, v}});
        loop.push({temp.fi, {u - 1, v}});
      }
      if (!avail[u + 1][v]) {
        avail[u + 1][v] = true;
        heap.push({temp.fi, {u + 1, v}});
        loop.push({temp.fi, {u + 1, v}});
      }
      if (!avail[u][v - 1]) {
        avail[u][v - 1] = true;
        heap.push({temp.fi, {u, v - 1}});
        loop.push({temp.fi, {u, v - 1}});
      }
      if (!avail[u][v + 1]) {
        avail[u][v + 1] = true;
        heap.push({temp.fi, {u, v + 1}});
        loop.push({temp.fi, {u, v + 1}});
      }
    }
    while (!loop.empty()) {
      piii temp = loop.top();
      loop.pop();
      int x = temp.se.fi, y = temp.se.se;
      for (int i = max(x - 2, 1); i <= min(x + 2, n); i++)
        for (int j = max(y - 2, 1); j <= min(y + 2, m); j++)
          if (!avail[i][j]) {
            avail[i][j] = true;
            heap.push({temp.fi + 1, {i, j}});
          }
    }
    /*cout << "==================================\\n";
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            if (avail[i][j] == true) cout << "#"; else cout << ".";
            cout << "\\n";
        }*/
  }
  return -1;
}
int main() {
  cin >> n >> m;
  cin >> c1 >> c2;
  cin >> d1 >> d2;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++)
      if (s[j - 1] == '#')
        avail[i][j] = true;
  }
  for (int i = 0; i <= n + 1; i++)
    avail[i][0] = avail[i][m + 1] = true;
  for (int j = 0; j <= m + 1; j++)
    avail[0][j] = avail[n + 1][j] = true;
  /*for (int i = 0; i <= n + 1; i++)
  {
      for (int j = 0; j <= m + 1; j++)
          if (avail[i][j] == true) cout << "#"; else cout << ".";
      cout << "\\n";
  }*/
  cout << Dijkstra(0, c1, c2);
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <queue>
#define fi first
#define se second
using namespace std;
typedef pair<int, pair<int, int>> piii;
const int N = 1e3 + 1;
int n, m, d1, d2, c1, c2;
bool avail[N][N], check = true;
priority_queue<piii, vector<piii>, greater<piii>> heap, loop;
int Dijkstra(int val, int x, int y) {
  avail[x][y] = true;
  heap.push({val, {x, y}});
  loop.push({val, {x, y}});
  while (!heap.empty()) {
    while (!heap.empty()) {
      piii temp = heap.top();
      heap.pop();
      int u = temp.se.fi, v = temp.se.se;
      if (u == d1 && v == d2)
        return temp.fi;
      if (!avail[u - 1][v]) {
        avail[u - 1][v] = true;
        heap.push({temp.fi, {u - 1, v}});
        loop.push({temp.fi, {u - 1, v}});
      }
      if (!avail[u + 1][v]) {
        avail[u + 1][v] = true;
        heap.push({temp.fi, {u + 1, v}});
        loop.push({temp.fi, {u + 1, v}});
      }
      if (!avail[u][v - 1]) {
        avail[u][v - 1] = true;
        heap.push({temp.fi, {u, v - 1}});
        loop.push({temp.fi, {u, v - 1}});
      }
      if (!avail[u][v + 1]) {
        avail[u][v + 1] = true;
        heap.push({temp.fi, {u, v + 1}});
        loop.push({temp.fi, {u, v + 1}});
      }
    }
    while (!loop.empty()) {
      piii temp = loop.top();
      loop.pop();
      int x = temp.se.fi, y = temp.se.se;
      for (int i = max(x - 2, 1); i <= min(x + 2, n); i++)
        for (int j = max(y - 2, 1); j <= min(y + 2, m); j++)
          if (!avail[i][j]) {
            avail[i][j] = true;
            heap.push({temp.fi + 1, {i, j}});
          }
    }
    /*cout << "==================================\\n";
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            if (avail[i][j] == true) cout << "#"; else cout << ".";
            cout << "\\n";
        }*/
  }
  return -1;
}
int main() {
  cin >> n >> m;
  cin >> c1 >> c2;
  cin >> d1 >> d2;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++)
      if (s[j - 1] == '#')
        avail[i][j] = true;
  }
  for (int i = 0; i <= n + 1; i++)
    avail[i][0] = avail[i][m + 1] = true;
  for (int j = 0; j <= m + 1; j++)
    avail[0][j] = avail[n + 1][j] = true;
  /*for (int i = 0; i <= n + 1; i++)
  {
      for (int j = 0; j <= m + 1; j++)
          if (avail[i][j] == true) cout << "#"; else cout << ".";
      cout << "\\n";
  }*/
  cout << Dijkstra(0, c1, c2);
  return 0;
}