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 centor = false;
    let mut i = 0;
    while i < w {
        if used[i] {
            i += 1;
            continue;
        }
        let mut j = i + 1;
        while j < w {
            if used[j] {
                j += 1;
                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;
            }
            j += 1;
        }
        if !used[i] {
            if centor {
                return false;
            }
            for k in 0..h {
                if s[k][i] != s[h - 1 - k][i] {
                    return false;
                }
            }
            centor = true;
            used[i] = true;
        }
    }
    used.iter().all(|p| *p)
}

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

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

fn main() {
    run();
}