Problem p03436 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  int black = 0;
  rep(i, h) {
    rep(j, w) { black += (s[i][j] == '#'); }
  }
  vector<vector<bool>> visited(h, vector<bool>(w, false));

  int offset[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));
  int path = 1;
  while (!q.empty()) {
    path++;
    int c = q.size();
    Rep(c) {
      auto [x, y] = q.front();
      q.pop();
      visited[x][y] = true;
      for (auto k : offset) {
        int x2 = x + k[0];
        int y2 = y + k[1];
        if (x2 == h - 1 && y2 == w - 1) {
          cout << h * w - black - path << endl;

          return 0;
        }
        if (0 <= x2 && x2 < h && 0 <= y2 && y2 < w) {
          if (!visited[x2][y2] && s[x2][y2] == '.') {
            q.push(make_pair(x2, y2));
          }
        }
      }
    }
  }
  cout << "-1\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  int black = 0;
  rep(i, h) {
    rep(j, w) { black += (s[i][j] == '#'); }
  }
  vector<vector<bool>> visited(h, vector<bool>(w, false));

  int offset[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  queue<pair<int, int>> q;
  q.push(make_pair(0, 0));
  int path = 1;
  while (!q.empty()) {
    path++;
    int c = q.size();
    Rep(c) {
      auto [x, y] = q.front();
      q.pop();
      if (!visited[x][y]) {
        visited[x][y] = true;
        for (auto k : offset) {
          int x2 = x + k[0];
          int y2 = y + k[1];
          if (x2 == h - 1 && y2 == w - 1) {
            cout << h * w - black - path << endl;
            return 0;
          }
          if (0 <= x2 && x2 < h && 0 <= y2 && y2 < w) {
            if (!visited[x2][y2] && s[x2][y2] == '.') {
              q.push(make_pair(x2, y2));
            }
          }
        }
      }
    }
  }
  cout << "-1\n";
}