#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } //constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; const long double PI = acos((long double)(-1)); using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; void dfs_tree(int cur, int pre, vector& used, vector>& rest, vector>& g, vector>& tree) { used[cur] = true; for (auto next : g[cur]) { if (next == pre) continue; if (used[next]) { rest.emplace_back(cur, next); } else { tree[cur].emplace_back(next); tree[next].emplace_back(cur); dfs_tree(next, cur, used, rest, g, tree); } } } int dh[] = { 1,-1,0,0 }; int dw[] = { 0,0,1,-1 }; int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ int H, W; scanf("%d %d", &W, &H); vector> a(H, vector(W)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) scanf("%d", &a[i][j]); vector> used(H, vector(W)); vector f(H * W); vector> g(H * W); vector> tree(H * W); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (used[i][j]) continue; queue> q; used[i][j] = true; q.emplace(i, j); while (!q.empty()) { auto [h, w] = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int nh = h + dh[i]; int nw = w + dw[i]; if (nh < 0 or nh >= H or nw < 0 or nw >= W) continue; if (a[nh][nw] != a[h][w]) continue; if (h < nh or w < nw) g[h * W + w].emplace_back(nh * W + nw), g[nh * W + nw].emplace_back(h * W + w); if (used[nh][nw]) continue; used[nh][nw] = true; q.emplace(nh, nw); } } vector> rest; dfs_tree(i * W + j, -1, f, rest, g, tree); if (!rest.empty()) { puts("possible"); return 0; } } } puts("impossible"); return 0; }