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 Rust (1.15.1) (judge verdict: Wrong Answer)

//https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

//

fn detect(s: &Vec<Vec<char>>) -> bool {
    let h = s.len();
    let w = s[0].len();
    let mut used = vec![false; w];
    let mut center = w % 2 == 0;
    for i in 0..w {
        if used[i] {
            continue;
        }
        for j in (i + 1)..w {
            if used[j] {
                continue;
            }
            let mut ok = true;
            for k in 0..h {
                if s[k][i] != s[h - 1 - k][j] {
                    ok = false;
                }
            }
            if ok {
                used[i] = true;
                used[j] = true;
                break;
            }
        }
        if !used[i] {
            if center {
                return false;
            }
            for k in 0..h {
                if s[k][i] != s[h - 1 - k][i] {
                    return false;
                }
            }
            center = true;
            used[i] = true;
        }
        if !used[i] {
            return false;
        }
    }
    true
}

fn can(s: &mut Vec<Vec<char>>, k: usize, used: bool) -> bool {
    let h = s.len();
    if 2 * k + 2 >= h {
        return detect(s);
    }
    for i in (k + 1)..(h - k) {
        if used && 2 * i + 1 == h {
            continue;
        }
        s.swap(i, h - 1 - k);
        if can(s, k + 1, used) {
            return true;
        }
        s.swap(i, h - 1 - k);
    }
    if !used {
        let mut ok = true;
        let w = s[0].len();
        for i in 0..w {
            if s[k][i] != s[k][w - 1 - i] {
                ok = false;
            }
        }
        if ok {
            s.swap(k, h / 2);
            if can(s, k, true) {
                return true;
            }
            s.swap(k, h / 2);
        }
    }
    false
}

fn run() {
    input! {
        h: usize,
        _w: usize,
        s: [chars; h],
    }
    let mut s = s;
    if can(&mut s, 0, h % 2 == 0) {
        println!("YES");
    } else {
        println!("NO");
    }
}

fn main() {
    run();
}