// yukicoder: No.13 囲みたい! // 2019.5.26 bal4u #include #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0'); return n; } void outs(char *s) { while (*s) pc(*s++); pc('\n'); } typedef struct { int r, c, pr, pc; } Q; Q q[10005]; int top; char vis[103][103]; int W, H; int map[103][103]; int mv[4][2] = {{-1,0}, {0,1}, {1,0}, {0,-1}}; int dfs(int rr, int cc, int val) { int i, r, c, pr, pc; q[0].r = rr, q[0].c = cc, pr = -1, top = 1; while (top) { r = q[--top].r, c = q[top].c, pr = q[top].pr, pc = q[top].pc; if (vis[r][c]) return 1; vis[r][c] = 1; for (i = 0; i < 4; i++) { rr = r + mv[i][0], cc = c + mv[i][1]; if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue; if (rr == pr && cc == pc) continue; if (map[rr][cc] == val) { q[top].r = rr, q[top].c = cc, q[top].pr = r, q[top++].pc = c; } } } return 0; } int main() { int r, c; W = in(), H = in(); if (W == 1 || H == 1) { outs("impossible"); return 0; } for (r = 0; r < H; r++) for (c = 0; c < W; c++) map[r][c] = in(); for (r = 0; r < H; r++) for (c = 0; c < W; c++) if (!vis[r][c]) { if (dfs(r, c, map[r][c])) { outs("possible"); return 0; } } outs("impossible"); return 0; }