Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_D main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LLONG_MAX / 2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psi = pair<string, int>;
// functions

#endif

int main_D() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vs S(H);
  REP(i, H) cin >> S[i];

  int black = 0;
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        black++;
    }
  }

  if (S[H - 2][W - 1] == '#' && S[H - 1][W - 2] == '#') {
    cout << -1 << endl;
    return 0;
  }

  queue<pii> que;
  que.push(pii(0, 0));
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  vvb vis(H, vb(W, false));
  vvi dist(H, vi(W, INF));
  dist[0][0] = 0;
  while (!que.empty()) {
    pii p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;
    vis[x][y] = true;

    if (vis[H - 1][W - 1])
      break;

    REP(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || H <= nx || ny < 0 || W <= ny || S[nx][ny] == '#' ||
          vis[nx][ny])
        continue;

      dist[nx][ny] = min(dist[nx][ny], dist[x][y] + 1);

      que.push(pii(nx, ny));
    }
  }

  int ans = 0;
  if (dist[H - 1][W - 1] == INF) {
    ans = -1;
  } else {
    ans = H * W - black - (dist[H - 1][W - 1] + 1);
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_D main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LLONG_MAX / 2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psi = pair<string, int>;
// functions

#endif

int main_D() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W;
  cin >> H >> W;
  vs S(H);
  REP(i, H) cin >> S[i];

  int black = 0;
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        black++;
    }
  }

  if (S[H - 2][W - 1] == '#' && S[H - 1][W - 2] == '#') {
    cout << -1 << endl;
    return 0;
  }

  queue<pii> que;
  que.push(pii(0, 0));
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  vvb vis(H, vb(W, false));
  vvi dist(H, vi(W, INF));
  dist[0][0] = 0;
  while (!que.empty()) {
    pii p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;
    vis[x][y] = true;

    if (vis[H - 1][W - 1])
      break;

    REP(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || H <= nx || ny < 0 || W <= ny || S[nx][ny] == '#' ||
          vis[nx][ny])
        continue;

      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        que.push(pii(nx, ny));
      }
    }
  }

  int ans = 0;
  if (dist[H - 1][W - 1] == INF) {
    ans = -1;
  } else {
    ans = H * W - black - (dist[H - 1][W - 1] + 1);
  }
  cout << ans << endl;

  return 0;
}