#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair P; typedef pair PP; int H, W; int M[110][110]; vector> cd; #define mp(a,b,c,d) make_pair( make_pair((a),(b)),make_pair((c),(d)) ) #define dir(xx,yy,x,y,i)(xx)=(x)+dir[(i)],(yy)=(y)+dir[(i)+1]; const int dir[] = { 0, 1, 0, -1, 0 }; bool bfs(int c){ cout << c << endl; queue que; for (auto &u : cd[c]) if(M[u.first][u.second]==c){ que.push(mp(u.first, u.second, -1, -1)); M[u.first][u.second] *= -1; while (que.empty() == false){ auto v = que.front(); que.pop(); for (int i = 0; i < 4; i++){ int xx, yy; dir(xx, yy, v.first.first, v.first.second, i); if ((xx != v.second.first) || (yy != v.second.second)){ if (M[xx][yy] == -c){ return true; } if (M[xx][yy] == c){ M[xx][yy] *= -1; que.push(mp(xx, yy, v.first.first, v.first.second)); } } } } } return false; } int main(void){ cd.resize(1010); cin >> W >> H; for (int i = 1; i <=H; i++) { for (int j = 1; j <= W; j++) { cin >> M[i][j]; cd[M[i][j]].push_back(make_pair(i, j)); } } for (int i = 1; i <= 1000; i++) if (cd[i].size() >= 4) if (bfs(i)) { cout << "possible" << endl; return 0; } cout << "impossible" << endl; return 0; }