Problem:
Score : 700 points

Problem Statement

There is an H \times W grid (H vertical, W horizontal), where each square contains a lowercase English letter.
Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string S_i.

Snuke can apply the following operation to this grid any number of times:

Choose two different rows and swap them. Or, choose two different columns and swap them.

Snuke wants this grid to be symmetric.
That is, for any 1 \leq i \leq H and 1 \leq j \leq W, the letter in the square at the i-th row and j-th column and the letter in the square at the (H + 1 - i)-th row and (W + 1 - j)-th column should be equal.

Determine if Snuke can achieve this objective.

Constraints

1 \leq H \leq 12

1 \leq W \leq 12

|S_i| = W

S_i consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

If Snuke can make the grid symmetric, print YES; if he cannot, print NO.

Sample Input 1

2 3
arc
rac

Sample Output 1

YES

If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:

Sample Input 2

3 7
atcoder
regular
contest

Sample Output 2

NO

Sample Input 3

12 12
bimonigaloaf
faurwlkbleht
dexwimqxzxbb
lxdgyoifcxid
ydxiliocfdgx
nfoabgilamoi
ibxbdqmzxxwe
pqirylfrcrnf
wtehfkllbura
yfrnpflcrirq
wvcclwgiubrk
lkbrwgwuiccv

Sample Output 3

YES

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#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) ++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(H % 2 == 1 && tcnt[int(S[i][j] - 'a')] > 0){
                        if(center == -1 || center == i){
                            center = i;
                            cok = true;
                        }
                    }
                    if(W % 2 == 1 && tcnt2[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;
}