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: Accepted)

// package arc.arc095;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int h = in.nextInt();
        int w = in.nextInt();
        S = new char[h][];
        S2 = new char[w][h];
        for (int i = 0; i < h ; i++) {
            S[i] = in.nextToken().toCharArray();
        }

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

    static char[][] S;

    private static boolean solve() {
        if (S.length % 2 == 0) {
            if (dfs(0, new int[S.length / 2][2], 0, -1)) {
                return true;
            }
        } else {
            for (int i = 0; i < S.length; i++) {
                if (dfs(1<<i, new int[S.length/2][2], 0, i)) {
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean dfs(int flag, int[][] rowPairs, int idx, int center) {
        if (Integer.bitCount(flag) == S.length) {
            return solveRowColumn(rowPairs, center);
        }

        for (int i = 0; i < S.length ; i++) {
            if ((flag & (1<<i)) == 0) {
                for (int j = i+1 ; j < S.length; j++) {
                    if ((flag & (1<<j)) == 0) {
                        rowPairs[idx][0] = i;
                        rowPairs[idx][1] = j;
                        if (dfs(flag | (1<<i) | (1<<j), rowPairs, idx+1, center)) {
                            return true;
                        }
                    }
                }
                break;
            }
        }
        return false;
    }

    static char[][] S2;

    private static boolean solveRowColumn(int[][] rowPairs, int center) {
        for (int i = 0; i < rowPairs.length ; i++) {
            int u = rowPairs[i][0];
            int v = rowPairs[i][1];
            for (int j = 0; j < S[0].length ; j++) {
                S2[j][i] = S[u][j];
                S2[j][S.length-1-i] = S[v][j];
            }
        }
        if (center != -1) {
            for (int j = 0; j < S[0].length ; j++) {
                S2[j][S.length/2] = S[center][j];
            }
        }

        boolean[][] match = new boolean[S2.length][S2.length];
        for (int i = 0; i < S2.length ; i++) {
            for (int j = i ; j < S2.length; j++) {
                match[i][j] = match[j][i] = String.valueOf(S2[i]).equals(new StringBuilder().append(S2[j]).reverse().toString());
            }
        }

        boolean[] used = new boolean[S2.length];
        int oc = 0;
        for (int i = 0; i < used.length ; i++) {
            if (used[i]) {
                continue;
            }
            for (int j = i+1 ; j < used.length; j++) {
                if (!used[j]) {
                    if (match[i][j]) {
                        used[i] = used[j] = true;
                        break;
                    }
                }
            }
            if (!used[i]) {
                if (S2.length % 2 == 0 || !match[i][i]) {
                    return false;
                }
                oc++;
                if (oc >= 2) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }

    public static class InputReader {
        private static final int BUFFER_LENGTH = 1 << 12;
        private InputStream stream;
        private byte[] buf = new byte[BUFFER_LENGTH];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int next() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public char nextChar() {
            return (char) skipWhileSpace();
        }

        public String nextToken() {
            int c = skipWhileSpace();
            StringBuilder res = new StringBuilder();
            do {
                res.append((char) c);
                c = next();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public int nextInt() {
            return (int) nextLong();
        }

        public long nextLong() {
            int c = skipWhileSpace();
            long sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble() {
            return Double.valueOf(nextToken());
        }

        int skipWhileSpace() {
            int c = next();
            while (isSpaceChar(c)) {
                c = next();
            }
            return c;
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }
}