Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
const int N = 50;
int h, w, def = 0, ans, br = 0;
char s[N][N];
bool did[N][N];
queue<pair<int, pair<int, int>>> q;
pair<int, pair<int, int>> p;
void pushq(int x, int y, int d) {
  if (x + 1 < h) {
    if (s[x + 1][y] == '.' && !did[x + 1][y]) {
      q.push(make_pair(d, make_pair(x + 1, y)));
      did[x + 1][y] = true;
    }
  }
  if (x - 1 >= 0) {
    if (s[x - 1][y] == '.' && !did[x - 1][y]) {
      q.push(make_pair(d, make_pair(x - 1, y)));
      did[x - 1][y] = true;
    }
  }
  if (y + 1 < w) {
    if (s[x][y + 1] == '.' && !did[x][y + 1]) {
      q.push(make_pair(d, make_pair(x, y + 1)));
      did[x][y + 1] = true;
    }
  }
  if (y - 1 >= 0) {
    if (s[x][y - 1] == '.' && !did[x][y - 1]) {
      q.push(make_pair(d, make_pair(x, y - 1)));
      did[x][y - 1] = true;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  CLR(did);
  cin >> h >> w;
  FOR(i, 0, h) {
    FOR(j, 0, w) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        br++;
    }
  }

  q.push(make_pair(0, make_pair(0, 0)));
  did[0][0] = true;
  int x, y;
  while (1) {
    p = q.front();
    q.pop();
    x = p.second.first;
    y = p.second.second;
    def = p.first;
    if (x == h - 1 && y == w - 1)
      break;
    pushq(x, y, def + 1);
    if (q.size() == 0) {
      ans = -1;
    }
  }
  if (ans != -1)
    ans = w * h - 1 - def - br;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> int_pair;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
const int N = 50;
int h, w, def = 0, ans, br = 0;
char s[N][N];
bool did[N][N];
queue<pair<int, pair<int, int>>> q;
pair<int, pair<int, int>> p;
void pushq(int x, int y, int d) {
  if (x + 1 < h) {
    if (s[x + 1][y] == '.' && !did[x + 1][y]) {
      q.push(make_pair(d, make_pair(x + 1, y)));
      did[x + 1][y] = true;
    }
  }
  if (x - 1 >= 0) {
    if (s[x - 1][y] == '.' && !did[x - 1][y]) {
      q.push(make_pair(d, make_pair(x - 1, y)));
      did[x - 1][y] = true;
    }
  }
  if (y + 1 < w) {
    if (s[x][y + 1] == '.' && !did[x][y + 1]) {
      q.push(make_pair(d, make_pair(x, y + 1)));
      did[x][y + 1] = true;
    }
  }
  if (y - 1 >= 0) {
    if (s[x][y - 1] == '.' && !did[x][y - 1]) {
      q.push(make_pair(d, make_pair(x, y - 1)));
      did[x][y - 1] = true;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  CLR(did);
  cin >> h >> w;
  FOR(i, 0, h) {
    FOR(j, 0, w) {
      cin >> s[i][j];
      if (s[i][j] == '#')
        br++;
    }
  }

  q.push(make_pair(0, make_pair(0, 0)));
  did[0][0] = true;
  int x, y;
  while (1) {
    p = q.front();
    q.pop();
    x = p.second.first;
    y = p.second.second;
    def = p.first;
    if (x == h - 1 && y == w - 1)
      break;
    pushq(x, y, def + 1);
    if (q.size() == 0) {
      ans = -1;
      break;
    }
  }
  if (ans != -1)
    ans = w * h - 1 - def - br;
  cout << ans << endl;
  return 0;
}