def g;gets.split.map(&:to_i);end w,h=g $m=(1..h).map{g+[0]}+[0]*w $checked={} def dfs(x,y) return if $checked[[x,y]] r=[[0,-1],[1,0],[-1,0],[0,1]] c=$m[y][x] stack=[[-1,x,y]] while stack[0] dir,tx,ty=stack.pop if $m[ty][tx]==c return true if $checked[[tx,ty]] $checked[[tx,ty]]=true 4.times{|i|i!=dir&&stack.push([3-i,tx+r[i][0],ty+r[i][1]])} end end end puts (0...h).any?{|y|(0...w).any?{|x|dfs(x,y)}}?"possible":"impossible"