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)

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

#[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>>,
    // ycnts: Vec<HashMap<char, usize>>,
    // xcnts: Vec<HashMap<char, usize>>,
    // ycenter: Vec<bool>,
    // xcenter: Vec<bool>,
}

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<_>>();
        }
        // let mut ycnts = vec![HashMap::<char, usize>::new(); h];
        // let mut xcnts = vec![HashMap::<char, usize>::new(); w];
        // let mut ycenter = vec![false; h];
        // let mut xcenter = vec![false; w];
        // for y in 0..h {
        //     for x in 0..w {
        //         let c = field[y][x];
        //         *ycnts[y].entry(c).or_insert(0) += 1;
        //     }
        //     if ycnts[y].values().filter(|v| *v % 2 == 1).count() == w % 2 {
        //         ycenter[y] = true;
        //     }
        // }
        // for x in 0..w {
        //     for y in 0..h {
        //         let c = field[y][x];
        //         *xcnts[x].entry(c).or_insert(0) += 1;
        //     }
        //     if xcnts[x].values().filter(|v| *v % 2 == 1).count() == h % 2 {
        //         xcenter[x] = true;
        //     }
        // }
        Solver {
            h: h,
            w: w,
            field: field,
            // ycnts: ycnts,
            // xcnts: xcnts,
            // ycenter: ycenter,
            // xcenter: xcenter,
        }
    }
    fn is_valid(&self, ypairs: &Vec<(usize, usize)>, xpairs: &Vec<(usize, usize)>) -> bool {
        // println!("{:?} {:?}", ypairs, xpairs);
        for yp in ypairs.iter() {
            for xp in xpairs.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)>,
        xpairs: &mut Vec<(usize, usize)>,
    ) -> bool {
        if rest == 0 {
            return self.is_valid(ypairs, xpairs);
        }
        if xpairs.len() == 0 && self.w % 2 == 1 {
            for x in 0..self.w {
                // if !self.xcenter[x] {
                //     continue;
                // }
                let nrest = rest ^ (1 << x);
                xpairs.push((x, x));
                if self.xdfs(nrest, ypairs, xpairs) {
                    return true;
                }
                xpairs.pop();
            }
        } else {
            let x1 = rest.trailing_zeros() as usize;
            for x2 in x1 + 1..self.w {
                if (rest & (1 << x2)) == 0
                /*|| self.xcnts[x1] != self.xcnts[x2]*/
                {
                    continue;
                }
                // println!("{} {} {}", rest, x1, x2);
                let nrest = rest ^ (1 << x1) ^ (1 << x2);
                assert!(nrest < rest);
                xpairs.push((x1, x2));
                if self.xdfs(nrest, ypairs, xpairs) {
                    return true;
                }
                xpairs.pop();
            }
        }
        false
    }
    fn ydfs(&self, rest: usize, pairs: &mut Vec<(usize, usize)>) -> bool {
        if rest == 0 {
            return self.xdfs((1 << self.w) - 1, pairs, &mut vec![]);
        }
        if pairs.len() == 0 && self.h % 2 == 1 {
            for y in 0..self.h {
                // if !self.ycenter[y] {
                //     continue;
                // }
                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
                /*|| self.ycnts[y1] != self.ycnts[y2]*/
                {
                    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();
}