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)

//#define debug

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define SZ(x) ((int)x.size())

int n, m, np;
char a[13][13];
bool used[13], mark[13];
ll pw[13];
ii all_pairs[13];

struct data {
	ll hs; int id;
	data(ll _hs, int _id) {
		hs = _hs;
		id = _id;
	}
	bool operator<(const data &other) const {
		return hs < other.hs;
	}
};

set<data> d;

void check() {
	d.clear();
	memset(mark, false, sizeof(mark));
	int residue = 0;
	for (int i = 1; i <= n; ++i) {
		if (!used[i]) {
			residue = i;
			break;
		}
	}
	for (int i = 1; i <= m; ++i) {
		ll htmp = 0;
		for (int j = 1; j <= np; ++j) {
			htmp = htmp * 26ll + (ll)(a[all_pairs[j].second][i] - 'a');
		}
		if (residue) {
			htmp = htmp * 26ll + (ll)(a[residue][i] - 'a');
		}
		d.insert(data(htmp, i));
	}
	for (int i = 1; i <= m; ++i) {
		if (mark[i]) continue;
		ll htmp = 0, htmp2 = 0;
		for (int j = 1; j <= np; ++j) {
			htmp = htmp * 26ll + (ll)(a[all_pairs[j].first][i] - 'a');
			htmp2 = htmp2 * 26ll + (ll)(a[all_pairs[j].second][i] - 'a');
		}
		if (residue) {
			htmp = htmp * 26ll + (ll)(a[residue][i] - 'a');
			htmp2 = htmp2 * 26ll + (ll)(a[residue][i] - 'a');
		}
		auto it = d.lower_bound(data(htmp, 0));
		if (it == d.end() || it->hs != htmp) continue;
		mark[i] = mark[it->id] = true;
		d.erase(it);
		d.erase(data(htmp2, i));
	}
	int cntm = 0;
	for (int i = 1; i <= m; ++i) cntm += mark[i];
	if (cntm == m) {
		puts("YES");
		exit(0);
	}
	if (cntm < m - 1) return;
	for (int i = 1; i <= m; ++i) {
		if (mark[i]) continue;
		ll htmp = 0, htmp2 = 0;
		for (int j = 1; j <= np; ++j) {
			htmp = htmp * 26ll + (ll)(a[all_pairs[j].first][i] - 'a');
			htmp2 = htmp2 * 26ll + (ll)(a[all_pairs[j].second][i] - 'a');
		}
		if (residue) {
			htmp = htmp * 26ll + (ll)(a[residue][i] - 'a');
			htmp2 = htmp2 * 26ll + (ll)(a[residue][i] - 'a');
		}
		if (htmp == htmp2) {
			puts("YES");
			exit(0);
		}
	}
}

void brute(int cnt) {
	if (cnt > np) {
		check();
		return;
	}
	int p1 = -1;
	for (int i = all_pairs[cnt - 1].first + 1; i <= n; ++i) {
		if (!used[i]) {
			used[(p1 = i)] = true;
			break;
		}
	}
	all_pairs[cnt].first = p1;
	for (int i = all_pairs[cnt - 1].first + 2; i <= n; ++i) {
		if (used[i]) continue;
		all_pairs[cnt].second = i;
		used[i] = true;
		brute(cnt + 1);
		used[i] = false;
	}
	used[p1] = false;
}

int main() {
#ifdef debug
	clock_t tStart = clock();
#endif
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		scanf("%s", a[i] + 1);
	}
	np = n / 2, pw[0] = 1;
	for (int i = 1; i <= n; ++i) {
		pw[i] = pw[i - 1] * 26ll;
	}
	brute(1);
	puts("NO");
#ifdef debug
	printf("Total time: %d ms\n", (int)(1000.0 * (double)(clock() - tStart) / CLOCKS_PER_SEC));
#endif
	return 0;
}