Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
typedef unsigned long long ull;
typedef std::pair<int, int> p;

ll min(ll a, ll b) {
  if (a >= b)
    return b;
  else
    return a;
}
ll max(ll a, ll b) {
  if (a >= b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool flag = true;
bool fl = false;
bool f = false;
bool used[210];
bool graph[100][100];
bool visited[8];
int abc[26] = {0};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int h, w, d[50][50];
char c[50][50];

int bfs() {
  std::queue<p> que;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      d[i][j] = INF;
    }
  }
  que.push(p(1, 1));
  d[1][1] = 0;
  while (que.size()) {
    p P = que.front();
    que.pop();
    if (P.first == h && P.second == w) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      ll nx = P.first + dx[i], ny = P.second + dy[i];
      if (0 < nx && nx <= h && 0 < ny && ny <= w && c[nx][ny] == '.' &&
          d[nx][ny] == INF) {
        que.push(p(nx, ny));
        d[nx][ny] = d[P.first][P.second] + 1;
      }
    }
  }
  return d[h][w];
}

int main() {
  int sum = 0;
  std::cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      std::cin >> c[i][j];
      if (c[i][j] == '.')
        sum++;
    }
  }
  if (bfs() == INF)
    std::cout << -1 << std::endl;
  else
    std::cout << sum - bfs() - 1 << std::endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using ll = long long;
typedef unsigned long long ull;
typedef std::pair<int, int> p;

ll min(ll a, ll b) {
  if (a >= b)
    return b;
  else
    return a;
}
ll max(ll a, ll b) {
  if (a >= b)
    return a;
  else
    return b;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

const ll Z = 1000000007;
const ll INF = 1 << 30;
const ll INF2 = 9000000000000000000LL;
bool flag = true;
bool fl = false;
bool f = false;
bool used[210];
bool graph[100][100];
bool visited[8];
int abc[26] = {0};
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int h, w, d[55][55];
char c[55][55];

int bfs() {
  std::queue<p> que;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      d[i][j] = INF;
    }
  }
  que.push(p(1, 1));
  d[1][1] = 0;
  while (que.size()) {
    p P = que.front();
    que.pop();
    if (P.first == h && P.second == w) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      ll nx = P.first + dx[i], ny = P.second + dy[i];
      if (0 < nx && nx <= h && 0 < ny && ny <= w && c[nx][ny] == '.' &&
          d[nx][ny] == INF) {
        que.push(p(nx, ny));
        d[nx][ny] = d[P.first][P.second] + 1;
      }
    }
  }
  return d[h][w];
}

int main() {
  int sum = 0;
  std::cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      std::cin >> c[i][j];
      if (c[i][j] == '.')
        sum++;
    }
  }
  if (bfs() == INF)
    std::cout << -1 << std::endl;
  else
    std::cout << sum - bfs() - 1 << std::endl;
}