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 <fstream>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <cmath>
#include <algorithm>
#include <assert.h>
#include <vector>
#include <cstring>
#include <random>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;

bool bit(int mask, int pos) {
    return (mask >> pos) & 1;
}

const int nmax = 16;

int curr[nmax];
int mt[nmax];
bool used[nmax];

bool dfs(int v) {
    if (used[v]) {
        return false;
    }
    used[v] = true;
    int val = curr[v];
    for (int i = 0; val; ++i, val >>= 1) {
        if (val & 1) {
            if (mt[i] == -1 || dfs(mt[i])) {
                mt[i] = v;
                return true;
            }
        }
    }
    return false;
}

string s[nmax];
int n, m;
bool ans = false;

void kuhn() {
    for (int i = 0; i < m; ++i) {
        used[i] = false;
    }
    memset(mt, -1, sizeof(mt));
    for (int i = 0; i < m; ++i) {
        if (used[i]) {
            continue;
        }
        if (dfs(i)) {
            for (int j = 0; j < m; ++j) {
                used[j] = false;
            }
        } else {
            return;
        }
    }
    ans = true;
}

bool occ[nmax];

const int alpha = 26;
int pos[alpha];

void build(int u, int v) {
    memset(pos, 0, sizeof(pos));
    for (int i = 0; i < m; ++i) {
        pos[s[v][i] - 'a'] |= (1 << i);
    }
    for (int i = 0; i < m; ++i) {
        curr[i] &= pos[s[u][i] - 'a'];
        //cout << curr[i] << "\n";
    }
}

void f(int lib) {
    if (lib == 0) {
        kuhn();
        return;
    }
    int tmp[nmax];
    memcpy(tmp, curr, sizeof(tmp));
    if (lib == 1) {
        for (int i = 0; i < m; ++i) {
            if (!occ[i]) {
                build(i, i);
            }
        }
        kuhn();
        memcpy(curr, tmp, sizeof(tmp));
        return;
    }
    for (int i = 0; i < n; ++i) {
        if (occ[i]) {
            continue;
        }
        for (int j = i + 1; j < n; ++j) {
            if (occ[j]) {
                continue;
            }
            build(i, j);
            occ[i] = occ[j] = true;
            f(lib - 2);
            if (ans) {
                return;
            }
            occ[i] = occ[j] = false;
            memcpy(curr, tmp, sizeof(tmp));
        }
    }
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        cin >> s[i];
    }

    for (int i = 0; i < m; ++i) {
        curr[i] = (1 << m) - 1;
    }

    f(n);

    cout << (ans ? "YES\n" : "NO\n");

}