Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
//---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
//---------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1e18
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define vec vector
using pint = pair<int, int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
//---------------------------------------------------------------
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const double pi = acos(-1);
const int mod = 1000000007;
struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
//---------------------------------------------------------------

// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

int dist[110][110];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll h, w;
  cin >> h >> w;
  vec<string> f(h);
  rep(i, h) cin >> f[i];

  ll wn = 0;
  rep(i, h) rep(j, w) {
    if (f[i][j] == '.')
      wn++;
  }

  memset(dist, -1, sizeof(dist));
  dist[0][0] = 1;

  queue<pint> que;
  que.push(pint(0, 0));

  while (!que.empty()) {

    pint cur = que.front();
    que.pop();

    ll x = cur.fi;
    ll y = cur.se;
    rep(k, 4) {

      ll ny = y + dy[k];
      ll nx = x + dx[k];

      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;

      if (f[ny][nx] == '#')
        continue;

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

  if (dist[h - 1][w - 1] == -1)
    cout << "-1" << endl;
  else
    cout << wn - dist[h - 1][w - 1] << endl;
  return 0;
}

Fixed submission:
//---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <numeric>
#include <sstream>
#include <vector>
//---------------------------------------------------------------
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define SIZE 100005
#define INF 1e18
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define vec vector
using pint = pair<int, int>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
//---------------------------------------------------------------
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const double pi = acos(-1);
const int mod = 1000000007;
struct mint {
  int64_t x;
  mint(int64_t x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(int64_t t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
//---------------------------------------------------------------

// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

int dist[110][110];

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  ll h, w;
  cin >> h >> w;
  vec<string> f(h);
  rep(i, h) cin >> f[i];

  ll wn = 0;
  rep(i, h) rep(j, w) {
    if (f[i][j] == '.')
      wn++;
  }

  memset(dist, -1, sizeof(dist));
  dist[0][0] = 1;

  queue<pint> que;
  que.push(pint(0, 0));

  while (!que.empty()) {

    pint cur = que.front();
    que.pop();

    ll x = cur.fi;
    ll y = cur.se;
    rep(k, 4) {

      ll ny = y + dy[k];
      ll nx = x + dx[k];

      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;

      if (f[ny][nx] == '#')
        continue;

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

  if (dist[h - 1][w - 1] == -1)
    cout << "-1" << endl;
  else
    cout << wn - dist[h - 1][w - 1] << endl;
  return 0;
}