def g;gets.split.map(&:to_i);end w,h=g $m=(1..h).map{g+[0]}+[0]*w $checked={} def dfs(gone,dir,x,y,c) r=[[0,-1],[1,0],[-1,0],[0,1]] $m[y][x]==c&&($checked[[x,y]]=true;gone.index([x,y])||(0..3).any?{|i|i!=dir&&dfs(gone+[[x,y]],3-i,x+r[i][0],y+r[i][1],c)}) end puts (0...h).any?{|y|(0...w).any?{|x|!$checked[[x,y]]&&dfs([],-1,x,y,$m[y][x])}}?"possible":"impossible"