Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define yn                                                                     \
  { puts("YES"); }                                                             \
  else {                                                                       \
    puts("NO");                                                                \
  }

int main() {
  int H, W;
  cin >> H >> W;
  vvi G(H, vi(W));
  int w_cnt = 0;
  rep(i, H) {
    string w;
    cin >> w;
    rep(j, W) {
      if (w[j] == '.') {
        G[i][j] = 1;
        w_cnt++;
      } else {
        G[i][j] = 0;
      }
    }
  }
  vvi d(H, vi(W, -1));
  d[0][0] = 0;
  auto isinRoad = [&](int w, int h) {
    if (w < 0 || w >= W) {
      return false;
    }
    if (h < 0 || h >= H) {
      return false;
    }
    return G[h][w] == 1;
  };

  queue<pair<pair<int, int>, int>> que;
  auto next = [&](int w, int h, int d) {
    int dw[] = {-1, 1, 0, 0};
    int dh[] = {0, 0, -1, 1};
    rep(x, 4) {
      if (isinRoad(dw[x] + w, dh[x] + h)) {
        que.push(pair<P, int>(P(dw[x] + w, dh[x] + h), d + 1));
      }
    }
  };
  next(0, 0, 0);
  while (!que.empty()) {
    pair<P, int> pos = que.front();
    int w = pos.first.first;
    int h = pos.first.second;
    int dis = pos.second;
    que.pop();
    if (d[h][w] != -1) {
      continue;
    }
    d[h][w] = dis;
    next(w, h, dis);
  }
  if (d[H - 1][W - 1] == -1) {
    cout << -1 << endl;
    return -1;
  }
  cout << w_cnt - d[H - 1][W - 1] - 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;
#define dame                                                                   \
  {                                                                            \
    puts("-1");                                                                \
    return 0;                                                                  \
  }
#define yn                                                                     \
  { puts("YES"); }                                                             \
  else {                                                                       \
    puts("NO");                                                                \
  }

int main() {
  int H, W;
  cin >> H >> W;
  vvi G(H, vi(W));
  int w_cnt = 0;
  rep(i, H) {
    string w;
    cin >> w;
    rep(j, W) {
      if (w[j] == '.') {
        G[i][j] = 1;
        w_cnt++;
      } else {
        G[i][j] = 0;
      }
    }
  }
  vvi d(H, vi(W, -1));
  d[0][0] = 0;
  auto isinRoad = [&](int w, int h) {
    if (w < 0 || w >= W) {
      return false;
    }
    if (h < 0 || h >= H) {
      return false;
    }
    return G[h][w] == 1;
  };

  queue<pair<pair<int, int>, int>> que;
  auto next = [&](int w, int h, int d) {
    int dw[] = {-1, 1, 0, 0};
    int dh[] = {0, 0, -1, 1};
    rep(x, 4) {
      if (isinRoad(dw[x] + w, dh[x] + h)) {
        que.push(pair<P, int>(P(dw[x] + w, dh[x] + h), d + 1));
      }
    }
  };
  next(0, 0, 0);
  while (!que.empty()) {
    pair<P, int> pos = que.front();
    int w = pos.first.first;
    int h = pos.first.second;
    int dis = pos.second;
    que.pop();
    if (d[h][w] != -1) {
      continue;
    }
    d[h][w] = dis;
    next(w, h, dis);
  }
  if (d[H - 1][W - 1] == -1) {
    cout << -1 << endl;
    return 0;
  }
  cout << w_cnt - d[H - 1][W - 1] - 1 << endl;
  return 0;
}