Problem p03436 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string>
#include <vector>

// #define int long long

using namespace std;

int grid[53][53];

struct s {
  int x;
  int y;
};

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

  int w, h;
  cin >> h >> w;

  string str;
  int white = 0;

  for (int hh = 0; hh < h; ++hh) {
    cin >> str;

    for (int ww = 0; ww < w; ++ww) {
      grid[hh + 1][ww + 1] = str[ww] == '#' ? -1 : 0;
      white += (str[ww] == '#' ? 0 : 1);
    }
  }
  // マップの周りを埋める
  for (int ww = 0; ww < w + 2; ++ww) {
    grid[0][ww] = -1;
    grid[h + 1][ww] = -1;
  }

  // マップの周りを埋める
  for (int hh = 0; hh < h + 2; ++hh) {
    grid[hh][0] = -1;
    grid[hh][w + 1] = -1;
  }

  queue
 qu;

  // スタート地点をqueueに積む
  qu.push({1, 1});

  for (;;) {
    // 1番後ろを取り出す
    s s = qu.front();
    qu.pop();
    const auto n = grid[s.y][s.x];

    // cout << s.x << "<x | y> " << s.y << " n:" << s.n << endl;

    int x = 0, y = 0;
    x = s.x - 1;
    y = s.y - 0;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    x = s.x - 0;
    y = s.y - 1;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    x = s.x + 1;
    y = s.y + 0;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    x = s.x + 0;
    y = s.y + 1;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    // ゴールに値が入ったら終わり
    if (s.y == h && s.x == w) {
      break;
    }

    // for (int hh = 0; hh < h + 2; ++hh)
    //{
    //     for (int ww = 0; ww < w + 2; ++ww)
    //     {
    //         cout << grid[hh][ww] + 1;
    //     }
    //     cout << '\n';
    // }
    // cout << '\n';
  }

  cout << white - grid[h][w] - 1 << endl;

  // デバッグ出力
  /*
  for (int h = 0; h < h + 2; ++h)
  {
      for (int w = 0; w < w + 2; ++w)
      {
          cout << grid[h][w] + 1;
      }
      cout << '\n';
  }
  */
}


Fixed submission:
#include <iostream>
#include <queue>
#include <string>
#include <vector>

// #define int long long

using namespace std;

int grid[53][53];

struct s {
  int x;
  int y;
};

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

  int w, h;
  cin >> h >> w;

  string str;
  int white = 0;

  for (int hh = 0; hh < h; ++hh) {
    cin >> str;

    for (int ww = 0; ww < w; ++ww) {
      grid[hh + 1][ww + 1] = str[ww] == '#' ? -1 : 0;
      white += (str[ww] == '#' ? 0 : 1);
    }
  }
  // マップの周りを埋める
  for (int ww = 0; ww < w + 2; ++ww) {
    grid[0][ww] = -1;
    grid[h + 1][ww] = -1;
  }

  // マップの周りを埋める
  for (int hh = 0; hh < h + 2; ++hh) {
    grid[hh][0] = -1;
    grid[hh][w + 1] = -1;
  }

  queue
 qu;

  // スタート地点をqueueに積む
  qu.push({1, 1});

  for (;;) {
    if (qu.empty()) {
      cout << -1 << endl;
      return 0;
    }

    // 1番後ろを取り出す
    s s = qu.front();
    qu.pop();
    const auto n = grid[s.y][s.x];

    // cout << s.x << "<x | y> " << s.y << " n:" << s.n << endl;

    int x = 0, y = 0;
    x = s.x - 1;
    y = s.y - 0;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    x = s.x - 0;
    y = s.y - 1;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    x = s.x + 1;
    y = s.y + 0;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    x = s.x + 0;
    y = s.y + 1;
    if (grid[y][x] == 0) {
      qu.push({x, y});
      grid[y][x] = n + 1;
    }

    // ゴールに値が入ったら終わり
    if (s.y == h && s.x == w) {
      break;
    }

    // for (int hh = 0; hh < h + 2; ++hh)
    //{
    //     for (int ww = 0; ww < w + 2; ++ww)
    //     {
    //         cout << grid[hh][ww] + 1;
    //     }
    //     cout << '\n';
    // }
    // cout << '\n';
  }

  cout << white - grid[h][w] - 1 << endl;

  // デバッグ出力
  /*
  for (int h = 0; h < h + 2; ++h)
  {
      for (int w = 0; w < w + 2; ++w)
      {
          cout << grid[h][w] + 1;
      }
      cout << '\n';
  }
  */
}