Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <functional>
#include <array>

using namespace std;
using P = pair<int, int>;
char S[20][20];
int cnt[26] = {};

int main(){
    int H, W;
    cin >> H >> W;

    for(int i = 0; i < H; ++i) for(int j = 0; j < W; ++j){
        cin >> S[i][j];
        ++cnt[int(S[i][j] - 'a')];
    }
    
    bool ok = true;
    vector<vector<char>> ans(H, vector<char>(W, '.'));
    if(H % 2 == 1 && W % 2 == 1){
        int odd = -1;
        for(int i = 0; i < 26; ++i){
            if(cnt[i] % 2 == 1){
                if(odd != -1){
                    ok = false;
                    break;
                }
                odd = i;
            }
        }
        if(odd == -1) ok = false;
        bool oddok = false;
        vector<P> center;
        for(int i = 0; i < H; ++i){
            for(int j = 0; j < W; ++j){
                if(int(S[i][j] - 'a') == odd){
                    int tcnt[26] = {};
                    for(int k = 0; k < W; ++k){
                        if(k != i) ++tcnt[int(S[k][j] - 'a')];
                    }
                    bool tok = true;
                    for(int k = 0; k < 26; ++k){
                        if(tcnt[k] % 2 == 1) tok = false;
                    }
                    int tcnt2[26] = {};
                    for(int k = 0; k < H; ++k){
                        if(k != j) ++tcnt2[int(S[i][k] - 'a')];
                    }
                    bool tok2 = true;
                    for(int k = 0; k < 26; ++k){
                        if(tcnt2[k] % 2 == 1) tok2 = false;
                    }
                    if(tok && tok2){
                        oddok = true;
                        center.push_back(P(i, j));
                    }
                } 
            }
        }/*
        for(size_t k = 0; k < center.size(); ++k){
            cout << center[k].first << " " << center[k].second << endl;
        }
        cout << endl;*/
        if(!oddok) ok = false;
        for(int i = 0; i < H; ++i){
            for(int j = 0; j < W; ++j){
                if(int(S[i][j] - 'a') != odd){
                    int tcnt[26] = {};
                    for(int k = 0; k < H; ++k){
                        if(k != i) ++tcnt[int(S[k][j] - 'a')];
                    }
                    int tcnt2[26] = {};
                    for(int k = 0; k < W; ++k){
                        if(k != j) ++tcnt2[int(S[i][k] - 'a')];
                    }
                    bool tok = false;
                    for(int k = 0; k < 26; ++k){
                        if(tcnt[k] > 0 && tcnt2[k] > 0) tok = true;
                    }
                    if(!tok){
                        bool cok = false;
                        if(tcnt[int(S[i][j] - 'a')] > 0 || tcnt2[int(S[i][j] - 'a')] > 0){
                            vector<P> newcent;
                            for(size_t k = 0; k < center.size(); ++k){
                                if(i == center[k].first || j == center[k].second){
                                    cok = true;
                                    newcent.push_back(center[k]);
                                }
                            }
                            center = newcent;/*
                            for(size_t k = 0; k < center.size(); ++k){
                                cout << center[k].first << " " << center[k].second << endl;
                            }*/
                        }
                        if(!cok){
                            ok = false;
                           // cout << i << " " << j << endl;
                        }
                    }
                }
            }
        }
    }
    else {
        int center = -1;
        for(int i = 0; i < 26; ++i){
            if(cnt[i] % 2 == 1){
                ok = false;
            }
        }
        //cout << ok << endl;
        for(int i = 0; i < H; ++i){
            for(int j = 0; j < W; ++j){
                int tcnt[26] = {};
                for(int k = 0; k <H; ++k){
                    if(k != i) ++tcnt2[int(S[k][j] - 'a')];
                }
                int tcnt2[26] = {};
                for(int k = 0; k < W; ++k){
                    if(k != j) ++tcnt[int(S[i][k] - 'a')];
                }
                bool tok = false;
                for(int k = 0; k < 26; ++k){
                    if(tcnt[k] > 0 && tcnt2[k] > 0) tok = true;
                }
                if(!tok){
                    bool cok = false;
                    if(H % 2 == 1 && tcnt2[int(S[i][j] - 'a')] > 0){
                        if(center == -1 || center == i){
                            center = i;
                            cok = true;
                        }
                    }
                    if(W % 2 == 1 && tcnt[int(S[i][j] - 'a')] > 0){
                        if(center == -1 || center == j){
                            center = j;
                            cok = true;
                        }
                    }
                    if(!cok) ok = false;
                    //cout << i << " " << j << endl;
                }
            }
        }
    }
    cout << (ok ? "YES" : "NO") << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:122:34: error: 'tcnt2' was not declared in this scope; did you mean 'tcnt'?
  122 |                     if(k != i) ++tcnt2[int(S[k][j] - 'a')];
      |                                  ^~~~~
      |                                  tcnt