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)

//extern crate rand;
//
//fn test() {
//    use rand::{Rng, thread_rng};
//    let mut rng = rand::thread_rng();
//
//    for i in 1..100000 {
//        let h: usize = rng.gen_range(3, 5);
//        let w: usize = rng.gen_range(3, 5);
//        let mut s = vec![vec![' '; w]; h];
//        for i in 0..h {
//            for j in 0..w {
//                s[i][j] = (rng.gen_range(0, 26) + 'a' as u8) as char;
//                s[h - 1 - i][w - 1 - j] = s[i][j];
//            }
//        }
//
//        for _ in 0..15 {
//            let from = rng.gen_range(0, w);
//            let to = rng.gen_range(0, w);
//            let from_s: Vec<char> = (0..h).map(|i| s[i][from]).collect();
//            let to_s: Vec<char> = (0..h).map(|i| s[i][to]).collect();
//            for i in 0..h {
//                s[i][from] = to_s[i];
//                s[i][to] = from_s[i];
//            }
//        }
//
//        for _ in 0..15 {
//            let from = rng.gen_range(0, h);
//            let to = rng.gen_range(0, h);
//            let from_s: Vec<char> = s[from].clone();
//            let to_s: Vec<char> = s[to].clone();
//            for i in 0..w {
//                s[from][i] = to_s[i];
//                s[to][i] = from_s[i];
//            }
//        }
//        let mut rows = Vec::new();
//        let mut used = vec![false; h];
//        if !rec(&s, &mut used, &mut rows) {
//            for s in &s {
//                for &c in s {
//                    print!("{}", c);
//                }
//                println!();
//            }
//            panic!();
//        }
//
//        if i % 1000 == 0 {
//            println!("{}", i);
//        }
//    }
//}

fn main() {
//    test();
    let mut sc = Scanner::new();
    let h: usize = sc.read();
    let w: usize = sc.read();
    let s: Vec<Vec<char>> = (0..h).map(|_| sc.read::<String>().chars().collect()).collect();

    if h == 1 {
        let mut s = s[0].clone();
        s.sort();
        let mut count = vec![0; 26];
        for &c in &s {
            count[c as usize - 'a' as usize] += 1;
        }

        if w % 2 == 1 {
            let mut used = false;
            for &c in &count {
                if c % 2 == 1 {
                    if used {
                        println!("NO");
                        return;
                    } else {
                        used = true;
                    }
                }
            }
        } else {
            for &c in &count {
                if c % 2 == 1 {
                    println!("NO");
                    return;
                }
            }
        }

        println!("YES");
        return;
    }

    let mut rows = vec![0];
    let mut used = vec![false; h];
    used[0] = true;
    if rec(&s, &mut used, &mut rows) {
        println!("YES");
        return;
    }
    let mut rows = vec![1];
    let mut used = vec![false; h];
    used[1] = true;
    if rec(&s, &mut used, &mut rows) {
        println!("YES");
        return;
    }
    println!("NO");
}

fn rec(s: &Vec<Vec<char>>, used: &mut Vec<bool>, rows: &mut Vec<usize>) -> bool {
    if rows.len() == s.len() {
        let h = s.len();
        let w = s[0].len();
        let mut t = vec![vec![' '; w]; h];
        for i in 0..h {
            for j in 0..w {
                t[i][j] = s[rows[i]][j];
            }
        }
        let mut used = vec![false; w];
        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 t[k][i] != t[h - 1 - k][j] {
                        ok = false;
                        break;
                    }
                }

                if ok {
                    used[i] = true;
                    used[j] = true;
                }
            }
        }

        let mut count = 0;
        for &b in &used {
            if !b { count += 1; }
        }
        if w % 2 == 1 && count > 1 { return false; }
        if w % 2 == 0 && count > 0 { return false; }
        return true;
    }
    let h = s.len();
    for i in 0..h {
        if used[i] { continue; }
        rows.push(i);
        used[i] = true;

        let check = if rows.len() > (h + 1) / 2 {
            let w = s[0].len();
            let mut up = Vec::new();
            let mut down = Vec::new();
            for col in 0..w {
                let mut u = Vec::new();
                let mut d = Vec::new();
                if h % 2 == 0 {
                    let center = h / 2;
                    for i in 0..h {
                        if center + i >= rows.len() { break; }
                        u.push(s[rows[center - 1 - i]][col]);
                        d.push(s[rows[center + i]][col]);
                    }
                } else {
                    let center = h / 2;
                    for i in 1..h {
                        if center + i >= rows.len() { break; }
                        u.push(s[rows[center - i]][col]);
                        d.push(s[rows[center + i]][col]);
                    }
                }
                up.push(u);
                down.push(d);
            }
            up.sort();
            down.sort();

            let mut ok = true;
            for col in 0..w {
                if up[col] != down[col] {
                    ok = false;
                    break;
                }
            }

            ok
        } else { true };

        if check { if rec(s, used, rows) { return true; } }
        used[i] = false;
        rows.pop();
    }
    return false;
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner { ptr: 0, length: 0, buf: vec![0; 1024], small_cache: vec![0; 1024] }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool { b == b'\n' || b == b'\r' || b == b'\t' || b == b' ' }

    fn read<T>(&mut self) -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)]).parse().unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}