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 sys
#import threading
#sys.setrecursionlimit(10**8)
#threading.stack_size(10**8)
input = sys.stdin.readline

############ ---- Input Functions ---- ############
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))
############ ---- Input Functions ---- ############

def Convex_Shape2():
    n,m = invr()

    full_matrix = []
    row_pos = []
    col_pos = [] 

    for row_index in range(n):
        row = insr()
        full_matrix.append(row)

        black_started = False 
        black_ended = False 

        for col_index in range(m):
            if row[col_index] == 'B':
                row_pos.append(row_index)
                col_pos.append(col_index)

                if not black_started:
                    black_started = True

                elif black_started and black_ended:
                    print("NO")
                    return 

            elif row[col_index] == 'W' and black_started:
                black_ended = True  
    
    for col_index in range(m):

        black_started = False 
        black_ended = False 

        for row_index in range(n): 

            if full_matrix[row_index][col_index] == 'B':
                if not black_started:
                    black_started = True 
                elif black_started and black_ended:
                    print("NO")
                    return 
            
            elif full_matrix[row_index][col_index] == 'W' and black_started:
                black_ended = True 
    
    num_black_squares = len(row_pos)

    for black_squre_index in range(num_black_squares):
        for next_index in range(black_squre_index+1,num_black_squares):
            row_inital = row_pos[black_squre_index]
            row_target = row_pos[next_index]
            col_initial = col_pos[black_squre_index]
            col_target = col_pos[next_index]

            if full_matrix[row_inital][col_target] != 'B' and full_matrix[row_target][col_initial] != 'B':
                print("NO")
                return 
    
    print("YES")
    return 

Convex_Shape2()