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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;
import java.util.HashMap;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.BitSet;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author anand.oza
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ESymmetricGrid solver = new ESymmetricGrid();
        solver.solve(1, in, out);
        out.close();
    }

    static class ESymmetricGrid {
        char[][] s;
        char[][] t;
        char[][] tR;
        int h;
        int w;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            h = in.nextInt();
            w = in.nextInt();

            s = new char[h][];
            for (int i = 0; i < h; i++) {
                s[i] = in.next().toCharArray();
            }
            t = new char[h][w];
            tR = new char[h][w];

            BitSet avail = new BitSet();
            for (int i = 0; i < w; i++) {
                avail.set(i);
            }

            int[] perm = new int[w];
            boolean answer = false;
            if (w % 2 == 1) {
                for (int i = 0; i < w; i++) {
                    perm[0] = i;
                    avail.clear(i);
                    answer |= go(perm, avail, 1);
                    avail.set(i);
                }
            } else {
                answer |= go(perm, avail, 0);
            }

            out.println(answer ? "YES" : "NO");
        }

        boolean go(int[] perm, BitSet avail, int index) {
            if (avail.cardinality() <= 1) {
                if (avail.cardinality() == 1) perm[index] = avail.nextSetBit(0);
                return eval(perm);
            } else {
                int first = avail.nextSetBit(0);
                avail.clear(first);
                for (int second = avail.nextSetBit(first + 1); second != -1; second = avail.nextSetBit(second + 1)) {
                    perm[index] = first;
                    perm[perm.length - 1 - index] = second;
                    avail.clear(second);
                    if (go(perm, avail, index + 1)) return true;
                    avail.set(second);
                }
                avail.set(first);
            }

            return false;
        }

        private boolean eval(int[] perm) {
            HashMap<String, Integer> columns = new HashMap<>();
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    t[i][j] = s[i][perm[j]];
                }
            }
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    tR[i][w - 1 - j] = s[i][perm[j]];
                }
            }

            for (int i = 0; i < h; i++) {
                String cur = String.valueOf(t[i]);
                String curReverse = String.valueOf(tR[i]);
                int count = columns.getOrDefault(curReverse, 0);
                if (count > 0) {
                    columns.put(curReverse, count - 1);
                    if (count == 1) {
                        columns.remove(curReverse);
                    }
                } else {
                    columns.put(cur, columns.getOrDefault(cur, 0) + 1);
                }
            }

            if (h % 2 == 0) {
                return columns.isEmpty();
            } else {
                if (columns.size() > 1) return false;
                String cur = columns.keySet().iterator().next();
                if (columns.get(cur) > 1) return false;
                return isPalindrome(cur);
            }
        }

        private static boolean isPalindrome(String cur) {
            for (int i = 0; i < cur.length(); i++) {
                if (cur.charAt(i) != cur.charAt(cur.length() - 1 - i)) return false;
            }
            return true;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}