import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random; import std.string, std.conv, std.stdio, std.typecons; struct point { int x; int y; point opBinary(string op)(point rhs) { static if (op == "+") return point(x + rhs.x, y + rhs.y); else static if (op == "-") return point(x - rhs.x, y - rhs.y); } } const auto sibPoints = [point(-1, 0), point(0, -1), point(1, 0), point(0, 1)]; struct currPrev { point curr; point prev; } void main() { auto rd = readln.split.map!(to!int); auto w = rd[0], h = rd[1]; auto mij = iota(h).map!(_ => readln.split.map!(to!int).array).array; writeln(calc(h, w, mij) ? "possible" : "impossible"); } bool calc(int h, int w, int[][] mij) { auto visited = new bool[][](h, w); foreach (i; 0..h) { foreach (j; 0..w) { if (!visited[i][j]) { auto r = calcN(i, j, h, w, mij, visited); if (r) return true; } } } return false; } bool calcN(int i, int j, int h, int w, int[][] mij, ref bool[][] visited) { bool valid(point p) { return p.x >= 0 && p.x < w && p.y >= 0 && p.y < h; } auto stack = SList!currPrev(); stack.insertFront(currPrev(point(j, i), point(-1, -1))); while (!stack.empty) { auto s = stack.front, cp = s.curr, pp = s.prev; stack.removeFront; foreach (sib; sibPoints) { auto np = cp + sib; if (!valid(np) || mij[np.y][np.x] != mij[cp.y][cp.x] || np == pp) continue; if (visited[np.y][np.x]) return true; stack.insertFront(currPrev(np, cp)); visited[np.y][np.x] = true; } } return false; }