#P7479. Determining the Life Status of a Black Go Group

    ID: 20674 Type: Default 1000ms 256MiB

Determining the Life Status of a Black Go Group

Determining the Life Status of a Black Go Group

Consider an n×mn\times m Go board. Each cell is either empty or contains a black stone. It is guaranteed that all black stones form a single connected group (i.e. for any two black stones (i1,j1)(i_1,j_1) and (i2,j2)(i_2,j_2) there exists a path through adjacent (Manhattan distance 1) black stones).

In Go, the liberties of a stone are defined as the set of all empty positions adjacent (up, down, left, right) to it. The liberties of a connected group of stones is the union of the liberties of all stones in that group.

A white move is legal if and only if, after placing a white stone in an empty cell, either:

  1. The connected white group (formed by that stone and any adjacent white stones) has at least one liberty, or
  2. The black group’s liberties become zero immediately (i.e. the move captures the black group).

We say that the black group is "alive" (i.e. an \emph{alive group}) if, no matter how many consecutive legal moves white plays, the black group always maintains at least one liberty. Otherwise, it is not alive.

Your task is to determine whether the black group is alive. Output YES if it is alive, and NO otherwise.

\textbf{Note:} A key observation is that white is trying to fill all the liberties of the black group. However, due to the legality rule, white cannot fill an isolated empty cell (i.e. a component of liberties of size exactly 11) as an intermediate move because such a move would be suicidal (unless it is the final move that captures the entire group). Thus, if there exist at least two isolated liberty cells (components of size 11) among the black group’s liberties, then white cannot fill them both, and the black group is considered alive.

inputFormat

The first line contains two integers nn and mm (1n,m10001 \le n,m \le 1000), representing the number of rows and columns of the board.

Each of the following nn lines contains a string of length mm consisting of the characters 'B' and '.', where 'B' denotes a black stone and '.' denotes an empty cell. It is guaranteed that the black stones form a single connected group.

outputFormat

Output a single line with either YES if the black group is alive or NO if it is not.

sample

3 3
...
.B.
...
YES