#B4040. Detecting a Special 4x4 Subrectangle in a Grid

    ID: 11697 Type: Default 1000ms 256MiB

Detecting a Special 4x4 Subrectangle in a Grid

Detecting a Special 4x4 Subrectangle in a Grid

You are given an ( n )-row by ( m )-column grid. Each cell in the grid is either white or black. Determine whether there exists a subrectangle of size (4) rows by (4) columns that satisfies the following conditions:

  • The first and fourth rows of the subrectangle contain only white cells.
  • In the second and third rows of the subrectangle, only the first and fourth cells are white, and the remaining two cells are black.

Formally, for a candidate subrectangle starting at row (i) and column (j) (using 0-indexing), the following must hold:

  1. For row (i) and row (i+3), every cell in columns (j) through (j+3) is white.
  2. For rows (i+1) and (i+2):
    • Cells at columns (j) and (j+3) are white.
    • Cells at columns (j+1) and (j+2) are black.

Output YES if such a subrectangle exists; otherwise, output NO.

Note: In this problem, the white cell is represented by the character 'W' and the black cell by the character 'B'.

inputFormat

The first line contains two integers \( n \) and \( m \) (\( 4 \le n, m \le 1000 \)), representing the number of rows and columns in the grid.

Each of the next \( n \) lines contains a string of length \( m \) consisting of characters 'W' and 'B', representing a row of the grid.

outputFormat

Output a single line containing either YES if there exists a valid subrectangle, or NO otherwise.

sample

4 4
WWWW
WBBW
WBBW
WWWW
YES