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 Ruby (2.3.3) (judge verdict: Accepted)

H , W = gets.split(' ').map(&:to_i)
ss = Array.new(H){Array.new(W)}
H.times do |i|
    ss[i] = gets.chomp.split('')
end

# make pair patterns by DFS
def make_pair_patterns(pool)
    return [[]] if pool.length == 0

    perms = []
    visited = {}
    stack = [pool.min]
    pool.delete(pool.min)
    visited[stack.dup] = true

    while stack.length > 0 do
        if pool.length == 0
            perms.push(stack.dup)
            pool.push(stack.pop)
            next
        end
        if stack.length % 2 == 0
            use = pool.min
            if visited[stack + [use]]
                pool.push(stack.pop)
                next
            end
        else
            use = pool.reject{|i| visited[stack + [i]]}.min
            unless use
                pool.push(stack.pop)
                next
            end
        end
        stack.push(use)
        visited[stack.dup] = true
        pool.delete(use)
    end
    return perms
end

if H % 2 == 0
    perms = make_pair_patterns((0...H).to_a)
else
    perms = []
    (0...H).each do |rest|
        pool = (0...H).to_a
        pool.delete(rest)
        patterns = make_pair_patterns(pool)
        patterns.each do |arr|
            arr.push(rest)
        end
        perms.concat(patterns)
    end
end

perms.each do |perm|
    pss = Array.new(H){Array.new(W)}
    (0...(H/2)).each do |i|
        pss[i] = ss[perm[2 * i]]
        pss[H - 1 - i] = ss[perm[2 * i + 1]]
    end
    pss[H/2] = ss[perm[-1]] if H % 2 == 1

    tss = Array.new(W){Array.new(H)}
    W.times do |i|
        H.times do |j|
            tss[i][j] = pss[j][i]
        end
    end

    center_available = (W % 2 == 1)
    success = true
    while tss.length > 0 do
        reverse = tss[0].reverse
        tss.delete_at(0)
        i_rev = tss.find_index(reverse)
        if i_rev
            tss.delete_at(i_rev)
        else
            if center_available && reverse.reverse == reverse
                center_available = false
            else
                success = false
                break
            end
        end
    end
    if success
        print 'YES'
        exit
    end
end

print 'NO'