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 <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;
        
        int same=0;
        for(int i2=0; i2<H; i2++){
            if(checked_gyo[i2]==1){continue;}
            
            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;
            }
        }
        
        if(same==1){continue;}
        int atoz[26]={};
        int kaibun=1;
        if(W%2==1){
            kaibun++;
        }
        for(int j=0; j<W; j++){
            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;
        
        int same=0;
        for(int j2=0; j2<W; j2++){
            if(checked_retsu[j2]==1){continue;}
            
            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;
            }
        }
        
        if(same==1){
            continue;
        }
        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;
}