Language: C++

#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <iomanip>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#define INF 10000000000000000
#define MOD 1000000007
using namespace std;


int main(){
    int H,W;
    cin >> H >> W;
    
    string S[H];
    for(int i=0; i<H; i++){
        cin >> S[i];
    }
    
    char gyo[H*W];
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            gyo[i*W + j]=S[i][j];
        }
    }
    
    char retsu[W*H];
    for(int j=0; j<W; j++){
        for(int i=0; i<H; i++){
            retsu[j*H + i]=S[i][j];
        }
    }
    
    
    for(int i=0; i<H; i++){
        sort(gyo+i*W, gyo+i*W + W);
    }
    
    for(int j=0; j<W; j++){
        sort(retsu+j*H, retsu+j*H+H);
    }
    
    
    
    int same_gyo=0;
    int same_retsu=0;
    int checked_gyo[H]={};
    int checked_retsu[W]={};
    int kaibun_gyo=0;
    int kaibun_retsu=0;
    
    for(int i=0; i<H; i++){
        if(checked_gyo[i]==1){continue;}
        checked_gyo[i]=1;
        
        
        for(int i2=0; i2<H; i2++){
            if(checked_gyo[i2]==1){continue;}
            
            int same=1;
            for(int num=0; num<W; num++){
                if(gyo[i*W+num]!=gyo[i2*W+num]){
                    same=0;
                    break;
                }
            }
            if(same==1){
                checked_gyo[i2]=1;
                same_gyo++;
                break;
            }
        }
        
        
        int atoz[26]={};
        int kaibun=1;
        if(W%2==1){
            kaibun++;
        }
        for(int i=0; i<H; i++){
            atoz[gyo[i*W+j]-'a']++;
        }
        for(int u=0; u<26; u++){
            if(atoz[u]%2==1){
                kaibun--;
            }
        }
        if(kaibun>0){
            kaibun_gyo++;
        }
    }
    
    
    
    for(int j=0; j<W; j++){
        if(checked_retsu[j]==1){continue;}
        checked_retsu[j]=1;
        
        
        for(int j2=0; j2<W; j2++){
            if(checked_retsu[j2]==1){continue;}
            
            int same=1;
            for(int num=0; num<H; num++){
                if(retsu[j*H+num]!=retsu[j2*H+num]){
                    same=0;
                    break;
                }
            }
            if(same==1){
                checked_retsu[j2]=1;
                same_retsu++;
                break;
            }
        }
        
        
        int atoz[26]={};
        int kaibun=1;
        if(H%2==1){
            kaibun++;
        }
        for(int i=0; i<H; i++){
            atoz[retsu[i+j*H]-'a']++;
        }
        for(int u=0; u<26; u++){
            if(atoz[u]%2==1){
                kaibun--;
            }
        }
        if(kaibun>0){
            kaibun_retsu++;
        }
    }
    
    if(same_gyo==H/2 && same_retsu==W/2){
        if(H%2==1 && kaibun_gyo==0){
            cout<<"NO"<<endl;
        }else if(W%2==1 && kaibun_retsu==0){
            cout<<"NO"<<endl;
        }else{
            cout<<"YES"<<endl;
        }
        
    }else{
        cout<<"NO"<<endl;
    }
    
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:85:26: error: 'j' was not declared in this scope
   85 |             atoz[gyo[i*W+j]-'a']++;
      |                          ^