Problem p03436 (C++)

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

int main() {

  int H, W;
  cin >> H >> W;
  vector<vector<char>> maze(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze.at(i).at(j);
    }
  }
  vector<vector<int>> distance(H, vector<int>(W, 0));

  int count = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze.at(i).at(j) == '#') {
        count++;
      }
    }
  }

  queue<pair<int, int>> q;
  q.push(make_pair(1, 1));
  while (distance.at(H - 1).at(W - 1) == 0) {
    pair<int, int> p = q.front();
    if (p.first > 1) {
      if (maze.at(p.first - 2).at(p.second - 1) != '#' &&
          distance.at(p.first - 2).at(p.second - 1) == 0) {
        distance.at(p.first - 2).at(p.second - 1) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first - 1, p.second));
      }
    }
    if (p.first < H) {
      if (maze.at(p.first).at(p.second - 1) != '#' &&
          distance.at(p.first).at(p.second - 1) == 0) {
        distance.at(p.first).at(p.second - 1) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first + 1, p.second));
      }
    }
    if (p.second > 1) {
      if (maze.at(p.first - 1).at(p.second - 2) != '#' &&
          distance.at(p.first - 1).at(p.second - 2) == 0) {
        distance.at(p.first - 1).at(p.second - 2) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first, p.second - 1));
      }
    }
    if (p.second < W) {
      if (maze.at(p.first - 1).at(p.second) != '#' &&
          distance.at(p.first - 1).at(p.second) == 0) {
        distance.at(p.first - 1).at(p.second) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first, p.second + 1));
      }
    }
    q.pop();
  }

  cout << H * W - (distance.at(H - 1).at(W - 1) + 1) - count << endl;
  return 0;
}

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

int main() {

  int H, W;
  cin >> H >> W;
  vector<vector<char>> maze(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze.at(i).at(j);
    }
  }
  vector<vector<int>> distance(H, vector<int>(W, 0));

  int count = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze.at(i).at(j) == '#') {
        count++;
      }
    }
  }

  queue<pair<int, int>> q;
  q.push(make_pair(1, 1));
  while (distance.at(H - 1).at(W - 1) == 0) {
    pair<int, int> p = q.front();
    if (p.first > 1) {
      if (maze.at(p.first - 2).at(p.second - 1) != '#' &&
          distance.at(p.first - 2).at(p.second - 1) == 0) {
        distance.at(p.first - 2).at(p.second - 1) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first - 1, p.second));
      }
    }
    if (p.first < H) {
      if (maze.at(p.first).at(p.second - 1) != '#' &&
          distance.at(p.first).at(p.second - 1) == 0) {
        distance.at(p.first).at(p.second - 1) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first + 1, p.second));
      }
    }
    if (p.second > 1) {
      if (maze.at(p.first - 1).at(p.second - 2) != '#' &&
          distance.at(p.first - 1).at(p.second - 2) == 0) {
        distance.at(p.first - 1).at(p.second - 2) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first, p.second - 1));
      }
    }
    if (p.second < W) {
      if (maze.at(p.first - 1).at(p.second) != '#' &&
          distance.at(p.first - 1).at(p.second) == 0) {
        distance.at(p.first - 1).at(p.second) =
            distance.at(p.first - 1).at(p.second - 1) + 1;
        q.push(make_pair(p.first, p.second + 1));
      }
    }
    q.pop();
    if (q.empty()) {
      cout << "-1" << endl;
      return 0;
    }
  }

  cout << H * W - (distance.at(H - 1).at(W - 1) + 1) - count << endl;
  return 0;
}