#include #include #include #include bool solve(int W, int H, std::vector> M) { std::vector> G(H * W); auto grid2int = [&W](int h, int w) { return h * W + w; }; for (int h = 0; h < H - 1; h++) { for (int w = 0; w < W; w++) { if (M.at(h).at(w) == M.at(h + 1).at(w)) { G.at(grid2int(h, w)).push_back(grid2int(h + 1, w)); G.at(grid2int(h + 1, w)).push_back(grid2int(h, w)); } } } for (int h = 0; h < H; h++) { for (int w = 0; w < W - 1; w++) { if (M.at(h).at(w) == M.at(h).at(w + 1)) { G.at(grid2int(h, w)).push_back(grid2int(h, w + 1)); G.at(grid2int(h, w + 1)).push_back(grid2int(h, w)); } } } std::unordered_set nodes; for (int i = 0; i < H * W; i++) { nodes.insert(i); } while (!nodes.empty()) { auto v0 = *nodes.begin(); nodes.erase(nodes.begin()); std::stack> stk; stk.emplace(v0, -1); while (!stk.empty()) { auto [v, p] = stk.top(); stk.pop(); nodes.erase(v); for (const auto &x: G.at(v)) { if (x == p) { continue; } if (nodes.find(x) == nodes.end()) { return true; } stk.emplace(x, v); } } } return false; } int main() { int W, H; std::cin >> W >> H; std::vector> M(H, std::vector(W)); for (int h = 0; h < H; h++) { for (int w = 0; w < W; w++) { std::cin >> M.at(h).at(w); } } const std::string ans = solve(W, H, M) ? "possible" : "impossible"; std::cout << ans << std::endl; }