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 Scala (2.11.7) (judge verdict: Time Limit Exceeded)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val S = Array.ofDim[Char](2*N, 2*N)

  for (y <- 1 to N){
    val s = in.next()
    for (x <- 1 to N){
      S(y)(x) = s(x-1)
    }
  }

  for (y <- N+1 to 2*N-1){
    for (x <- 1 to N){
      S(y)(x) = S(y-N)(x)
    }
  }
  for (y <- 1 to N){
    for (x <- N+1 to 2*N-1){
      S(y)(x) = S(y)(x-N)
    }
  }
  for (y <- N+1 to 2*N-1){
    for (x <- N+1 to 2*N-1){
      S(y)(x) = S(y-N)(x-N)
    }
  }


  // process
  var ans = 0
  var ny = 1
  var nx = 1

  val dp = Array.ofDim[Long](2*N, 2*N)
  var tmp = 42L
  for(y <- 1 to 2*N-1; x <- y+1 to 2*N-1) {
    if(S(y)(x) == S(x)(y)) {
      dp(y)(x) = tmp
    }
  }
  while(ny <= N && nx <= N) {
    var flag = true
    for (y <- ny to ny+N-1; x <- y+1 to ny+N-1) {
      if(dp(y)(x) != tmp) {
        flag = false
      }
    }
    if(flag)
      ans += 1
    ny += 1
    nx += 1
  }


  //// for y
  for(sty <- 2 to N) {
    tmp += 1

    for(y <- sty to 2*N-1; x <- 1 to 2*N-1 - (sty-1)) {
      if(S(y)(x) == S(x+(sty-1))(y-(sty-1))) {
        dp(y)(x) = tmp
      }
    }

    ny = sty; nx = 1
    while(ny <= N && nx <= N) {
      var flag = true
      for (y <- ny to ny+N-1; x <- nx + y-ny+1 to nx+N-1) {
        if(dp(y)(x) != tmp) {
          flag = false
        }
      }
      if(flag){
        ans += 1
      }
      
      ny += 1; nx += 1
    }

  }


  //// for x
  for(stx <- 2 to N) {
    tmp += 1

    for(x <- stx to 2*N-1; y <- 1 to 2*N-1 - (stx-1)) {
      if(S(y)(x) == S(x-(stx-1))(y+(stx-1))) {
        dp(y)(x) = tmp
      }
    }
    
    ///
    nx = stx; ny = 1
    while(ny <= N && nx <= N) {
      var flag = true

      for (x <- nx to nx+N-1; y <- ny + x-nx+1 to ny+N-1) {
        if(dp(y)(x) != tmp) {
          flag = false
        }
      }
      if(flag){
        ans += 1
      }
      
      ny += 1; nx += 1
    }

  }


  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}