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: Runtime Error)

class Board
  def initialize(board)
    @board = board
  end

  def point_symmetry?
    done = Array.new($w, false)
    ($w-1).times do |i|
      next if done[i]
      (i+1..$w-1).each do |j|
        next if done[j]
        done[i], done[j] = true, true if symmetry?(i, j)
      end
    end
    if $w.odd? && done.count(false) == 1
      i = done.index(false)
      return symmetry?(i, i)
    end
    done.all?
  end

  def symmetry?(i, j)
    $h.times do |k|
      return false if @board[k][i] != @board[$h-k-1][j]
    end
    true
  end
end

def permutation(pairs, remains)
  pairs.permutation do |_pairs|
    a = []
    b = []
    _pairs.each do |pair|
      a << $g[pair[0]]
      b << $g[pair[1]]
    end
    board = Board.new(a + remains + b.reverse)
    if board.point_symmetry?
      puts 'YES'
      exit 0
    end
  end
end

def make_pairs(p, done, pairs)
  if p == $ps.size - ($h.odd? ? 1 : 0)
    remain = done.find_index(false)
    remains = remain ? [$g[remain]] : []
    permutation(pairs, remains)
    return
  end
  v = $ps[p]
  n = v.size
  (n-1).times do |i|
    next if done[v[i]]
    (i+1..n-1).each do |j|
      next if done[v[j]]
      done[v[i]], done[v[j]] = true, true
      pairs << [v[i], v[j]]
      make_pairs(p + 1, done, pairs)
      done[v[i]], done[v[j]] = false, false
      pairs.pop
    end
  end
end

$h, $w = gets.split.map(&:to_i)
$g = []

$h.times do
  $g << gets.chomp
end

$ps = $g.map.with_index { |s,i| [s.split('').sort.join, i] }.reduce(Hash.new { |k,v| k[v] = [] }) { |z, x| z[x[0]] << x[1]; z }.map { |k,v| v }

make_pairs(0, Array.new($h, false), [])
puts 'NO'