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)

// https://atcoder.jp/contests/arc095/tasks/arc095_c
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

type Pairs = Vec<(usize,usize)>;

fn make_pairs(n: usize, left: usize, taken: usize, pairs: Pairs) -> Vec<Pairs> {
    if left <= 1 {
        return vec![pairs];
    }

    let mut w = vec![];
    for i in 0..n {
        if (taken>>i)&1 == 1 {
            continue;
        }
        for j in i+1..n {
            if (taken>>j)&1 == 0 {
                let mut tp = pairs.clone();
                tp.push((i, j));
                let tt = taken|(1<<i)|(1<<j);
                w.append(&mut make_pairs(n, left-2, tt, tp));
            }
        }
        break;
    }
    w
}


fn make_table(s: &Vec<Vec<char>>, pairs: Pairs) -> Vec<Vec<char>> {
    let n = s.len();
    let m = s[0].len();

    let pn = pairs.len();
    let mut col_perm = vec![0; m];
    let mut total = 0;
    for i in 0..pn {
        let (l, r) = pairs[i];
        col_perm[i] = l;
        col_perm[m-1-i] = r;
        total += l+r;
    }
    if m % 2 == 1 {
        col_perm[m/2] = m*(m-1)/2-total;
    }

    let mut ret = dvec!(' '; n, m);
    for i in 0..n {
        for j in 0..m {
            ret[i][j] = s[i][col_perm[j]];
        }
    }
    ret
}

fn is_reverse(a: &Vec<char>, b: &Vec<char>) -> bool {
    let m = a.len();
    for j in 0..m {
        if a[j] != b[m-1-j] {
            return false;
        }
    }
    true
}

fn is_ok(table: Vec<Vec<char>>) -> bool {
    // debug!(table);

    let n = table.len();
    let m = table[0].len();
    let mut used = vec![false; n];

    let mut no_counterpart = 0;
    for i in 0..n {
        if used[i] {
            continue;
        }
        let mut found = n;
        for ti in i+1..n {
            if is_reverse(&table[i], &table[ti]) {
                found = ti;
                break;
            }
        }
        if found == n {
            if is_reverse(&table[i], &table[i]) {
                no_counterpart += 1;
            } else {
                no_counterpart += 10000;
            }
        } else {
            used[i] = true;
            used[found] = true;
        }
    }
    ifv!(n % 2 == 1, no_counterpart == 1, no_counterpart == 0)
}

fn main() {
    input! {
        h: usize, w: usize,
        s: [chars; h]
    };

    for pairs in make_pairs(w, w, 0, vec![]) {
        let table = make_table(&s, pairs);
        if is_ok(table) {
            println!("YES");
            return;
        }
    }
    println!("NO");
}