Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
// typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
// typedef unsigned int uint;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int H, W;
string G[51];
bool checked[51][51];

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int wc = 0;

  cin >> H >> W;
  rep(i, 0, H) {
    cin >> G[i];
    rep(j, 0, W) {
      if (G[i][j] == '.') {
        wc++;
      }
    }
  }

  queue<pair<pair<int, int>, int>> q;
  q.push(make_pair(make_pair(0, 0), 1));
  checked[0][0] = true;

  int dist = -1;

  while (!q.empty()) {
    auto p = q.front();
    q.pop();

    if (p.first.first == W - 1 && p.first.second == H - 1) {
      dist = p.second;
      break;
    }

    rep(i, 0, 4) {
      int nx = p.first.first + dx[i];
      int ny = p.first.second + dy[i];

      if (0 <= nx && nx < W && 0 <= ny && ny < H && !checked[ny][nx] &&
          G[ny][nx] == '.') {
        q.push(make_pair(make_pair(nx, ny), p.second + 1));
      }
    }
  }

  if (dist == -1) {
    cout << "-1" << endl;
  } else {
    cout << wc - dist << endl;
  }

  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef long long ll;
// typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
// typedef unsigned int uint;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int H, W;
string G[51];
bool checked[51][51];

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int wc = 0;

  cin >> H >> W;
  rep(i, 0, H) {
    cin >> G[i];
    rep(j, 0, W) {
      if (G[i][j] == '.') {
        wc++;
      }
    }
  }

  queue<pair<pair<int, int>, int>> q;
  q.push(make_pair(make_pair(0, 0), 1));
  checked[0][0] = true;

  int dist = -1;

  while (!q.empty()) {
    auto p = q.front();
    q.pop();

    if (p.first.first == W - 1 && p.first.second == H - 1) {
      dist = p.second;
      break;
    }

    rep(i, 0, 4) {
      int nx = p.first.first + dx[i];
      int ny = p.first.second + dy[i];

      if (0 <= nx && nx < W && 0 <= ny && ny < H && !checked[ny][nx] &&
          G[ny][nx] == '.') {
        checked[ny][nx] = true;
        q.push(make_pair(make_pair(nx, ny), p.second + 1));
      }
    }
  }

  if (dist == -1) {
    cout << "-1" << endl;
  } else {
    cout << wc - dist << endl;
  }

  return 0;
}