Problem p00373 (C++)

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

int w1, w2, h1, h2, sum;
int hash_a[700][700][10], hash_b[700][10];
int cumo_a[700][700][10], cumo_b[700][10];
char s[700][700], t[700][700];

void make_table() {
  rep(i, h1) rep(j, w1 - w2 + 1) {
    rep(k, w2) {
      rep(l, 6) hash_a[i][j][(k * 6) / 60] =
          hash_a[i][j][(k * 6) / 60] * 2 + (((s[i][j + k] >> l) & 1) ? 1 : 0);
      rep(l, 6) cumo_a[i][j][(k * 6) / 60] =
          cumo_a[i][j][(k * 6) / 60] * 2 + (s[i][j + k] == 63 ? 1 : 0);
    }
  }
  rep(i, h2) {
    rep(j, w2) {
      rep(k, 6) hash_b[i][(j * 6) / 60] =
          hash_b[i][(j * 6) / 60] * 2 + (((t[i][j] >> k) & 1) ? 1 : 0);
      rep(k, 6) cumo_b[i][(j * 6) / 60] =
          cumo_b[i][(j * 6) / 60] * 2 + (t[i][j] == 63 ? 1 : 0);
    }
  }
}

void count_s() {
  rep(i, h1 - h2 + 1) {
    rep(j, w1 - w2 + 1) {
      rep(k, h2) {
        int p1[10] = {}, p2[10] = {};
        int flag = 0;
        rep(l, 10) {
          p1[l] = hash_a[i + k][j][l] ^ hash_b[k][l];
          p2[l] = cumo_a[i + k][j][l] | cumo_b[k][l];
          if ((p1[l] | p2[l]) != p2[l]) {
            flag++;
            break;
          }
        }
        if (flag)
          break;
        if (k == h2 - 1)
          sum++;
      }
    }
  }
}

void Za_a_tsu() {
  rep(i, h1) rep(j, w1) {
    if ('0' <= s[i][j] && s[i][j] <= '9')
      s[i][j] = (s[i][j] - '0');
    if ('a' <= s[i][j] && s[i][j] <= 'z')
      s[i][j] = (s[i][j] - 'a' + 11);
    if ('A' <= s[i][j] && s[i][j] <= 'Z')
      s[i][j] = (s[i][j] - 'A' + 37);
  }
  rep(i, h2) rep(j, w2) {
    if ('0' <= t[i][j] && t[i][j] <= '9')
      t[i][j] = (t[i][j] - '0');
    if ('a' <= t[i][j] && t[i][j] <= 'z')
      t[i][j] = (t[i][j] - 'a' + 11);
    if ('A' <= t[i][j] && t[i][j] <= 'Z')
      t[i][j] = (t[i][j] - 'A' + 37);
  }
}

signed main() {
  cin >> w1 >> h1 >> w2 >> h2;
  rep(i, h1) scanf("%s", s[i]);
  rep(i, h2) scanf("%s", t[i]);
  Za_a_tsu();
  make_table();
  count_s();
  cout << sum << endl;
}

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

int w1, w2, h1, h2, sum;
int hash_a[800][800][10], hash_b[800][10];
int cumo_a[800][800][10], cumo_b[800][10];
char s[800][700], t[800][800];

void make_table() {
  rep(i, h1) rep(j, w1 - w2 + 1) {
    rep(k, w2) {
      rep(l, 6) hash_a[i][j][(k * 6) / 60] =
          hash_a[i][j][(k * 6) / 60] * 2 + (((s[i][j + k] >> l) & 1) ? 1 : 0);
      rep(l, 6) cumo_a[i][j][(k * 6) / 60] =
          cumo_a[i][j][(k * 6) / 60] * 2 + (s[i][j + k] == 63 ? 1 : 0);
    }
  }
  rep(i, h2) {
    rep(j, w2) {
      rep(k, 6) hash_b[i][(j * 6) / 60] =
          hash_b[i][(j * 6) / 60] * 2 + (((t[i][j] >> k) & 1) ? 1 : 0);
      rep(k, 6) cumo_b[i][(j * 6) / 60] =
          cumo_b[i][(j * 6) / 60] * 2 + (t[i][j] == 63 ? 1 : 0);
    }
  }
}

void count_s() {
  rep(i, h1 - h2 + 1) {
    rep(j, w1 - w2 + 1) {
      rep(k, h2) {
        int p1[10] = {}, p2[10] = {};
        int flag = 0;
        rep(l, 10) {
          p1[l] = hash_a[i + k][j][l] ^ hash_b[k][l];
          p2[l] = cumo_a[i + k][j][l] | cumo_b[k][l];
          if ((p1[l] | p2[l]) != p2[l]) {
            flag++;
            break;
          }
        }
        if (flag)
          break;
        if (k == h2 - 1)
          sum++;
      }
    }
  }
}

void Za_a_tsu() {
  rep(i, h1) rep(j, w1) {
    if ('0' <= s[i][j] && s[i][j] <= '9')
      s[i][j] = (s[i][j] - '0');
    if ('a' <= s[i][j] && s[i][j] <= 'z')
      s[i][j] = (s[i][j] - 'a' + 11);
    if ('A' <= s[i][j] && s[i][j] <= 'Z')
      s[i][j] = (s[i][j] - 'A' + 37);
  }
  rep(i, h2) rep(j, w2) {
    if ('0' <= t[i][j] && t[i][j] <= '9')
      t[i][j] = (t[i][j] - '0');
    if ('a' <= t[i][j] && t[i][j] <= 'z')
      t[i][j] = (t[i][j] - 'a' + 11);
    if ('A' <= t[i][j] && t[i][j] <= 'Z')
      t[i][j] = (t[i][j] - 'A' + 37);
  }
}

signed main() {
  cin >> w1 >> h1 >> w2 >> h2;
  rep(i, h1) scanf("%s", s[i]);
  rep(i, h2) scanf("%s", t[i]);
  Za_a_tsu();
  make_table();
  count_s();
  cout << sum << endl;
}