let () = Scanf.scanf "%d\n" @@ fun n ->
  let ss = Array.init n @@ fun _ -> Scanf.scanf "%s\n" @@ fun s -> s in
  Printf.printf "%d\n" @@
    Array.fold_left (List.fold_left (fun acc (a, b, w) ->
      if
        List.for_all (fun i ->
          List.for_all (fun j ->
            ss.((i + a) mod n).[(j + b) mod n] = ss.((j + a) mod n).[(i + b) mod n]) @@
            Array.to_list @@
              Array.init i @@ fun j -> j) @@
            Array.to_list @@
              Array.init (n - 1) @@ fun i -> i + 1
      then acc + w
      else acc)) 0 @@
    Array.init n @@ function
      | 0 -> [(0, 0, n)]
      | i -> [(i, 0, n - i); (0, i, n - i)]