shift :: [a] -> Int -> [a]
shift l n = drop n l  ++ take n l

transpose :: [[a]] -> [[a]]
transpose ([]:_) = []
transpose xs = map head xs : transpose (map tail xs)

convert :: Int -> Int -> [[Char]] -> [[Char]]
convert a b xs = shift (map (flip shift b) xs) a

isgood :: [[Char]] -> Bool
isgood sec = transpose sec == sec

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

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

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