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: Runtime Error)

#include"bits/stdc++.h"
using namespace std;

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    for (int i = 0; i < H; i++) {
        cin >> S[i];
    }

    map<map<char, int>, int> row_mp;
    for (int i = 0; i < H; i++) {
        map<char, int> mp;
        for (char c : S[i]) {
            mp[c]++;
        }
        row_mp[mp]++;
    }

    map<map<char, int>, int> col_mp;
    for (int i = 0; i < W; i++) {
        map<char, int> mp;
        for (int j = 0; j < H; j++) {
            mp[S[j][i]]++;
        }
        col_mp[mp]++;
    }


    if (H % 2 == 0 && W % 2 == 0) {
        for (auto itr = row_mp.begin(); itr != row_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                cout << "NO" << endl;
                return 0;
            }
        }

        for (auto itr = col_mp.begin(); itr != col_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                cout << "NO" << endl;
                return 0;
            }
        }

        assert(W >= 6);

    } else if (H % 2 == 1 && W % 2 == 0) {
        bool odd_appear = false;
        for (auto itr = row_mp.begin(); itr != row_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                if (odd_appear) {
                    cout << "NO" << endl;
                    return 0;
                } else {
                    //この行が回文となるか確認
                    bool odd = false;
                    for (auto p : itr->first) {
                        if (p.second % 2 == 1) {
                            if (odd) {
                                cout << "NO" << endl;
                                return 0;
                            } else {
                                odd = true;
                            }
                        }
                    }
                    odd_appear = true;
                }
            }
        }

        for (auto itr = col_mp.begin(); itr != col_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                cout << "NO" << endl;
                return 0;
            }
        }
    } else if (H % 2 == 0 && W % 2 == 1) {
        for (auto itr = row_mp.begin(); itr != row_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                cout << "NO" << endl;
                return 0;
            }
        }

        bool odd_appear = false;
        for (auto itr = col_mp.begin(); itr != col_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                if (odd_appear) {
                    cout << "NO" << endl;
                    return 0;
                } else {
                    //この列が回文となるか確認
                    bool odd = false;
                    for (auto p : itr->first) {
                        if (p.second % 2 == 1) {
                            if (odd) {
                                cout << "NO" << endl;
                                return 0;
                            } else {
                                odd = true;
                            }
                        }
                    }
                    odd_appear = true;
                }
            }
        }
    } else if (H % 2 == 1 && W % 2 == 1) {
        bool odd_appear = false;
        char center;
        for (auto itr = row_mp.begin(); itr != row_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                if (odd_appear) {
                    cout << "NO" << endl;
                    return 0;
                } else {
                    //この行が回文となるか確認
                    bool odd = false;
                    for (auto p : itr->first) {
                        if (p.second % 2 == 1) {
                            if (odd) {
                                cout << "NO" << endl;
                                return 0;
                            } else {
                                odd = true;
                                center = p.first;
                            }
                        }
                    }
                    odd_appear = true;
                }
            }
        }

        odd_appear = false;
        for (auto itr = col_mp.begin(); itr != col_mp.end(); itr++) {
            if (itr->second % 2 != 0) {
                if (odd_appear) {
                    cout << "NO" << endl;
                    return 0;
                } else {
                    //この列が回文となるか確認
                    bool odd = false;
                    for (auto p : itr->first) {
                        if (p.second % 2 == 1) {
                            if (odd) {
                                cout << "NO" << endl;
                                return 0;
                            } else {
                                if (p.first != center) {
                                    cout << "NO" << endl;
                                    return 0;
                                }
                                odd = true;
                            }
                        }
                    }
                    odd_appear = true;
                }
            }
        }

    } else {
        assert(false);
    }

    cout << "YES" << endl;
}