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 _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>

//LLONG_MAX
#define int long long
#define CONTAINS(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v,a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v,a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;

int H, W;
char S[15][15];
int half;
int ary[12];
bool ary_flg[12];

bool check()
{
	bool flg[12] = {};
	if (W % 2 == 1)
	{
		int test[26] = {};
		for (int y = 0; y < H; y++)
		{
			test[S[y][ary[W / 2]] - 'a']++;
		}
		int test_cnt = 0;
		for (int i = 0; i < 26; i++)
		{
			if (test[i] % 2 == 1)
			{
				test_cnt++;
			}
		}
		if (H % 2 == 1)
		{
			if (test_cnt != 1) return false;
		}
		else
		{
			if (test_cnt != 0) return false;
		}
	}
	int cnt = 0;
	for (int y1 = 0; y1 < H; y1++)
	{
		if (flg[y1]) continue;
		for (int y2 = y1 + 1; y2 < H; y2++)
		{
			if (flg[y2]) continue;
			bool is_valid = true;
			for (int x = 0; x < W / 2; x++)
			{
				if (S[y1][ary[x]] != S[y2][ary[W - x - 1]])
				{
					is_valid = false;
					break;
				}
			}
			if (is_valid)
			{
				flg[y1] = true;
				flg[y2] = true;
				cnt += 2;
				break;
			}
		}
	}

	if (H % 2 == 1)
	{
		if (H - 1 != cnt)
		{
			return false;
		}

		int c;
		REP(y, H)
		{
			if (!flg[y])
			{
				c = y;
				break;
			}
		}
		for (int x = 0; x < W / 2; x++)
		{
			if (S[c][ary[x]] != S[c][ary[W - x - 1]])
			{
				return false;
			}
		}

		return true;
	}
	else
	{
		return (H == cnt);
	}
}

void comb2(int n)
{
	if (n >= W)
	{
		if (check())
		{
			cout << "YES" << endl;
			exit(0);
		}
		return;
	}

	int index = W - (n - half) - 1;
	int init = ary[n - half];
	for (int i = init; i < W; i++)
	{
		if (ary_flg[i]) continue;
		ary[index] = i;
		ary_flg[i] = true;
		comb2(n + 1);
		ary_flg[i] = false;
	}

}

void comb(int n)
{
	if (n >= half)
	{
		comb2(n);
		return;
	}

	int init = n == 0 ? 0 : (ary[n - 1] + 1);
	for (int i = init; i < W; i++)
	{
		ary[n] = i;
		ary_flg[i] = true;
		comb(n + 1);
		ary_flg[i] = false;
	}
}

signed main()
{
	cin >> H >> W;
	REP(y, H)
	{
		cin >> S[y];
	}
	half = W / 2;
	if (W % 2 == 1) half++;
	comb(0);
	cout << "NO" << endl;
}