Language: C++

#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define int long long
using namespace std;

typedef pair<int, int> Pii;

template<typename T>
void readvec(vector<T> &a);
void readindex(vector<int> &a);

string ans = "NO";
int H, W;
string S[12];
int center;

vector<int> makepair(vector<int> &P);
void check(vector<int> pair);

void fors(vector<int> &P, int i, int N){
    if(N <= 0){
        vector<int> pair = makepair(P);
        check(pair);
    }else{
        for(P[i] = 0; P[i] < N; P[i]++){
            fors(P, i + 1, N - 2);
        }
    }
}

vector<int> makepair(vector<int> &P){
    vector<int> index(W), pair(W);
    REP(i, W){
        index[i] = i;
    }
    if(W % 2 == 1) index.erase(index.begin() + center);

    REP(i, W / 2){
        pair[2 * i] = index[0];
        index.erase(index.begin() + 0);
        pair[2 * i + 1] = index[P[i]];
        index.erase(index.begin() + P[i]);
    }
    if(W % 2 == 1) pair[W - 1] = center;
    return pair;
}

void check(vector<int> pair){
    string S2[12];
    REP(i, H){
        S2[i] = S[i];
    }
    REP(i, H){
        REP(j, W / 2){
            S2[i][j] = S[i][pair[2 * j]];
            S2[i][W - j - 1] = S[i][pair[2 * j + 1]];
        }
        if(W % 2 == 1) S2[i][(W - 1) / 2] = S[i][pair[W - 1]];
    }
    vector<int> index(H);
    REP(i, H){
        index[i] = i;
    }
    while(index.size() >= 2){
        int i1 = index[0];
        index.erase(index.begin() + 0);
        found = false;
        reverse(S2[i1].begin(), S2[i1].end());
        REP(i, index.size()){
            if(S2[i1] == S2[index[i]]){
                found = true;
                index.erase(index.begin() + i);
                break;
            }
        }
        if(!found) break;
    }
    if(index.size() > 1 && H % 2 == 1){
        swap(index[0], index[1]);
        while(index.size() >= 2){
            int i1 = index[0];
            index.erase(index.begin() + 0);
            found = false;
            reverse(S2[i1].begin(), S2[i1].end());
            REP(i, index.size()){
                if(S2[i1] == S2[index[i]]){
                    found = true;
                    index.erase(index.begin() + i);
                    break;
                }
            }
            if(!found) break;
        }
    }
    if(index.size() == 0) ans = "YES";
    if(index.size() == 1){
        string S3 = S2[index[0]];
        reverse(S3.begin(), S3.end());
        if(S3 == S2[index[0]]) ans = "YES";
    }
}

signed main(){

    cin >> H >> W;
    REP(i, H){
        cin >> S[i];
    }

    vector<int> P(W / 2);
    for(center = 0; center < W; center++){
        fors(P, 0, W - (W % 2) - 1);
    }
    cout << ans;
    
    return 0;
}


template<typename T>
void readvec(vector<T> &a){
    REP(i, a.size()){
        cin >> a[i];
    }
}
void readindex(vector<int> &a){
    REP(i, a.size()){
        cin >> a[i];
        a[i]--;
    }
}

Compiler output:
a.cc: In function 'void check(std::vector<long long int>)':
a.cc:69:9: error: 'found' was not declared in this scope; did you mean 'round'?
   69 |         found = false;
      |         ^~~~~
      |         round
a.cc:85:13: error: 'found' was not declared in this scope; did you mean 'round'?
   85 |             found = false;
      |             ^~~~~
      |             round