fun main(args: Array){ val (a1, a2) = readLine()!!.split(" ").map { it.toInt() } w = a1 h = a2 for(i in 1..h) { map.add(readLine()!!.trim().split(" ").map { it.toInt() }.toTypedArray()) flags.add((1..w).map { false }.toTypedArray()) } for(i in 0..h-1) { for(j in 0..w-1) { val history = mutableMapOf>() history[i] = mutableMapOf() history[i]!![j] = true if(getAns(i, j, -1, -1, history)) { println("possible") return } } } println("impossible") } var w = 0 var h = 0 val map = mutableListOf>() var flags = mutableListOf>() fun getAns(y:Int, x:Int, prevY:Int, prevX:Int, history:MutableMap>): Boolean { if(flags[y][x]) { return false } flags[y][x] = true val num = map[y][x] for(i in Math.max(0, y-1)..Math.min(h-1, y+1)) { for(j in Math.max(0, x-1)..Math.min(w-1, x+1)) { if(i!=y && j!=x) { continue } if(i==y&&j==x) { continue } if(i ==prevY && j ==prevX) { continue } if(map[i][j] != map[y][x]) { continue } if(history.containsKey(i) && history[i]!!.containsKey(j)) { return true } if(!history.containsKey(i)) { history[i] = mutableMapOf() } history[i]!![j] = true val ret = getAns(i, j, y, x, history) history[i]!!.remove(j) if(history[i]!!.size == 0) { history.remove(i) } if(ret) { return true } } } return false }