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