import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; // SList, DList, BinaryHeap void main() { auto rd = readln.split.to!(size_t[]), w = rd[0], h = rd[1]; auto mij = h.iota.map!(_ => readln.split.to!(int[])).array; int[] usedDigits() { bool[int] b; foreach (mi; mij) foreach (m; mi) b[m] = true; return b.keys; } bool valid(point p) { return p.x >= 0 && p.y >= 0 && p.x < w && p.y < h; } bool calc(int d) { auto vij = new bool[][](h, w); point findDigit() { foreach (int i, mi; mij) foreach (int j, m; mi) if (m == d && !vij[i][j]) return point(j, i); return point(-1, -1); } bool wfs(point s) { auto q = DList!pointsCP(pointsCP(s, point(-1, -1))); vij[s.y][s.x] = true; while (!q.empty) { auto cp = q.front; q.removeFront; auto curr = cp.curr, prev = cp.prev; foreach (sp; sibPoints) { auto np = curr + sp; if (valid(np) && np != prev && mij[curr.y][curr.x] == mij[np.y][np.x]) { if (vij[np.y][np.x]) return true; vij[np.y][np.x] = true; q.insertBack(pointsCP(np, cp.curr)); } } } return false; } for (;;) { auto s = findDigit; if (s.x == -1 || s.y == -1) return false; if (wfs(s)) return true; } } auto di = usedDigits; writeln(di.any!calc ? "possible" : "impossible"); } struct pointsCP { point curr; point prev; } struct Point(T) { T x, y; point opBinary(string op)(point rhs) { static if (op == "+") return point(x + rhs.x, y + rhs.y); } } alias Point!int point; const auto sibPoints = [point(-1, 0), point(0, -1), point(1, 0), point(0, 1)];