Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

// North, west, south, east
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int main(void) {
  int h, w;
  std::cin >> h >> w;

  int countAll = h * w;
  int countOriginalBlack = 0;

  std::vector<std::vector<char>> board(h, std::vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> board[i][j];
      if (board[i][j] == '#')
        countOriginalBlack++;
    }
  }

  int sx = 0;
  int sy = 0;
  int gx = h - 1;
  int gy = w - 1;

  std::queue<std::pair<int, int>> que;

  // //11/08/2019 added: to record the distance from the start
  std::vector<std::vector<int>> distance(h, std::vector<int>(w));

  // //11/08/2019 added: not to  check the position already visited
  std::vector<std::vector<int>> dualcheck(h, std::vector<int>(w));

  // the distance from the start to the start must be 0
  distance[sx][sy] = 0;
  dualcheck[sx][sy]++;

  // push the Coordinate
  que.push({sx, sy});

  while (que.size()) {
    int x, y;
    std::tie(x, y) = que.front();

    // after completing addition to the tie, que.front must be deleted
    que.pop();
    for (int i = 0; i < 4; i++) {

      // neighborhood of the maze[y][x]
      int nx = x + dx[i];
      int ny = y + dy[i];

      // Outside of the maze, or the wall is inappropriate
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || board[nx][ny] == '#') {
        continue;
      }

      // 11/08/2019 added: not to count the place already visited
      if (dualcheck[nx][ny] != 0)
        continue;

      // the coordinate can be plus 1
      distance[nx][ny] = distance[y][x] + 1;

      // 11/08/2019 added: not to count the place already visited
      dualcheck[nx][ny]++;
      que.push({nx, ny});
    }
  }

  int minRouteLength = distance[gx][gy];
  int ans = countAll - countOriginalBlack - minRouteLength - 1;

  if (!distance[gx][gy])
    ans = -1;

  std::cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

// North, west, south, east
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int main(void) {
  int h, w;
  std::cin >> h >> w;

  int countAll = h * w;
  int countOriginalBlack = 0;

  std::vector<std::vector<char>> board(h, std::vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> board[i][j];
      if (board[i][j] == '#')
        countOriginalBlack++;
    }
  }

  int sx = 0;
  int sy = 0;
  int gx = h - 1;
  int gy = w - 1;

  std::queue<std::pair<int, int>> que;

  // //11/08/2019 added: to record the distance from the start
  std::vector<std::vector<int>> distance(h, std::vector<int>(w));

  // //11/08/2019 added: not to  check the position already visited
  std::vector<std::vector<int>> dualcheck(h, std::vector<int>(w));

  // the distance from the start to the start must be 0
  distance[sx][sy] = 0;
  dualcheck[sx][sy]++;

  // push the Coordinate
  que.push({sx, sy});

  while (que.size()) {
    int x, y;
    std::tie(x, y) = que.front();

    // after completing addition to the tie, que.front must be deleted
    que.pop();
    for (int i = 0; i < 4; i++) {

      // neighborhood of the maze[y][x]
      int nx = x + dx[i];
      int ny = y + dy[i];

      // Outside of the maze, or the wall is inappropriate
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || board[nx][ny] == '#') {
        continue;
      }

      // 11/08/2019 added: not to count the place already visited
      if (dualcheck[nx][ny] != 0)
        continue;

      // the coordinate can be plus 1
      distance[nx][ny] = distance[x][y] + 1;

      // 11/08/2019 added: not to count the place already visited
      dualcheck[nx][ny]++;
      que.push({nx, ny});
    }
  }

  int minRouteLength = distance[gx][gy];
  int ans = countAll - countOriginalBlack - minRouteLength - 1;

  if (!distance[gx][gy])
    ans = -1;

  std::cout << ans << std::endl;
  return 0;
}