Problem p03804 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n - 1);
  return ans * n;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
bool is_prime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<char>> v(n, vector<char>(n));
  vector<vector<char>> u(m, vector<char>(m));
  rep(i, n) {
    rep(j, n) { cin >> v.at(i).at(j); }
  }
  rep(i, m) {
    rep(j, m) { cin >> u.at(i).at(j); }
  }
  string res = "No";
  rep(i, n - m) {
    rep(j, m) {
      if (v.at(i).at(j) == u.at(0).at(0)) {
        bool flag = 1;
        rep(k, m) {
          rep(l, m) {
            if (v.at(i + k).at(j + l) != u.at(k).at(l)) {
              flag = 0;
              break;
            }
          }
        }
        if (flag) {
          res = "Yes";
          break;
        }
      }
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

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;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n - 1);
  return ans * n;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
bool is_prime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<char>> v(n, vector<char>(n));
  vector<vector<char>> u(m, vector<char>(m));
  rep(i, n) {
    rep(j, n) { cin >> v.at(i).at(j); }
  }
  rep(i, m) {
    rep(j, m) { cin >> u.at(i).at(j); }
  }
  string res = "No";
  rep(i, n - m + 1) {
    rep(j, n - m + 1) {
      if (v.at(i).at(j) == u.at(0).at(0)) {
        bool flag = 1;
        rep(k, m) {
          rep(l, m) {
            if (v.at(i + k).at(j + l) != u.at(k).at(l)) {
              flag = 0;
              break;
            }
          }
        }
        if (flag) {
          res = "Yes";
          break;
        }
      }
    }
  }
  cout << res << endl;
}