Problem p03364 (C++)

Buggy submission (status: Runtime Error):
// Create Time: 2018-04-28 20:23:17
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define snuke(i, x)                                                            \
  for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); ++i)
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, n, a) for (int i = (n)-1; i >= (a); i--)
// #define OUT(x) std::cout<<(#x)<<":"<<(x)<<std::endl
// #define TST(x) std::cout<<(#x)<<":"<<(x)<<" "
#define OUT(args...)                                                           \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    OUT_TST("\n", _it, args);                                                  \
  }
#define TST(args...)                                                           \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    OUT_TST("", _it, args);                                                    \
  }
#define SZ(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define unq(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())
#define mem(f, x) memset((f), (x), sizeof(f))
#define clr(f) mem(f, 0)
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define eb emplace_back
#define DEBUG
void OUT_TST(string s, istream_iterator<string> it) { cerr << s; }
template <typename T, typename... Args>
void OUT_TST(string s, istream_iterator<string> it, T a, Args... args) {
  cerr << *it << "=" << a << " ";
  OUT_TST(s, ++it, args...);
}
template <class T> void rd(T &ret) {
  ret = 0;
  bool ok = 0, u = 0;
  for (;;) {
    int c = getchar();
    if (c >= '0' && c <= '9')
      ret = (ret << 3) + (ret << 1) + c - '0', ok = 1;
    else if (c == '-')
      u = 1;
    else if (ok) {
      if (u)
        ret *= -1;
      return;
    }
  }
}
long long powmod(long long p, long long n, long long mod) {
  long long ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * p % mod;
    p = p * p % mod;
  }
  return ret;
}
template <class T> bool chmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef tuple<int, int, int> tp;
/****head****/
const ll sd = 257;
const int N = 303;
const ll mod = (ll)1e9 + 7;
ll vn[N][N], hn[N][N], pn[N];
char bd[N][N];
int n;
void init() {
  pn[0] = 1;
  rep(i, 1, 2 * n) pn[i] = pn[i - 1] * sd % mod;
  rep(i, 0, 2 * n) {
    hn[i][0] = bd[i][0];
    rep(j, 1, 2 * n) hn[i][j] = (hn[i][j - 1] * sd + bd[i][j]) % mod;
  }
  rep(i, 0, 2 * n) {
    vn[i][0] = bd[0][i];
    rep(j, 1, 2 * n) vn[i][j] = (vn[i][j - 1] * sd + bd[j][i]) % mod;
  }
}
void copy(int x, int y) {
  for (int i = 0, a = x; i < n; ++i, ++a) {
    for (int j = 0, b = y; j < n; ++j, ++b) {
      bd[a][b] = bd[i][j];
    }
  }
}
ll geth(int i, int l, int r, ll ha[N][N]) { // 0 for hn, 1 for vn
  if (l == 0)
    return ha[i][r];
  ll ret = (ha[i][r] - ha[i][l - 1] * pn[r - l + 1]) % mod;
  if (ret < 0)
    ret += mod;
  return ret;
}
bool ok(int x, int y) {
  rep(i, 0, n) {
    if (geth(x + i, y, y + n - 1, hn) != geth(y + i, x, x + n - 1, vn))
      return 0;
  }
  return 1;
}
int main() {
  cin >> n;
  rep(i, 0, n) scanf("%s", bd[i]);
  copy(0, n);
  copy(n, 0);
  copy(n, n);
  init();
  int ans = 0;
  rep(i, 0, n) rep(j, 0, n) if (ok(i, j))++ ans;
  cout << ans << endl;
  return 0;
}


Fixed submission:
// Create Time: 2018-04-28 20:23:17
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
#define snuke(i, x)                                                            \
  for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); ++i)
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, n, a) for (int i = (n)-1; i >= (a); i--)
// #define OUT(x) std::cout<<(#x)<<":"<<(x)<<std::endl
// #define TST(x) std::cout<<(#x)<<":"<<(x)<<" "
#define OUT(args...)                                                           \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    OUT_TST("\n", _it, args);                                                  \
  }
#define TST(args...)                                                           \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    OUT_TST("", _it, args);                                                    \
  }
#define SZ(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define unq(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())
#define mem(f, x) memset((f), (x), sizeof(f))
#define clr(f) mem(f, 0)
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define eb emplace_back
#define DEBUG
void OUT_TST(string s, istream_iterator<string> it) { cerr << s; }
template <typename T, typename... Args>
void OUT_TST(string s, istream_iterator<string> it, T a, Args... args) {
  cerr << *it << "=" << a << " ";
  OUT_TST(s, ++it, args...);
}
template <class T> void rd(T &ret) {
  ret = 0;
  bool ok = 0, u = 0;
  for (;;) {
    int c = getchar();
    if (c >= '0' && c <= '9')
      ret = (ret << 3) + (ret << 1) + c - '0', ok = 1;
    else if (c == '-')
      u = 1;
    else if (ok) {
      if (u)
        ret *= -1;
      return;
    }
  }
}
long long powmod(long long p, long long n, long long mod) {
  long long ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * p % mod;
    p = p * p % mod;
  }
  return ret;
}
template <class T> bool chmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool chmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef tuple<int, int, int> tp;
/****head****/
const ll sd = 257;
const int N = 606;
const ll mod = (ll)1e9 + 7;
ll vn[N][N], hn[N][N], pn[N];
char bd[N][N];
int n;
void init() {
  pn[0] = 1;
  rep(i, 1, 2 * n) pn[i] = pn[i - 1] * sd % mod;
  rep(i, 0, 2 * n) {
    hn[i][0] = bd[i][0];
    rep(j, 1, 2 * n) hn[i][j] = (hn[i][j - 1] * sd + bd[i][j]) % mod;
  }
  rep(i, 0, 2 * n) {
    vn[i][0] = bd[0][i];
    rep(j, 1, 2 * n) vn[i][j] = (vn[i][j - 1] * sd + bd[j][i]) % mod;
  }
}
void copy(int x, int y) {
  for (int i = 0, a = x; i < n; ++i, ++a) {
    for (int j = 0, b = y; j < n; ++j, ++b) {
      bd[a][b] = bd[i][j];
    }
  }
}
ll geth(int i, int l, int r, ll ha[N][N]) { // 0 for hn, 1 for vn
  if (l == 0)
    return ha[i][r];
  ll ret = (ha[i][r] - ha[i][l - 1] * pn[r - l + 1]) % mod;
  if (ret < 0)
    ret += mod;
  return ret;
}
bool ok(int x, int y) {
  rep(i, 0, n) {
    if (geth(x + i, y, y + n - 1, hn) != geth(y + i, x, x + n - 1, vn))
      return 0;
  }
  return 1;
}
int main() {
  cin >> n;
  rep(i, 0, n) scanf("%s", bd[i]);
  copy(0, n);
  copy(n, 0);
  copy(n, n);
  init();
  int ans = 0;
  rep(i, 0, n) rep(j, 0, n) if (ok(i, j))++ ans;
  cout << ans << endl;
  return 0;
}