def f(board, width, height) visited = {} g = lambda{|h, w, n=board[h][w], cnt=0, from=nil| return if !h.between?(0, height-1) return if !w.between?(0, width-1) return if n != board[h][w] # p [n, cnt, [h,w]] return !from.nil? if visited[[h,w]] cnt += 1 visited[[h,w]] = true from != :l && g.(h, w-1, n, cnt, :r) || from != :r && g.(h, w+1, n, cnt, :l) || from != :u && g.(h-1, w, n, cnt, :d) || from != :d && g.(h+1, w, n, cnt, :u) } # visited.each{|l| puts l.join} height.times{|h| width.times{|w| if g.(h, w) # p [h,w] # visited.each{|l| puts l.join} return true end } } # visited.each{|l| puts l.join} false end W, H = gets.split.take(2).map(&:to_i) M = H.times.map{gets.split.take(W).map(&:to_i)} puts f(M, W, H) ? :possible : :impossible