#include #include #include #include #define REP(i, a, b) for (int i = int(a); i < int(b); i++) #define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl using namespace std; typedef long long int lli; template vector make_v(size_t a, T b) { return vector(a, b); } template auto make_v(size_t a, Ts... ts) { return vector(a, make_v(ts...)); } class UnionFind { private: vector par; public: UnionFind(int n) { par.resize(n); REP(i, 0, n) { par[i] = i; } } int Find(int n) { return (par[n] == n ? n : par[n] = Find(par[n])); } bool Union(int a, int b) { a = Find(a); b = Find(b); if (a == b) return false; par[a] = b; return true; } bool Same(int a, int b) { return (Find(a) == Find(b)); } }; int main() { int W, H; cin >> W >> H; auto M = make_v(H + 2, W + 2, 0); REP(i, 1, H + 1) { REP(j, 1, W + 1) { cin >> M[i][j]; } } UnionFind uf((H + 2) * (W + 2)); const int dx[2] = {0, 1}; const int dy[2] = {1, 0}; bool ok = false; REP(i, 1, H + 1) { REP(j, 1, W + 1) { int bs = (W + 2) * i + j; REP(k, 0, 2) { int nx = j + dx[k]; int ny = i + dy[k]; int nb = (W + 2) * ny + nx; if (M[i][j] != M[ny][nx]) continue; if (!uf.Union(bs, nb)) { ok = true; } } } } cout << (ok ? "possible" : "impossible") << endl; return 0; }