#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main() { int w, h; cin >> w >> h; int m[w][h]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> m[j][i]; } } int memo[w][h]; memset(memo, -1, sizeof(memo)); queue qu; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; bool ok = false; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (memo[j][i] == -1) { qu.push(j); qu.push(i); memo[j][i] = 0; while (!qu.empty()) { int x = qu.front(); qu.pop(); int y = qu.front(); qu.pop(); for (int k = 0; k < 4; k++) { int nx = x + dx[k]; int ny = y + dy[k]; if (nx < 0 || ny < 0 || nx >= w || ny >= h || m[nx][ny] != m[x][y] || (memo[nx][ny] != -1 && memo[nx][ny] == memo[x][y]-1)) { continue; } if (memo[nx][ny] != -1) { cout << "possible" << endl; return 0; } memo[nx][ny] = memo[x][y]+1; qu.push(nx); qu.push(ny); } } } } } cout << "impossible" << endl; }