#C9973. Balanced Path in a Grid

    ID: 54125 Type: Default 1000ms 256MiB

Balanced Path in a Grid

Balanced Path in a Grid

You are given an (n \times m) grid consisting of characters '0' and '1'. Your task is to determine if there exists a path from the top-left corner (cell (0, 0)) to the bottom-right corner (cell (n-1, m-1)) moving only right or down such that the number of '0's is equal to the number of '1's along the path. The path includes both the starting and ending cells. Formally, if the number of '0's is (Z) and the number of '1's is (O) along a chosen path, then you should output "YES" if (O = Z), and "NO" otherwise.

inputFormat

The first line contains two integers (n) and (m), representing the number of rows and columns respectively. Each of the next (n) lines contains a string of length (m) consisting only of the characters '0' and '1', representing the grid.

outputFormat

Output a single line containing either "YES" or "NO". Output "YES" if there exists a valid path from the top-left to the bottom-right cell where the number of '0's equals the number of '1's; otherwise, output "NO".## sample

4 5
01101
10000
10010
00111
YES

</p>