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: Time Limit Exceeded)

fn main() {
    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();

    let mut rows = vec![0; h];
    let mut used = vec![false; h];

    if solve(&s, &mut rows, &mut used, 0) {
        println!("YES");
    } else {
        println!("NO");
    }
}

fn solve(s: &Vec<Vec<char>>, rows: &mut Vec<usize>, used: &mut Vec<bool>, pos: usize) -> bool {
    let h = rows.len();
    if pos == h / 2 {
        if h % 2 == 0 {
            return check(s, rows);
        } else {
            for i in 0..h {
                if !used[i] {
                    rows[pos] = i;
                    break;
                }
            }
            return check(s, rows);
        }
    }

    for i in 0..h {
        if used[i] {
            continue;
        }
        used[i] = true;
        for j in (i + 1)..h {
            if used[j] {
                continue;
            }
            used[j] = true;

            rows[pos] = i;
            rows[h - 1 - pos] = j;
            if solve(s, rows, used, pos + 1) {
                return true;
            }

            used[j] = false;
        }
        used[i] = false;
    }

    return false;
}

fn check(s: &Vec<Vec<char>>, rows: &Vec<usize>) -> bool {
    let h = rows.len();
    let w = s[0].len();
    let mut used = vec![false; w];
    for c1 in 0..w {
        if used[c1] {
            continue;
        }
        for c2 in (c1 + 1)..w {
            if used[c2] {
                continue;
            }
            let mut ok = true;
            for i in 0..h {
                let cl = s[rows[i]][c1];
                let cr = s[rows[h - 1 - i]][c2];
                if cl != cr {
                    ok = false;
                    break;
                }
            }
            if ok {
                used[c1] = true;
                used[c2] = true;
                break;
            }
        }
    }

    let mut already_checked = false;
    for c in 0..w {
        if used[c] {
            continue;
        }
        if already_checked {
            return false;
        }
        already_checked = true;
        for i in 0..h {
            if s[rows[i]][c] != s[rows[h - 1 - i]][c] {
                return false;
            }
        }
    }

    return true;
}

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();
    }
}