#include #include using namespace std; int w, h; int vertexCount; bool connected[10000][10000] = { false }; bool vertex[10000]; bool visited[10000]; int index(int x, int y) { return x + y * w; } bool scan(int start, int from) { if (visited[start]) { return true; } visited[start] = true; for (int i = 0; i < vertexCount; ++i) { if (i != from && connected[start][i]) { if (scan(i, start)) { return true; } } } return false; } int main() { cin >> w >> h; vector > m(w, vector(h)); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { cin >> m[x][y]; } } vertexCount = w * h; bool found = false; for (int i = 1; i <= 1000 && !found; ++i) { fill(&vertex[0], &vertex[vertexCount - 1] + 1, false); fill(&visited[0], &visited[vertexCount - 1] + 1, false); for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { if (m[x][y] == i) { vertex[index(x, y)] = true; if (x > 0 && m[x - 1][y] == i) { connected[index(x, y)][index(x - 1, y)] = true; connected[index(x - 1, y)][index(x, y)] = true; } if (x < w - 1 && m[x + 1][y] == i) { connected[index(x, y)][index(x + 1, y)] = true; connected[index(x + 1, y)][index(x, y)] = true; } if (y > 0 && m[x][y - 1] == i) { connected[index(x, y)][index(x, y - 1)] = true; connected[index(x, y - 1)][index(x, y)] = true; } if (y < h - 1 && m[x][y + 1] == i) { connected[index(x, y)][index(x, y + 1)] = true; connected[index(x, y + 1)][index(x, y)] = true; } } } } for (int start = 0; start < vertexCount && !found; ++start) { if (vertex[start] && !visited[start]) { found |= scan(start, -1); } } } cout << (found ? "possible" : "impossible") << endl; return 0; }