Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<long long>
#define sz(x) (int)x.size()
#define pi pair<long long, long long>
#define endl '\n'
#define _ << ' '
#define FAST_LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
const ll mod = 998244353;
const ll INF = (ll)1e18 + 1;
const ll N = 1e5;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m, b = 0;

bool check(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m)
    return 0;
  return 1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("B.txt", "r", stdin);
#endif

  int d[50][50] = {0};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char ch;
      cin >> ch;
      if (ch == '#') {
        b++;
        d[i][j] = -1;
      }
    }
  }
  queue<pair<int, int>> q;
  q.push({0, 0});
  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = cur.F + dx[i];
      int ny = cur.S + dy[i];
      if (!check(nx, ny) || d[nx][ny] != 0)
        continue;
      d[nx][ny] = d[cur.F][cur.S] + 1;
      q.push({nx, ny});
    }
  }
  if (d[n - 1][m - 1] == 0) {
    cout << -1;
  } else {
    cout << n * m - b - d[n - 1][m - 1] - 1;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<long long>
#define sz(x) (int)x.size()
#define pi pair<long long, long long>
#define endl '\n'
#define _ << ' '
#define FAST_LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define clog2(x) (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    idxset;
// find_by_order(i) : iterator to the value at index i, order_of_key(x) : the
// index of value x
const ll mod = 998244353;
const ll INF = (ll)1e18 + 1;
const ll N = 1e5;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m, b = 0;

bool check(int i, int j) {
  if (i < 0 || j < 0 || i >= n || j >= m)
    return 0;
  return 1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
// freopen("B.txt", "r", stdin);
#endif

  int d[50][50] = {0};
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char ch;
      cin >> ch;
      if (ch == '#') {
        b++;
        d[i][j] = -1;
      }
    }
  }
  queue<pair<int, int>> q;
  q.push({0, 0});
  while (!q.empty()) {
    auto cur = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = cur.F + dx[i];
      int ny = cur.S + dy[i];
      if (!check(nx, ny) || d[nx][ny] != 0)
        continue;
      d[nx][ny] = d[cur.F][cur.S] + 1;
      q.push({nx, ny});
    }
  }
  if (d[n - 1][m - 1] == 0) {
    cout << -1;
  } else {
    cout << n * m - b - d[n - 1][m - 1] - 1;
  }
}