Problem p03361 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define forin(i, a) for (auto i : (a))
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define in(...)                                                                \
  __VA_ARGS__;                                                                 \
  input__(__VA_ARGS__);
#define input(...) in(__VA_ARGS__)
#define out(...) output__(__VA_ARGS__);
#define output(...) out(__VA_ARGS__)
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](auto const &a, auto const &b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }
#define unless(a) if (!(a))

using namespace std;

using pii = pair<int, int>;

template <class... T> void input__(T &...a) {
  for (auto v : {&a...}) {
    cin >> *v;
  }
}

void output__() { cout << endl; }

template <class Head, class... T> void output__(Head const &v, T const &...a) {
  if (sizeof...(a) == 0) {
    cout << v;
  } else {
    cout << v << " ";
  }
  output__(a...);
}

struct State {
  int x, y, l;
};

int main() {
  int ndir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

  int in(h, w);
  string list_input(field, h);
  queue<State> que;
  rep(i, h) {
    rep(j, w) {
      if (field[i][j] == '#') {
        que.push({j, i, 1});
        int max_length = -1;
        while (!que.empty()) {
          State top = que.front();
          que.pop();

          if (field[top.y][top.x] != '#') {
            continue;
          }

          field[top.y][top.x] = '.';
          if (max_length < top.l) {
            max_length = top.l;
          }

          forin(d, ndir) {
            int nx = top.x + d[0];
            int ny = top.y + d[1];

            if (0 <= nx and nx < w and 0 <= ny and ny < h) {
              que.push({nx, ny, top.l + 1});
            }
          }
        }

        if (max_length <= 1) {
          out("No");
          exit(1);
        }
      }
    }
  }
  out("Yes");
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define forin(i, a) for (auto i : (a))
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define in(...)                                                                \
  __VA_ARGS__;                                                                 \
  input__(__VA_ARGS__);
#define input(...) in(__VA_ARGS__)
#define out(...) output__(__VA_ARGS__);
#define output(...) out(__VA_ARGS__)
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](auto const &a, auto const &b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }
#define unless(a) if (!(a))

using namespace std;

using pii = pair<int, int>;

template <class... T> void input__(T &...a) {
  for (auto v : {&a...}) {
    cin >> *v;
  }
}

void output__() { cout << endl; }

template <class Head, class... T> void output__(Head const &v, T const &...a) {
  if (sizeof...(a) == 0) {
    cout << v;
  } else {
    cout << v << " ";
  }
  output__(a...);
}

struct State {
  int x, y, l;
};

int main() {
  int ndir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

  int in(h, w);
  string list_input(field, h);
  queue<State> que;
  rep(i, h) {
    rep(j, w) {
      if (field[i][j] == '#') {
        que.push({j, i, 1});
        int max_length = -1;
        while (!que.empty()) {
          State top = que.front();
          que.pop();

          if (field[top.y][top.x] != '#') {
            continue;
          }

          field[top.y][top.x] = '.';
          if (max_length < top.l) {
            max_length = top.l;
          }

          forin(d, ndir) {
            int nx = top.x + d[0];
            int ny = top.y + d[1];

            if (0 <= nx and nx < w and 0 <= ny and ny < h) {
              que.push({nx, ny, top.l + 1});
            }
          }
        }

        if (max_length <= 1) {
          out("No");
          exit(0);
        }
      }
    }
  }
  out("Yes");
}