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)

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[i] || done[j]
        if symmetry?(i, j)
          done[i], done[j] = true, true
        end
      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 dfs(p, done, pairs)
  if done.count(true) == $h - ($h.odd? ? 1 : 0)
    remain = done.find_index(false)
    remains = remain ? [$g[remain]] : []
    permutation(pairs, remains)
    return
  end
  s = pairs.empty? ? 0 : pairs[p-1][0] + 1
  (s..($h-1)).each do |i|
    next if done[i]
    (i+1..$h).each do |j|
      next if done[i] || done[j]
      next unless $is_pair[i][j]
      done[i], done[j] = true, true
      pairs << [i, j]
      dfs(p + 1, done, pairs)
      done[i], done[j] = false, false
      pairs.pop
    end
  end
end

def create_is_pair
  same_chars = $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 }
  ps = same_chars.map { |k,v| v }
  $is_pair = Hash.new { |k,v| k[v] = Hash.new(false) }
  ps.each do |a|
    n = a.size
    n.times do |i|
      n.times do |j|
        $is_pair[a[i]][a[j]] = true if i != j
      end
    end
  end
end

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

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

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