import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Main { public static final String IMPOSSIBLE = "impossible"; public static final String POSSIBLE = "possible"; int W; int H; int[][] matrix; int[][] visited; //クラスを使わずにしてみたかったので int getPos(int i, int j) { return j * W + i; } void input() { Scanner scanner = new Scanner(System.in); //番兵を含む W = scanner.nextInt() + 2; H = scanner.nextInt() + 2; matrix = new int[H][W]; for (int i = 1; i < H-1 ; i++) { for (int j = 1; j < W-1; j++) { matrix[i][j] = scanner.nextInt(); } } } public Main() { input(); visited = new int[H][W]; for (int i = 0; i < H; i++) { Arrays.fill(visited[i], -1); } for (int i = 1; i < H - 1; i++) { for (int j = 1; j < W - 1; j++) { if (visited[i][j] != -1) { continue; } if (bfs(i, j)) { System.out.println(POSSIBLE); return; } } } System.out.println(IMPOSSIBLE); } private boolean bfs(int startX, int startY) { LinkedList queue = new LinkedList(); queue.add(getPos(startX, startY)); int currentNum = matrix[startX][startY]; visited[startX][startY] = 0; while (queue.size() > 0) { final Integer next = queue.pop(); int x = next % W; int y = next / W; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { int p1 = x + i; int p2 = y + j; if (Math.abs(i + j) == 1 && matrix[p1][p2] == currentNum) { //次の場所が別の親になっているかどうかをチェックします。 if (visited[p1][p2] == 0 || visited[x][y] == getPos(p1, p2)) { continue; } if (visited[p1][p2] != -1) { //この時点で囲める return true; } //次の場所の親が元の場所と設定します。 visited[p1][p2] = getPos(x, y); queue.add(getPos(p1, p2)); } } } } return false; } public static void main(String[] args) { new Main(); }}