h, w = gets.not_nil!.split(" ").map{|x| x.to_i} rect = [] of Array(String) 1.upto(h) do |i| rect << gets.not_nil!.split("") end def ok?(rect, h, w) 0.upto(h-1) do |dy| 0.upto(w-1) do |dx| if dx == 0 && dy == 0 next end _rect = rect.clone ok = true 0.upto(h-1) do |y| 0.upto(w-1) do |x| if _rect[y][x] == "." next end ny = y + dy nx = x + dx if nx >= w || ny >= h next end if _rect[y][x] == _rect[ny][nx] _rect[y][x] = "." _rect[ny][nx] = "." else ok = false break end end end 0.upto(h-1) do |y| 0.upto(w-1) do |x| if _rect[y][x] == "#" ok = false end end end if ok return true end end end return false end puts ok?(rect, h, w) ? "YES" : "NO"