Language: C++

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int N;
    std::cin >> N;
    if (N == 1) {
        std::cout << 1 << std::endl;
        return 0;
    }
    std::vector<std::vector<char>> input(N, std::vector<char>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            std::cin >> input[i][j];
        }
    }
    /*{
        std::vector<std::vector<char>> input3(N, std::vector<char>(N));
        int count = 0;
        for (int x = 0; x < N; ++x) {
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < N; ++j) {
                    input3[i][j] = input[(i + x) % N][j];
                }
            }
            bool ok = true;
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < i; ++j) {
                    if (input3[(i + x) % N][j] != input3[(j + x) % N][i]) {
                        ok = false;
                        break;
                    }
                }
            }
            if (ok) {
                ++count;
            }
        }
        std::cout << count * N << std::endl;
    }*/
    if (N == 2) {
        if (input[0][0] == input[1][1] && input[0][1] == input[1][0]) {
            std::cout << 4 << std::endl;
        } else if (input[0][0] == input[1][1] || input[0][1] == input[1][0]) {
            std::cout << 2 << std::endl;
        } else {
            std::cout << 0 << std::endl;
        }
        return 0;
    }
    std::vector<std::vector<char>> input2(N, std::vector<char>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            input2[i][j] = input[i][(j + N - i) % N];
        }
    }
    if (std::equal(input2[0].begin(), input2[0].end(), input2[1].begin())) {
        bool ok = true;
        for (int i = 2; i < N; ++i) {
            if (!std::equal(input2[0].begin(), input2[0].end(), input2[i].begin())) {
                ok = false;
                break;
            }
        }
        if (ok) {
            std::cout << N * N << std::endl;
            return 0;
        }
    }
    if (N % 2 == 0 && std::equal(input2[0].begin(), input2[0].end(), input2[N / 2].begin())) {
        bool ok = true;
        for (int i = 1; i < N / 2; ++i) {
            if (!std::equal(input2[i].begin(), input2[i].end(), input2[i + N / 2].begin())) {
                ok = false;
                break;
            }
        }
        if (ok) {
            int diffpos1 = -1;
            int diffpos2 = -1;
            for (int i = 0; i < N / 2; ++i) {
                for (int j = 0; j < N; ++j) {
                    if (input2[i][j] != input2[i + 1][j]) {
                        if (diffpos1 < 0) {
                            diffpos1 = (j + N - i * 2) % N;
                        } else if (diffpos2 < 0) {
                            if (j == (diffpos1 + i * 2) % N) {

                            } else {
                                diffpos2 = (j + N - i * 2) % N;
                                if (diffpos1 < 2 && diffpos2 >= N - 2) {
                                    std::swap(diffpos1, diffpos2);
                                }
                                if ((diffpos1 + 2) % N != diffpos2) {
                                    ok = false;
                                    break;
                                }
                            }
                        } else {
                            if (j == (diffpos1 + i * 2) % N) {

                            } else if (j == (diffpos2 + i * 2) % N) {

                            } else {
                                ok = false;
                                break;
                            }
                        }
                    }
                }
            }
            
            if (ok) {
                std::cout << 2 * N << std::endl;   
                return 0;                     
            }
        }
    }
    for (int x = 0; x < N; ++x) {
        bool ok = [&] {
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < i; ++j) {
                    if (input[(i + x) % N][j] != input[(j + x) % N][i]) {
                        return false;
                    }
                }
            }
            return true;
        }();
        if (ok) {
            std::cout << N << std::endl;
            return;
        }
    }
    std::cout << 0 << std::endl;

    /*std::vector<std::vector<char>> input3(N, std::vector<char>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            input3[i][j] = input[(i + N - j) % N][j];
            std::cout << input2[i][j];
        }
        std::cout << std::endl;
    }*/
    
    return 0;
}

/*
xbcd
bxef
cexg
dfgx


fbcd
bcdf
cdfb
dfbc

dxbc
fbxe
gcex
xdfg

bcdf
cdfb
dfbc
fbcd




xbcd
bydf
cdxb
dfby

cdxb
efbx
xgce
gxdf

xbcd
fbyd
xbcd
fbyd


fbcd
bcdf
cdfb
dfbc

bcdx
xefb
exgc
fgxd


xbcd
xefb
xgce
xdfg




xbcdef
bxghij
cfxklm
dgixno
ehjkxp
aaaaax

xaaaaa
aayaaa
aaaaza
xaaaaa
aayaaa
aaaaza

xaaaaa
ayaaaa
aazaaa
aaaxaa
aaaaya
aaaaaa


hbcde
bcdeh
cdehb
dehbc
ehbcd

cdexb
fghbx
xijcf
ixkdg
jkxeh



*/

Compiler output:
a.cc: In function 'int main()':
a.cc:136:13: error: return-statement with no value, in function returning 'int' [-fpermissive]
  136 |             return;
      |             ^~~~~~