#include #include using namespace std; struct Point { int x; int y; }; int main() { const int MAX_HEIGHT = 100; const int MAX_WIDTH = 100; int dx[] = { -1, 1, 0, 0 }; int dy[] = { 0, 0, -1, 1 }; int map[MAX_WIDTH][MAX_HEIGHT]; bool visited[MAX_WIDTH][MAX_HEIGHT]; int w, h; cin >> w >> h; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { visited[x][y] = false; } } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> map[x][y]; } } queue> nodes; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { if (visited[x][y]) continue; Point start{ x, y }; nodes.push(make_pair(start, start)); visited[x][y] = true; while (!nodes.empty()) { Point current = nodes.front().first; Point prev = nodes.front().second; nodes.pop(); for (int dir = 0; dir < 4; dir++) { int nx = current.x + dx[dir]; int ny = current.y + dy[dir]; if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue; if (nx == prev.x && ny == prev.y) continue; if (map[nx][ny] != map[current.x][current.y]) continue; if (visited[nx][ny]) { cout << "possible" << endl; return 0; } visited[nx][ny] = true; nodes.push(make_pair(Point{ nx, ny }, current)); } } } } cout << "impossible" << endl; return 0; }