Consider an *n* × *m* grid. Initially all the cells of the grid are colored white. Lenny has painted some of the cells (at least one) black. We call a painted grid convex if one can walk from any black cell to any another black cell using a path of side-adjacent black cells changing his direction at most once during the path. In the figure below, the left grid is convex while the right one is not convex, because there exist two cells which need more than one time to change direction in their path.

You're given a painted grid in the input. Tell Lenny if the grid is convex or not.
The first line of the input contains two integers *n* and *m* (1 ≤ *n*, *m* ≤ 50) — the size of the grid. Each of the next *n* lines contains *m* characters "B" or "W". Character "B" denotes a black cell of the grid and "W" denotes a white cell of the grid.

It's guaranteed that the grid has at least one black cell.
import bisect
import itertools
import sys
import collections

n, m = map(int, input().split())
grid = [list(input()) for _ in range(n)]
# print('\n'.join(map(str, grid)))

# row check
good = True
for i in range(n):
    b = False
    w = False
    for j in range(m):
        if grid[i][j] == 'B' and not b:
            b = True
        elif grid[i][j] == 'W' and b and not w:
            w = True
        elif grid[i][j] == 'B' and b and w:
            good = False
            # print('rowcheck', i, j)
            break
    if not good:
        break

# col check
if good:
    for j in range(m):
        b = False
        w = False
        for i in range(n):
            if grid[i][j] == 'B' and not b and not w:
                b = True
            elif grid[i][j] == 'W' and b and not w:
                w = True
            elif grid[i][j] == 'B' and b and w:
                good = False
                # print('colcheck', i, j)
                break
        if not good:
            break
        
if good:
    def temp(x, y):
        q = collections.deque()
        grid[x][y] = 'W'
        q.append((x, y))
        
        while q:
            i, j = q.popleft()
            for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                ii, jj = i + di, j + dj
                if 0 <= ii < n and 0 <= jj < m and grid[ii][jj] == 'B':
                    grid[ii][jj] = 'W'
                    q.append((ii, jj))
                
    filled = False
    for i in range(n):
        for j in range(m):
            if grid[i][j] == 'B':
                if not filled:
                    # print('filling', i, j)
                    filled = True
                    temp(i, j)
                else:
                    # print('what?', i, j)
                    good = False
                    break
        if not good:
            break
        
print("YES" if good else "NO")