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: Accepted)

#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define mp make_pair
using namespace std;
using ll=long long;
using P=pair<int,int>;
const ll INF=1LL<<30;
const ll LINF=1LL<<60;
const double eps=1e-9;
const ll MOD=1000000007LL;
template<typename T>void chmin(T &a,T b){a=min(a,b);};
template<typename T>void chmax(T &a,T b){a=max(a,b);};
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V>typename enable_if<is_class<T>::value==0>::type fill_v(T& t,const V& v){t=v;}
template<typename T,typename V>typename enable_if<is_class<T>::value!=0>::type fill_v(T& t,const V& v){for(auto &e:t)fill_v(e,v);};
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
vector<string> s;
int h,w;
int dfs(int co,vector<int> v){
    if(co==h/2+h%2){
        vector<int> vis(w);
        bool ff=w%2;
        for(int i=0;i<w;i++){
            if(vis[i])continue;
            for(int j=i+1;j<w;j++){
                if(vis[j])continue;
                bool f=true;
                for(int k=0;k<h;k++){
                    if(s[v[k]][i]!=s[v[h-k-1]][j])f=false;
                }
                if(f){
                    vis[i]=true;
                    vis[j]=true;
                    break;
                }
            }
            if(vis[i])continue;
            bool fff=true;
            for(int k=0;k<h;k++){
                if(s[v[k]][i]!=s[v[h-k-1]][i])fff=false;
            }
            if(ff&&fff){
                ff=false;
                vis[i]=true;
            }else{
                return 0;
            }
        }
        return 1;
    }
    int res=0;
    if(h%2&&co==0){
        for(int i=0;i<h;i++){
            vector<int> nv=v;
            nv[h/2]=i;
            res|=dfs(co+1,nv);
        }
    }else{
        int vis=0;
        for(int i=0;i<h;i++){
            if(v[i]>=0)vis|=(1<<v[i]);
        }
        for(int i=0;i<h;i++){
            if(vis&(1<<i))continue;
            for(int j=i+1;j<h;j++){
                if(vis&(1<<j))continue;
                vector<int> nv=v;
                nv[co-h%2]=i,nv[h-co-1+h%2]=j;
                res|=dfs(co+1,nv);
            }
            break;
        }
    }
    return res;
}
int main(){
    cin>>h>>w;
    s.resize(h);
    for(int i=0;i<h;i++)cin>>s[i];
    if(dfs(0,vector<int>(h,-1))){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }
}