Problem p03804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (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 RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define optimize_cin()                                                         \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define debug(x) std::cerr << #x << " : " << x << "\n";
using namespace std;
using lint = long long;
template <class T> using grid = vector<vector<T>>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const lint MOD = (lint)1e9 + 7;

int main() {
  int N, M;
  cin >> N >> M;
  vector<string> A(N);
  vector<string> B(M);
  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }

  debug(A.size());
  debug(B.size());
  debug(M);
  debug(N - M);

  debug(A[N - M - 1][N - M - 1]);
  rep(ay, N - M + 1) {
    rep(ax, N - M + 1) {
      int cnt = 0;
      rep(by, M) {
        rep(bx, M) {
          if (A[ay + by][ax + bx] == B[by][bx]) {
            cnt++;
          }
        }
      }
      if (cnt == M * M) {
        cout << "Yes"
             << "\n";
        return 0;
      }
    }
  }

  cout << "No"
       << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (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 RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define optimize_cin()                                                         \
  cin.tie(0);                                                                  \
  ios::sync_with_stdio(false)
#define debug(x) std::cerr << #x << " : " << x << "\n";
using namespace std;
using lint = long long;
template <class T> using grid = vector<vector<T>>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const lint MOD = (lint)1e9 + 7;

int main() {
  int N, M;
  cin >> N >> M;
  vector<string> A(N);
  vector<string> B(M);
  rep(i, N) { cin >> A[i]; }
  rep(i, M) { cin >> B[i]; }

  debug(A.size());
  debug(B.size());
  debug(M);
  debug(N - M);

  //    if (N == 1 && M == 1)
  //    {
  //        if (A[0] == B[0])
  //        {
  //            cout << "Yes\n";
  //            return 0;
  //        }
  //        else
  //        {
  //            cout << "No\n";
  //            return 0;
  //        }
  //    }

  rep(ay, N - M + 1) {
    rep(ax, N - M + 1) {
      int cnt = 0;
      rep(by, M) {
        rep(bx, M) {
          if (A[ay + by][ax + bx] == B[by][bx]) {
            cnt++;
          }
        }
      }
      if (cnt == M * M) {
        cout << "Yes"
             << "\n";
        return 0;
      }
    }
  }

  cout << "No"
       << "\n";

  return 0;
}