(h,w),*m=$<.map{|l|l.split.map(&:to_i)+[0]}+[[0]*100] (h*w).times{|i| *a=i ( v,*a=a # m[y=v%h][v/h]==$c&&(m[y][v/h]=-1;((y<1?[]:[-1])+[1,-h,h]).count{|w|a<1&&($><<:possible;exit)) m[y=v%h][v/h]==$c&&(m[y][v/h]=-1;((y<1?[]:[-1])+[1,-h,h]).count{|w|w+=v;t=m[w%h][w/h];t==$c&&a<1&&($><<:possible;exit)) )while a[0]if 0<$c=m[i%h][i/h] m.map{|n|n.map!{|t|t<0?0:t}} } $><<:impossible