Problem p03364 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 10;
#define ULL unsigned long long
ULL maxSize = 4000007;
ULL seedx = 233;
ULL seedy = 23333;
struct HashString {
  ULL xp[maxn];
  ULL yp[maxn];
  ULL H[maxn][maxn];
  char s[maxn][maxn];
  int x, y;
  void init(int n, int m) {
    xp[0] = yp[0] = 1;
    x = n, y = m;
    for (int i = 1; i <= x; i++)
      xp[i] = xp[i - 1] * seedx;
    for (int i = 1; i <= y; i++)
      yp[i] = yp[i - 1] * seedy;
    return;
  }
  void initHash() {
    for (int i = 0; i < x; i++) {
      H[i][0] = s[i][0] - 'A' + 1;
      for (int j = 1; j < y; j++)
        H[i][j] = H[i][j - 1] * seedy + s[i][j] - 'A' + 1;
    }
    for (int i = 1; i < x; i++)
      for (int j = 0; j < y; j++)
        H[i][j] += H[i - 1][j] * seedx;
    return;
  }
  ULL askHash(int x1, int y1, int x2, int y2) {
    ULL ret = H[x2][y2];
    if (x1 > 0)
      ret -= H[x1 - 1][y2] * xp[x2 - x1 + 1];
    if (y1 > 0)
      ret -= H[x2][y1 - 1] * yp[y2 - y1 + 1];
    if (x1 > 0 && y1 > 0)
      ret += H[x1 - 1][y1 - 1] * xp[x2 - x1 + 1] * yp[y2 - y1 + 1];
    return ret;
  }
} s1, s2;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  s1.init(2 * n, 2 * n);
  s2.init(2 * n, 2 * n);
  for (int i = 0; i < n; i++)
    cin >> s1.s[i];
  for (int i = n; i < 2 * n; i++) {
    for (int j = 0; j < n; j++) {
      s1.s[j][i] = s1.s[j][i - n];
    }
  }
  for (int i = n; i < 2 * n; i++) {
    for (int j = 0; j < 2 * n; j++) {
      s1.s[i][j] = s1.s[i - n][j];
    }
  }
  for (int i = 0; i < 2 * n; i++) {
    for (int j = 0; j < 2 * n; j++) {
      s2.s[i][j] = s1.s[j][i];
    }
  }
  s1.initHash();
  s2.initHash();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (s1.askHash(i, j, i + n - 1, j + n - 1) ==
          s2.askHash(j, i, j + n - 1, i + n - 1))
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
const int maxn = 900;
#define ULL unsigned long long
ULL maxSize = 4000007;
ULL seedx = 233;
ULL seedy = 23333;
struct HashString {
  ULL xp[maxn];
  ULL yp[maxn];
  ULL H[maxn][maxn];
  char s[maxn][maxn];
  int x, y;
  void init(int n, int m) {
    xp[0] = yp[0] = 1;
    x = n, y = m;
    for (int i = 1; i <= x; i++)
      xp[i] = xp[i - 1] * seedx;
    for (int i = 1; i <= y; i++)
      yp[i] = yp[i - 1] * seedy;
    return;
  }
  void initHash() {
    for (int i = 0; i < x; i++) {
      H[i][0] = s[i][0] - 'A' + 1;
      for (int j = 1; j < y; j++)
        H[i][j] = H[i][j - 1] * seedy + s[i][j] - 'A' + 1;
    }
    for (int i = 1; i < x; i++)
      for (int j = 0; j < y; j++)
        H[i][j] += H[i - 1][j] * seedx;
    return;
  }
  ULL askHash(int x1, int y1, int x2, int y2) {
    ULL ret = H[x2][y2];
    if (x1 > 0)
      ret -= H[x1 - 1][y2] * xp[x2 - x1 + 1];
    if (y1 > 0)
      ret -= H[x2][y1 - 1] * yp[y2 - y1 + 1];
    if (x1 > 0 && y1 > 0)
      ret += H[x1 - 1][y1 - 1] * xp[x2 - x1 + 1] * yp[y2 - y1 + 1];
    return ret;
  }
} s1, s2;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  s1.init(2 * n, 2 * n);
  s2.init(2 * n, 2 * n);
  for (int i = 0; i < n; i++)
    cin >> s1.s[i];
  for (int i = n; i < 2 * n; i++) {
    for (int j = 0; j < n; j++) {
      s1.s[j][i] = s1.s[j][i - n];
    }
  }
  for (int i = n; i < 2 * n; i++) {
    for (int j = 0; j < 2 * n; j++) {
      s1.s[i][j] = s1.s[i - n][j];
    }
  }
  for (int i = 0; i < 2 * n; i++) {
    for (int j = 0; j < 2 * n; j++) {
      s2.s[i][j] = s1.s[j][i];
    }
  }
  s1.initHash();
  s2.initHash();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (s1.askHash(i, j, i + n - 1, j + n - 1) ==
          s2.askHash(j, i, j + n - 1, i + n - 1))
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}