#C9835. Continuous Fertile Patch
Continuous Fertile Patch
Continuous Fertile Patch
You are given a garden represented as a grid with n rows and m columns. Each cell in the grid is either fertile (denoted by .
) or infertile (denoted by #
). Your task is to determine whether all fertile cells form a single, continuous patch. In other words, starting from any fertile cell, you should be able to reach every other fertile cell by moving only up, down, left, or right without passing through an infertile cell.
If all fertile cells are connected, print YES
. Otherwise, print NO
. It is also possible that the garden has no fertile soil at all; in that case, print NO
.
Note: Use standard input (stdin) to read data and standard output (stdout) to print your result.
inputFormat
The first line contains two space-separated integers n and m, representing the number of rows and columns respectively. The following n lines each contain a string of length m composed of characters '.' and '#', which represent fertile and infertile cells respectively.
outputFormat
Output a single line with either YES
if all the fertile cells are part of a single continuous patch, or NO
otherwise.## sample
4 5
..#..
.#...
.....
##...
YES
</p>