#K93222. Number Bridges

    ID: 38372 Type: Default 1000ms 256MiB

Number Bridges

Number Bridges

You are given a grid with N rows and M columns. Each cell of the grid contains an integer value. Starting from the top-left cell (0-indexed), your task is to determine whether it is possible to travel to the bottom-right cell following the rule that you can only move to a cell (up, down, left, or right) if the value in the current cell is strictly greater than the value of the adjacent cell. Formally, you can move from cell \( (i,j) \) to \( (x,y) \) if and only if:

[ grid[i][j] > grid[x][y] ]

You have to process multiple test cases. Each test case starts with two integers \(N\) and \(M\). If both are zero, the input terminates. For each test case, output "Possible" if there exists a path from the top-left corner to the bottom-right corner following the above rule, otherwise output "Impossible".

inputFormat

The input is given via standard input in the following format:

  • Multiple test cases.
  • For each test case, the first line contains two space-separated integers \(N\) and \(M\) denoting the number of rows and columns respectively.
  • The following \(N\) lines each contain \(M\) space-separated integers representing the grid.
  • The end of input is indicated by a line containing "0 0".

outputFormat

For each test case, print a single line with either "Possible" or "Impossible" on standard output, indicating whether it is possible to reach the bottom-right cell from the top-left cell following the rule.

## sample
3 3
3 1 2
0 2 1
1 1 3
0 0
Impossible

</p>