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

use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

struct Solver {
    h: usize,
    w: usize,
    field: Vec<Vec<char>>,
}

impl Solver {
    fn new() -> Solver {
        let h: usize = read();
        let w: usize = read();
        let mut field = vec![vec![]; h];
        for y in 0..h {
            field[y] = read::<String>().chars().collect::<Vec<_>>();
        }
        Solver {
            h: h,
            w: w,
            field: field,
        }
    }
    fn is_valid(&self, ypairs: &Vec<(usize, usize)>, xp: &(usize, usize)) -> bool {
        // println!("{:?} {:?}", ypairs, xpairs);
        for yp in ypairs.iter() {
            if self.field[yp.0][xp.0] != self.field[yp.1][xp.1]
                || self.field[yp.0][xp.1] != self.field[yp.1][xp.0]
            {
                return false;
            }
        }
        true
    }
    fn xdfs(&self, rest: usize, ypairs: &Vec<(usize, usize)>) -> bool {
        if rest == 0 {
            return true;
        }
        if rest == (1 << self.w) - 1 && self.w % 2 == 1 {
            for x in 0..self.w {
                if !self.is_valid(ypairs, &(x, x)) {
                    continue;
                }
                let nrest = rest ^ (1 << x);
                if self.xdfs(nrest, ypairs) {
                    return true;
                }
            }
        } else {
            let x1 = rest.trailing_zeros() as usize;
            for x2 in x1 + 1..self.w {
                if (rest & (1 << x2)) == 0 || !self.is_valid(ypairs, &(x1, x2)) {
                    continue;
                }
                // println!("{} {} {}", rest, x1, x2);
                let nrest = rest ^ (1 << x1) ^ (1 << x2);
                assert!(nrest < rest);
                if self.xdfs(nrest, ypairs) {
                    return true;
                }
            }
        }
        false
    }
    fn ydfs(&self, rest: usize, pairs: &mut Vec<(usize, usize)>) -> bool {
        if rest == 0 {
            return self.xdfs((1 << self.w) - 1, pairs);
        }
        if pairs.len() == 0 && self.h % 2 == 1 {
            for y in 0..self.h {
                let nrest = rest ^ (1 << y);
                pairs.push((y, y));
                if self.ydfs(nrest, pairs) {
                    return true;
                }
                pairs.pop();
            }
        } else {
            let y1 = rest.trailing_zeros() as usize;
            for y2 in y1 + 1..self.h {
                if (rest & (1 << y2)) == 0 {
                    continue;
                }
                // println!("{} {} {}", rest, y1, y2);
                let nrest = rest ^ (1 << y1) ^ (1 << y2);
                assert!(nrest < rest);
                pairs.push((y1, y2));
                if self.ydfs(nrest, pairs) {
                    return true;
                }
                pairs.pop();
            }
        }
        false
    }
    fn solve(&mut self) {
        if self.ydfs((1 << self.h) - 1, &mut vec![]) {
            println!("YES");
        } else {
            println!("NO");
        }
    }
}

fn main() {
    let mut solver = Solver::new();
    solver.solve();
}