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<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

typedef vector<char> vc;

pair<vc, vc>  getCrossChar(const vector<vc>& s, int i, int j, bool f = 0){
	vc x, y;
	rep(k,s[i].size()){
		x.emplace_back(s[i][k]);
	}

	rep(k,s.size()){
		if(i == k) continue;
		y.emplace_back(s[k][j]);
	}
	sort(all(x));
	if(not f) sort(all(y));

	//cout << x << y << endl;
	return make_pair(x,y);
}

bool check(vector<pair<vc,int>>& c){
	vector<vc> f, s;
	rep(i,c.size() / 2){
		sort(all(c[i].first));
		f.emplace_back(c[i].first);
	}
	range(i,c.size() / 2, c.size()){
		sort(all(c[i].first));
		s.emplace_back(c[i].first);
	}
	sort(all(f));
	sort(all(s));

	rep(i,f.size()){
		rep(j,f[i].size()){
			if(f[i][j] != s[i][j]) return false;
		}
	}
	return true;
}

bool dfs(const vector<vc>& s){
	int h = s.size();
	int w = s[0].size();
	//cout << h << ' ' << w << endl; for(auto i : s){ for(auto j : i){ cout << j; } cout << endl; }

	map<vc, vector<pair<vc,int>>> m;
	rep(i,h){
		rep(j,w){
			pair<vc, vc> p = getCrossChar(s, i, j);
			m[p.first].emplace_back(p.second, i);
		}
	}

	//show(m.size())
	if((h + 1) / 2 != m.size()) {
		//cout << "kumiawase" << endl;
		return false;
	}

	if(h % 2 == 1){
		vector<vc> nxt;
		for(auto i : m){
			if(i.second.size() == w * 2){
				if(not check(i.second)) {
					//cout << "kisuu chekcfalse " << endl;
					return false;
				}
			}else{
				rep(j,w){
					nxt.emplace_back(getCrossChar(s, i.second[0].second, j, 1).second);
				}
			}
		}

		return dfs(nxt);
	}else{
		for(auto i : m){
			if(not check(i.second)) {
				//cout << "guusuu checkfalse" << endl;
				return false;
			}
		}
		return true;
	}
}

int main(){
	int h, w;
	cin >> h >> w;

	vector<vc> s(h, vc(w));
	rep(i,h){
		rep(j,w){
			cin >> s[i][j];
		}
	}

	cout << (dfs(s) ? "YES" : "NO") << endl;
}