# うーん・・・ T = $stdin.read.lines.map{|s| s.split.map(&:to_i)} W, H = T[0].size, T.size OFFSETS = [[-1, 0], [+1, 0], [0, -1], [0, +1]] def check(map, x, y, a) return true if map == T OFFSETS.each do |ox, oy| new_x = x + ox new_y = y + oy if 0 <= new_x && new_x < W && 0 <= new_y && new_y < H then n = map[new_y][new_x] unless a[n] then new_map = map.map{|r| r.dup} new_map[y][x] = n new_map[new_y][new_x] = 0 new_a = a.dup new_a[n] = true return true if check(new_map, new_x, new_y, new_a) end end end return false end map = Array.new(H){|i| Array.new(W){|j| i * H + j + 1}} x, y = W - 1, H - 1 map[y][x] = 0 puts check(map, x, y, Array.new(W * H)) ? "Yes" : "No"