Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \
  int aa;                                                                      \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecpl(p)                                                               \
  long long aa;                                                                \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecps(p)                                                               \
  string aa;                                                                   \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecp2(p)                                                               \
  int ab;                                                                      \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define vecpl2(p)                                                              \
  long long ab;                                                                \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define vecps2(p)                                                              \
  string ab;                                                                   \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define set0(k, n)                                                             \
  for (int nn = 0; nn < (n); nn++) {                                           \
    (k).push_back(0);                                                          \
  }
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define vecsize(b, size) vector<int>(b)((size));
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;

int main() {

  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  fx(H) { cin >> S.at(x); }
  int road = 0;
  fx(W) {
    fy(H) {
      if (S.at(y).at(x) == '.') {
        road++;
      }
    }
  }

  queue<int> ax;
  queue<int> ay;
  ax.push(1);
  ay.push(1);
  int field[51][51];

  fx(52) {
    fy(52) { field[x][y] = 5000; }
  }
  field[1][1] = 1;
  while (ax.size() > 0) {
    int x = ax.front();
    int y = ay.front();
    ax.pop();
    ay.pop();
    if (x + 1 <= H && S.at(x).at(y - 1) == '.') {
      if (field[x + 1][y] > field[x][y] + 1) {
        field[x + 1][y] = field[x][y] + 1;
        ax.push(x + 1);
        ay.push(y);
      }
    }
    if (x - 1 > 0 && S.at(x - 2).at(y - 1) == '.') {
      if (field[x - 1][y] > field[x][y] + 1) {
        field[x - 1][y] = field[x][y] + 1;
        ax.push(x - 1);
        ay.push(y);
      }
    }

    if (y + 1 <= W && S.at(x - 1).at(y) == '.') {
      if (field[x][y + 1] > field[x][y] + 1) {
        field[x][y + 1] = field[x][y] + 1;
        ax.push(x);
        ay.push(y + 1);
      }
    }
    if (y - 1 > 0 && S.at(x - 1).at(y - 2) == '.') {
      if (field[x][y - 1] > field[x][y] + 1) {
        field[x][y - 1] = field[x][y] + 1;
        ax.push(x);
        ay.push(y - 1);
      }
    }
  }
  if (road - field[H][W] < 0) {
    ce(-1)
  } else {
    ce(road - field[H][W])
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \
  int aa;                                                                      \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecpl(p)                                                               \
  long long aa;                                                                \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecps(p)                                                               \
  string aa;                                                                   \
  cin >> aa;                                                                   \
  (p).push_back(aa);
#define vecp2(p)                                                               \
  int ab;                                                                      \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define vecpl2(p)                                                              \
  long long ab;                                                                \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define vecps2(p)                                                              \
  string ab;                                                                   \
  cin >> ab;                                                                   \
  (p).push_back(ab);
#define set0(k, n)                                                             \
  for (int nn = 0; nn < (n); nn++) {                                           \
    (k).push_back(0);                                                          \
  }
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define vecsize(b, size) vector<int>(b)((size));
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;

int main() {

  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  fx(H) { cin >> S.at(x); }
  int road = 0;
  fx(W) {
    fy(H) {
      if (S.at(y).at(x) == '.') {
        road++;
      }
    }
  }

  queue<int> ax;
  queue<int> ay;
  ax.push(1);
  ay.push(1);
  int field[60][60];

  fx(52) {
    fy(52) { field[x][y] = 5000; }
  }
  field[1][1] = 1;
  while (ax.size() > 0) {
    int x = ax.front();
    int y = ay.front();
    ax.pop();
    ay.pop();
    if (x + 1 <= H && S.at(x).at(y - 1) == '.') {
      if (field[x + 1][y] > field[x][y] + 1) {
        field[x + 1][y] = field[x][y] + 1;
        ax.push(x + 1);
        ay.push(y);
      }
    }
    if (x - 1 > 0 && S.at(x - 2).at(y - 1) == '.') {
      if (field[x - 1][y] > field[x][y] + 1) {
        field[x - 1][y] = field[x][y] + 1;
        ax.push(x - 1);
        ay.push(y);
      }
    }

    if (y + 1 <= W && S.at(x - 1).at(y) == '.') {
      if (field[x][y + 1] > field[x][y] + 1) {
        field[x][y + 1] = field[x][y] + 1;
        ax.push(x);
        ay.push(y + 1);
      }
    }
    if (y - 1 > 0 && S.at(x - 1).at(y - 2) == '.') {
      if (field[x][y - 1] > field[x][y] + 1) {
        field[x][y - 1] = field[x][y] + 1;
        ax.push(x);
        ay.push(y - 1);
      }
    }
  }
  if (road - field[H][W] < 0) {
    ce(-1)
  } else {
    ce(road - field[H][W])
  }
}