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::cmp;

fn read_line() -> String {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).ok();
    buf
}

fn search2(
    hist_y: &[usize],
    h:usize,
    w:usize, 
    map: &Vec<Vec<char>>, 
    used: usize
) -> bool {
    let mut used = used;
    for tar in 0..w {
        if used&(1 << tar) > 0 {
            continue;
        }
        used |= 1 << tar;
        let mut found = false;
        for tar2 in tar+1..w {
            if used&(1 << tar2) > 0 {
                continue;
            }
            let mut flag = true;
            for i in 0..h {
                let y1 = hist_y[i];
                let y2 = hist_y[h -1 - i];
                if map[y1][tar]!=map[y2][tar2] {
                    flag = false;
                    break;
                }
            }
            if flag {
                used |= 1 << tar2;
                found = true;
                break;                
            }
        }
        if !found {
            return false;
        }
    }
    return true;
}

fn search1(
    current: usize,
    hist_y: &mut [usize],
    h: usize,
    w: usize,
    map: &Vec<Vec<char>>,
    used: usize
) -> bool {
    if current >= h/2 {
        if h%2 > 0 {
            for tar in 0..h {
                if used&(1 << tar) == 0 {
                    hist_y[h/2] = tar;
                    break;
                }
            }
        }
        if w%2 > 0 {
            for tar in 0..w {
                let mut flag = true;
                for i in 0..h/2 {
                    let y1 = hist_y[i];
                    let y2 = hist_y[h - 1 - i];
                    if map[y1][tar] != map[y2][tar] {
                        flag = false;
                        break;
                    }
                }
                if flag && search2(hist_y, h ,w, map, 1 << tar) {
                    return true;
                }
            }
            return false;
        }
        return search2(hist_y, h ,w, &map, 0)
    }

    for tar in 0..h {
        if used&(1 << tar) > 0 {
            continue;
        }
        for tar2 in tar+1..h {
            if used&(1 << tar2) == 0 {
                hist_y[current] = tar;
                hist_y[h -1 - current] = tar2;
                let nused = used | (1 << tar) | (1 << tar2);
                if search1(current + 1, hist_y, h, w, &map, nused) {
                    return true;
                }
            }
        }
    }
    return false;
}


fn main() {
    let hw:Vec<usize> = read_line().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let (h,w) = (hw[0], hw[1]);
    let map:Vec<Vec<char>> = (0..h).map(|_| read_line().trim().chars().collect()).collect();

    let mut hist_y = vec![0; h];

    if search1(0, &mut hist_y, h, w, &map, 0) {
        println!("YES");
    } else {
        println!("NO");
    }
}