isgood :: Int -> Int -> [[Char]] -> Bool
isgood a b xs = foldr (&&) True [sec!!j!!i == sec!!i!!j | j <- [0..len], i <- [0..len]]
    where
      len = (length xs) - 1
      sec = [[xs!!(mod (i+a) (len+1))!!(mod (j+b) (len+1)) | j <- [0..len]] | i <- [0..len]]

solve :: [[Char]] -> Int
solve xs = sum $ [1 | a <- [0..len], b <- [0..len], isgood a b xs]
    where
      len = (length xs) - 1

transform :: [String] -> [[Char]]
transform (_:xs) = xs

main :: IO ()
main = print . solve . transform . words =<< getContents