Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main(void) {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> s(H, vector<char>(W));
  int white = 0;
  int black = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s[i][j];
      if (s[i][j] == '.')
        white++;
      else
        black++;
    }
  }

  queue<pair<int, int>> qp;
  queue<int> qcnt;
  vector<vector<bool>> arrmap(H, vector<bool>(W, true));
  int i = 0, j = 0;
  int cnt = 0;

  while (1) {
    if (i == H - 1 && j == W - 1) {
      break;
    }

    if (i > 0 && s[i - 1][j] == '.' && arrmap[i - 1][j]) {
      pair<int, int> p;
      p.first = i - 1;
      p.second = j;
      qp.push(p);
      arrmap[i - 1][j] = false;
      qcnt.push(cnt + 1);
    }
    if (i < H - 1 && s[i + 1][j] == '.' && arrmap[i + 1][j]) {
      pair<int, int> p;
      p.first = i + 1;
      p.second = j;
      qp.push(p);
      arrmap[i + 1][j] = false;
      qcnt.push(cnt + 1);
    }
    if (j < W - 1 && s[i][j + 1] == '.' && arrmap[i][j + 1]) {
      pair<int, int> p;
      p.first = i;
      p.second = j + 1;
      qp.push(p);
      arrmap[i][j + 1] = false;
      qcnt.push(cnt + 1);
    }
    if (j > 0 && s[i][j - 1] == '.' && arrmap[i][j - 1]) {
      pair<int, int> p;
      p.first = i + 1;
      p.second = j;
      qp.push(p);
      arrmap[i][j - 1] = false;
      qcnt.push(cnt + 1);
    }
    if (qp.empty()) {
      cout << -1 << endl;
      return 0;
    }
    pair<int, int> ij;
    ij = qp.front();
    qp.pop();
    i = ij.first;
    j = ij.second;
    cnt = qcnt.front();
    qcnt.pop();
  }

  cout << white - cnt - 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main(void) {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> s(H, vector<char>(W));
  int white = 0;
  int black = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> s[i][j];
      if (s[i][j] == '.')
        white++;
      else
        black++;
    }
  }

  queue<pair<int, int>> qp;
  queue<int> qcnt;
  vector<vector<bool>> arrmap(H, vector<bool>(W, true));
  int i = 0, j = 0;
  int cnt = 0;

  while (1) {
    if (i == H - 1 && j == W - 1) {
      break;
    }

    if (i > 0 && s[i - 1][j] == '.' && arrmap[i - 1][j]) {
      pair<int, int> p;
      p.first = i - 1;
      p.second = j;
      qp.push(p);
      arrmap[i - 1][j] = false;
      qcnt.push(cnt + 1);
    }
    if (i < H - 1 && s[i + 1][j] == '.' && arrmap[i + 1][j]) {
      pair<int, int> p;
      p.first = i + 1;
      p.second = j;
      qp.push(p);
      arrmap[i + 1][j] = false;
      qcnt.push(cnt + 1);
    }
    if (j < W - 1 && s[i][j + 1] == '.' && arrmap[i][j + 1]) {
      pair<int, int> p;
      p.first = i;
      p.second = j + 1;
      qp.push(p);
      arrmap[i][j + 1] = false;
      qcnt.push(cnt + 1);
    }
    if (j > 0 && s[i][j - 1] == '.' && arrmap[i][j - 1]) {
      pair<int, int> p;
      p.first = i;
      p.second = j - 1;
      qp.push(p);
      arrmap[i][j - 1] = false;
      qcnt.push(cnt + 1);
    }
    if (qp.empty()) {
      cout << -1 << endl;
      return 0;
    }
    pair<int, int> ij;
    ij = qp.front();
    qp.pop();
    i = ij.first;
    j = ij.second;
    cnt = qcnt.front();
    qcnt.pop();
  }

  cout << white - cnt - 1 << endl;
  return 0;
}