#C10023. Escape the Grid

    ID: 39183 Type: Default 1000ms 256MiB

Escape the Grid

Escape the Grid

You are given a grid with n rows and m columns. Each cell in the grid is either a free cell (denoted by 0) or an obstacle (denoted by 1). Starting from the top‐left cell (1, 1), your task is to determine whether there exists a valid path to the bottom‐right cell (n, m) by moving only down or right, without stepping onto any obstacles.

The problem can be mathematically modeled by considering each cell (i, j) valid if and only if $$grid[i][j] = 0$$. Use depth-first search (DFS) to explore the grid and decide if such a path exists.

inputFormat

The input is read from stdin. The first line contains two integers n and m representing the number of rows and columns respectively. The following n lines contain m space-separated integers each (either 0 or 1) which describe the grid. The starting cell is at position (1, 1) and the target cell is at (n, m).

outputFormat

Output a single line to stdout containing YES if there exists a path from the starting cell to the target cell while avoiding obstacles, otherwise output NO.

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