Language: C++

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する
int H, W;

void swapH(vector<vector<char> > &vv, int hx, int hy) {
    for (int i = 0; i < W; i++) {
        swap(vv[hx][i], vv[hy][i]);
    }
}

void swapW(vector<vector<char> > &vv, int wx, int wy) {
    for (int i = 0; i < H; i++) {
        swap(vv[i][wx], vv[i][wy]);
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);


    cin >> H >> W;
    vector<vector<char> > vv(H, vector<char>(W));
    rep(i, 0, H) {
        rep(j, 0, W) {
            cin >> vv[i][j];
        }
    }

    vector<vector<int> > Hhash(H, vector<int>(26, 0));
    rep(i, 0, H) {
        rep(j, 0, W) {
            Hhash[i][vv[i][j] - 'a']++;
        }
    }

    rep(i, 0, H) {
        rep(j, 0, H) {
            if (Hhash[i] > Hhash[j]) {
                swap(Hhash[i], Hhash[j]);
                swapH(vv, i, j);
            }
        }
    }

    vector<vector<int> > Whash(W, vector<int>(26, 0));
    rep(i, 0, W) {
        rep(j, 0, H) {
            Whash[i][vv[j][i] - 'a']++;
        }
    }
//    while (true) {
//        bool flag = [&] {
//            rep(i, 0, W) {
//                rep(j, i + 1, W) {
//                    if (Whash[i] == Whash[j] && j != W - i - 1) {
//                        swap(Whash[j], Whash[W - j - 1]);
//                        swapW(vv, i, W - i - 1);
//                        return true;
//                    }
//                }
//            }
//            return false;
//        }();
//
//        if (flag) continue;
//        else break;
//    }
    rep(i, 0, W) {
        rep(j, 0, W) {
            if (Whash[i] > Whash[j]) {
                swap(Whash[i], Whash[j]);
                swapW(vv, i, j);
            }
        }
    }
    bool dbcheck = false;
    for (int i = 0; i < H;) {
        if (i == H - 1) {
            if (dbcheck || H % 2 == 0) {
                cout << "NO" << endl;
                return 0;
            }
        }else{
            break;
        }
        if (Hhash[i] == Hhash[i + 1]) {
            i += 2;
        } else if (dbcheck || H % 2 == 0) {
            cout << "NO" << endl;
            return 0;
        } else {
            dbcheck = true;
            i++;
        }
    }
    dbcheck = false;
    for (int i = 0; i < W;) {
        if (i == W - 1) {
            if (dbcheck || W % 2 == 0) {
                cout << "NO" << endl;
                return 0;
            }else{
                break;
            }
        }
        if (Whash[i] == Whash[i + 1]) {
            i += 2;
        } else if (dbcheck || W % 2 == 0) {
            cout << "NO" << endl;
            return 0;
        } else {
            dbcheck = true;
            i++;
        }
    }
    assert(H == 0);


//    rep(i, 0, H) {
//        rep(j, 0, W) {
//            if (vv[i][j] != vv[H - i][W - j]) {
//                cout << "NO" << endl;
//                return 0;
//            }
//        }
//    }
    cout << "YES" << endl;



//    swapH(vv,1,0);
//    swapW(vv,1,2);

//    rep(i, 0, H) {
//        rep(j, 0, W) {
//            cout << vv[i][j];
//        }
//        cout << endl;
//    }

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:179:5: error: 'assert' was not declared in this scope
  179 |     assert(H == 0);
      |     ^~~~~~
a.cc:34:1: note: 'assert' is defined in header '<cassert>'; this is probably fixable by adding '#include <cassert>'
   33 | #include <climits>
  +++ |+#include <cassert>
   34 |