Problem:
Score : 500 points

Problem Statement

Snuke has two boards, each divided into a grid with N rows and N columns.
For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j).

There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i,j}. On the second board, nothing is written yet.

Snuke will write letters on the second board, as follows:

First, choose two integers A and B ( 0 \leq A, B < N ).

Write one letter in each square on the second board.
Specifically, write the letter written in Square ( i+A, j+B ) on the first board into Square (i,j) on the second board.
Here, the k-th row is also represented as the (N+k)-th row, and the k-th column is also represented as the (N+k)-th column.

After this operation, the second board is called a good board when, for every i and j ( 1 \leq i, j \leq N ), the letter in Square (i,j) and the letter in Square (j,i) are equal.

Find the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Constraints

1 \leq N \leq 300

S_{i,j} ( 1 \leq i, j \leq N ) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
S_{1,1}S_{1,2}..S_{1,N}
S_{2,1}S_{2,2}..S_{2,N}
:
S_{N,1}S_{N,2}..S_{N,N}

Output

Print the number of the ways to choose integers A and B ( 0 \leq A, B < N ) such that the second board is a good board.

Sample Input 1

2
ab
ca

Sample Output 1

2

For each pair of A and B, the second board will look as shown below:

The second board is a good board when (A,B) = (0,1) or (A,B) = (1,0), thus the answer is 2.

Sample Input 2

4
aaaa
aaaa
aaaa
aaaa

Sample Output 2

16

Every possible choice of A and B makes the second board good.

Sample Input 3

5
abcde
fghij
klmno
pqrst
uvwxy

Sample Output 3

0

No possible choice of A and B makes the second board good.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

// 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;

// Rolling Hash
// Verified: AOJ 2444 (Substring)

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;
}

struct RollingHash {
public:
    long long base_1, base_2, mod_1, mod_2;
    vector<long long> hash1, hash2, pow_hash;
    long long pow;

    // init (constructor)
    RollingHash() {}
    RollingHash(string s, int N) : base_1(1013), base_2(1007),
        mod_1(1000000007), mod_2(1000000009),
        hash1(s.length()+1,0), hash2(s.length()+1,0),
        pow_hash(s.length()+1,0), pow(1) {
            pow = mod_pow(base_1, N, mod_1);
            for(int i=0; i<s.length(); i++) {
                hash1[i+1] = (hash1[i]+s[i]) * base_1 % mod_1;
                // hash2[i+1] = (hash2[i]+s[i]) * base_2 % mod_2;
                // pow1[i+1] = pow1[i] * base_1 % mod_1;
                // pow2[i+1] = pow2[i] * base_2 % mod_2;
            }

            for(int i=0; i<=s.length(); i++) {
                pow_hash[i] = (hash1[i] * pow) % mod_1;
            }
        }

    // calc str[l,r) hash (0-indexed)
    // if get(i1,j1) == get(i2,j2) -> identical string
    pair<long long, long long> get(int l, int r) {
        long long t1 = (hash1[r] - pow_hash[l] + mod_1) % mod_1;
        // long long t2 = ((hash2[r] - hash2[l] * pow2[r-l]) % mod_2 + mod_2) % mod_2;
        long long t2 = 1;
        return make_pair(t1, t2);
    }
};

using pll = pair<long long, long long>;
 
signed main() {
    int N; cin >> N;
    vector<string> vs(N);
    rep(i,0,N) cin >> vs[i];

    vector<string> pat(2*N-1, ""), inv(2*N-1, "");
    rep(i,0,2*N-1) {
        int idx = (i % N);
        pat[i] = vs[idx] + vs[idx].substr(0, N-1);
        // cerr << "pat: " << pat[i] << endl;
    }

    rep(j,0,2*N-1) rep(i,0,2*N-1) {
        inv[j] += pat[i][j];
    }

    /*
    // cout << "Pattern generate end" << endl;
    rep(i,0,2*N-1) {
        // cerr << "inv: " << inv[i] << endl;
    }
    */

    vector<RollingHash> rh_pat(2*N-1), rh_inv(2*N-1);
    rep(i,0,2*N-1) {
        rh_pat[i] = RollingHash(pat[i], N);
        rh_inv[i] = RollingHash(inv[i], N);
    }

    // cout << "Rolling Hash end" << endl;

    int ans = 0;
    rep(i,0,N) rep(j,0,N) {
        bool ok = true;
        // printf("i = %lld, j = %lld\n", i, j);
        rep(k,0,N) {
            int row_l = i+k, col_l = j;
            int row_r = j+k, col_r = i;

            pll vl = rh_pat[row_l].get(col_l, col_l+N);
            pll vr = rh_inv[row_r].get(col_r, col_r+N);

            if(vl != vr) {
                ok = false;
                break;
            }
        }
        if(ok) ans++;
    }
    cout << ans << endl;
    return 0;
}