Problem p03361 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 62;
const int INF = 1 << 30;
const double PI = acos(-1);
int func(int h, int w, vector<string> &s, vector<vector<int>> &has, int c) {
  int res = c;
  // clog<<h<<" "<<w<<endl;
  if (has[w][h] == 0) {
    has[w][h] = 1;
    for (int nh = h - 1; nh <= h + 1; nh++) {
      for (int nw = w - 1; nw <= w + 1; nw++) {
        if (nh < 0 or nw < 0 or nh >= s.size() or nw >= s[0].size() or
            not(nh == h xor nw == w)) {
          continue;
        }
        if (s[nh][nw] == '#') {
          chmax(res, func(nh, nw, s, has, c + 1));
        }
      }
    }
  }
  //	clog<<h<<" "<<w<<" - "<<res<<endl;
  return res;
}
int main() {
  int h, w;
  bool ans = true;
  cin >> h >> w;
  vector<string> s(h);
  vector<vector<int>> has(h, vector<int>(w, 0));
  rep(i, h) { cin >> s[i]; }
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '#' and has[i][j] == 0) {
        if (func(i, j, s, has, 0) == 0) {
          ans = false;
        }
      } else {
        has[i][j] = 1;
      }
    }
  }
  cout << solve(ans) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 62;
const int INF = 1 << 30;
const double PI = acos(-1);
int func(int h, int w, vector<string> &s, vector<vector<int>> &has, int c) {
  int res = c;
  // clog<<h<<" "<<w<<endl;
  if (has[h][w] == 0) {
    has[h][w] = 1;
    for (int nh = h - 1; nh <= h + 1; nh++) {
      for (int nw = w - 1; nw <= w + 1; nw++) {
        if (nh < 0 or nw < 0 or nh >= s.size() or nw >= s[0].size() or
            not(nh == h xor nw == w)) {
          continue;
        }
        if (s[nh][nw] == '#') {
          chmax(res, func(nh, nw, s, has, c + 1));
        }
      }
    }
  }
  //	clog<<h<<" "<<w<<" - "<<res<<endl;
  return res;
}
int main() {
  int h, w;
  bool ans = true;
  cin >> h >> w;
  vector<string> s(h);
  vector<vector<int>> has(h, vector<int>(w, 0));
  rep(i, h) { cin >> s[i]; }
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '#' and has[i][j] == 0) {
        if (func(i, j, s, has, 0) == 0) {
          ans = false;
        }
      } else {
        has[i][j] = 1;
      }
    }
  }
  cout << solve(ans) << endl;
}