#C4450. Pathfinding in an Obstacle Grid

    ID: 47990 Type: Default 1000ms 256MiB

Pathfinding in an Obstacle Grid

Pathfinding in an Obstacle Grid

You are given a grid represented by an n x m matrix of integers. A cell with value 0 indicates a free cell, while a cell with value 1 is an obstacle. Starting from the top-left corner of the grid, your goal is to determine whether there exists a path to the bottom-right corner. However, you are only allowed to move either to the right or downward at each step.

If a valid path exists, output YES; otherwise, output NO.

Note: Movement is limited to rightward or downward directions only. This means you cannot move left or upwards even if it could potentially lead to the destination.

inputFormat

The input begins with an integer T representing the number of test cases. Each test case is described as follows:

  • The first line of each test case contains two space-separated integers n and m, representing the number of rows and columns respectively.
  • The next n lines each contain m space-separated integers (each integer is either 0 or 1), representing the grid.

All input is provided via standard input (stdin).

outputFormat

For each test case, output a single line containing either YES if there exists a valid path from the top-left to the bottom-right corner, or NO otherwise. All output should be printed to standard output (stdout).

## sample
2
3 3
0 0 0
1 1 0
0 0 0
3 3
0 1 0
1 0 0
0 0 0
YES

NO

</p>