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 itertools
import math
import threading
import time
from builtins import input, range
from math import gcd as gcd
import sys
from io import BytesIO, IOBase
import queue
import itertools
import collections
from heapq import heappop, heappush
import random
import os
from random import randint
import decimal
from math import factorial as fac


def solve():
    n, m = map(int, input().split())
    f = [str(input()) for i in range(n)]

    visited = set()
    q = []
    q_it = 0

    for i in range(n):
        ff = False
        for j in range(m):
            if f[i][j] == "B":
                q.append((i, j))
                ff = True
                break
        if ff:
            break

    if len(q) == 0:
        print("YES")
        return

    visited.add(q[0])
    while q_it < len(q):
        y, x = q[q_it]
        q_it += 1
        for dy, dx in [[-1, 0], [1, 0], [0, -1], [0, 1]]:
            yy, xx = y + dy, x + dx
            if 0 <= yy < n and 0 <= xx < m:
                if f[yy][xx] == "B" and (yy, xx) not in visited:
                    visited.add((yy, xx))
                    q.append((yy, xx))

    if len(visited) != sum([f[i].count("B") for i in range(n)]):
        print("NO")
        return

    for i in range(n):
        was_black = False
        was_white = False
        for j in range(m):
            if f[i][j] == "B" and was_black and was_white:
                print("NO")
                return
            if f[i][j] == "B":
                was_black = True
            else:
                was_white = True

    for i in range(m):
        was_black = False
        was_white = False
        for j in range(n):
            if f[j][i] == "B" and was_black and was_white:
                print("NO")
                return
            if f[j][i] == "B":
                was_black = True
            else:
                was_white = True

    print("YES")


if __name__ == '__main__':
    multytest = False

    if multytest:
        t = int(input())
        for i in range(t):
            solve()
    else:
        solve()
