def f(board, width, height) visited = height.times.map{[0]*width} g = lambda{|h, w, n, cnt=0, from=nil| return if !h.between?(0, height-1) return if !w.between?(0, width-1) return if n != board[h][w] if visited[h][w] > 0 # p [n, cnt, [h,w]] return cnt + visited[h][w] > 4 end cnt += 1 visited[h][w] = cnt 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| next if visited[h][w] > 0 if g.(h, w, board[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