Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

int main() {
  ll H, W;
  cin >> H >> W;
  bool s[H][W];
  REP(i, H) REP(j, W) {
    char tmp;
    cin >> tmp;
    s[i][j] = (tmp == '.' ? false : true);
  }

  bool visited[H][W];
  REP(i, H) REP(j, W) visited[i][j] = false;
  queue<pair<pll, ll>> qu;
  qu.push({{0, 0}, 0});
  vector<pll> move{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

  ll goal_dist;
  while (1) {
    auto now = qu.front();
    qu.pop();
    pll now_grid = now.first;
    ll now_dist = now.second;
    if (now_grid.first == H - 1 && now_grid.second == W - 1) {
      goal_dist = now_dist;
      break;
    }
    if (visited[now_grid.first][now_grid.second])
      continue;
    visited[now_grid.first][now_grid.second] = true;

    REP(i, 4) {
      pll next_grid = {now_grid.first + move[i].first,
                       now_grid.second + move[i].second};
      if (next_grid.first < 0 || next_grid.first > H - 1)
        continue;
      if (next_grid.second < 0 || next_grid.second > W - 1)
        continue;
      if (!visited[next_grid.first][next_grid.second] &&
          !s[next_grid.first][next_grid.second]) {
        ll next_dist = now_dist + 1;
        qu.push({next_grid, next_dist});
      }
    }
  }

  ll sum = 0;
  REP(i, H) REP(j, W) sum += s[i][j];
  cout << H * W - sum - goal_dist - 1 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <typename T> void vprint(T &v) {
  REP(i, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

int main() {
  ll H, W;
  cin >> H >> W;
  bool s[H][W];
  REP(i, H) REP(j, W) {
    char tmp;
    cin >> tmp;
    s[i][j] = (tmp == '.' ? false : true);
  }

  bool visited[H][W];
  REP(i, H) REP(j, W) visited[i][j] = false;
  queue<pair<pll, ll>> qu;
  qu.push({{0, 0}, 0});
  vector<pll> move{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

  ll goal_dist;
  while (1) {
    if (qu.empty()) {
      cout << -1 << endl;
      return 0;
    }
    auto now = qu.front();
    qu.pop();
    pll now_grid = now.first;
    ll now_dist = now.second;
    if (now_grid.first == H - 1 && now_grid.second == W - 1) {
      goal_dist = now_dist;
      break;
    }
    if (visited[now_grid.first][now_grid.second])
      continue;
    visited[now_grid.first][now_grid.second] = true;

    REP(i, 4) {
      pll next_grid = {now_grid.first + move[i].first,
                       now_grid.second + move[i].second};
      if (next_grid.first < 0 || next_grid.first > H - 1)
        continue;
      if (next_grid.second < 0 || next_grid.second > W - 1)
        continue;
      if (!visited[next_grid.first][next_grid.second] &&
          !s[next_grid.first][next_grid.second]) {
        ll next_dist = now_dist + 1;
        qu.push({next_grid, next_dist});
      }
    }
  }

  ll sum = 0;
  REP(i, H) REP(j, W) sum += s[i][j];
  cout << H * W - sum - goal_dist - 1 << endl;

  return 0;
}