Problem B
Piece It Together

Tom has developed a special kind of puzzle: it involves a whole bunch of identical puzzle pieces. The pieces have the shape of three adjoint squares in an L-shape. The corner square is black, the two adjacent squares are white.

Figure 1: A puzzle piece

The puzzler is given a pattern of black and white squares in a rectangular grid. The challenge is to create that pattern using these pieces. The pieces can be rotated, but must not overlap.

Tom has already designed a few nice patterns, but he needs to find out if they can be constructed with the pieces at all. Rather than trying to test this for each pattern by hand, he wants to write a computer program to determine this for him. Can you help him?


The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 500$): the height and width of the grid containing the pattern, respectively. The next $n$ lines contain $m$ characters each, denoting the grid. Each character is ‘B’, ‘W’, or ‘.’, indicating a black, white or empty square respectively. The grid contains at least one black or white square.


Output “YES” or “NO”, indicating whether or not it is possible to construct the pattern with the puzzle pieces. You may assume that there is an infinite supply of pieces.

Sample Input 1 Sample Output 1
3 4
Sample Input 2 Sample Output 2
3 3

Please log in to submit a solution to this problem

Log in