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. [Image] 

You're given a painted grid in the input. Tell Lenny if the grid is convex or not.


-----Input-----

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.


-----Output-----

On the only line of the output print "YES" if the grid is convex, otherwise print "NO". Do not print quotes.


-----Examples-----
Input
3 4
WWBW
BWWW
WWWB

Output
NO

Input
3 1
B
B
W

Output
YES

Difficulty: interview

Starter code:

Solutions:
def f():
    n, m = map(int, input().split())
    t = [input() for j in range(n)]

    p = [''.join(i) for i in zip(*t)]
    if h(p): return 1

    i = 0
    while i < n and not 'B' in t[i]: i += 1

    while i < n:
        a = t[i].find('B')
        if a < 0:
            i += 1
            break
        b = t[i].rfind('B')
        if 'W' in t[i][a: b + 1]: return 1

        for j in range(i + 1, n):
            if a > 0 and t[j][a - 1] == 'B' and t[j][b] == 'W': return 1
            if b < m - 1 and t[j][b + 1] == 'B' and t[j][a] == 'W': return 1
        i += 1

    while i < n:
        if 'B' in t[i]: return 1
        i += 1    

    return 0  
            
def h(t):
    i, n = 0, len(t)
    while i < n and not 'B' in t[i]: i += 1

    while i < n:
        a = t[i].find('B')
        if a < 0:
            i += 1
            break
        b = t[i].rfind('B')
        if 'W' in t[i][a: b + 1]: return 1
        i += 1

    while i < n:
        if 'B' in t[i]: return 1
        i += 1    

    return 0          
    
print('YNEOS'[f():: 2])

n, m = map(int, input().split())
z = [[] for i in range(n+1)]

for i in range(n):
    a = input()
    for j in a:
        z[i].append(j)

def solve(n, m):
    for i in range(n):
        cnt = 0
        for j in range(1, m):
            if z[i][j] != z[i][j - 1]:
                cnt += 1
        if cnt > 2:
            return 1
        if cnt == 2 and z[i][0] == 'B':
            return 1

    for j in range(m):
        cnt = 0
        for i in range(1, n):
            if z[i][j] != z[i-1][j]:
                cnt += 1
        if cnt > 2:
            return 1
        if cnt == 2 and z[0][j] == 'B':
            return 1
    for i in range(n):
        for j in range(m):
            if z[i][j] == 'B':
                for x in range(i, n):
                    for y in range(m):
                        if z[x][y] == 'B':
                            if z[i][y]=='W' and z[x][j]=='W':
                                return 1
    return 0

print(['YES','NO'][solve(n, m)])

def f():

    n, m = list(map(int, input().split()))

    t = [input() for j in range(n)]



    p = [''.join(i) for i in zip(*t)]

    if h(p): return 1



    i = 0

    while i < n and not 'B' in t[i]: i += 1



    while i < n:

        a = t[i].find('B')

        if a < 0:

            i += 1

            break

        b = t[i].rfind('B')

        if 'W' in t[i][a: b + 1]: return 1



        for j in range(i + 1, n):

            if a > 0 and t[j][a - 1] == 'B' and t[j][b] == 'W': return 1

            if b < m - 1 and t[j][b + 1] == 'B' and t[j][a] == 'W': return 1

        i += 1



    while i < n:

        if 'B' in t[i]: return 1

        i += 1    



    return 0  

            

def h(t):

    i, n = 0, len(t)

    while i < n and not 'B' in t[i]: i += 1



    while i < n:

        a = t[i].find('B')

        if a < 0:

            i += 1

            break

        b = t[i].rfind('B')

        if 'W' in t[i][a: b + 1]: return 1

        i += 1



    while i < n:

        if 'B' in t[i]: return 1

        i += 1    



    return 0          

    

print('YNEOS'[f():: 2])



# Made By Mostafa_Khaled


n,m = list(map(int, input().split()))

row, col_sum, row_sum, black = [], [], [], []
for i in range(n):
    row.append(input())
    t = [0]
    for j in range(m):
        t += [t[j] + (row[i][j] == 'B')]
    row_sum += [t]

d = [[0,1], [1,0], [-1,0], [0,-1]]
for i in range(n):
    for j in range(m):
        if row[i][j] is 'W':
            continue
        w = 0
        for di in d:
            x = i + di[0]
            y = j + di[1]
            if x < 0 or y < 0 or x >= n or y >= m: 
                w += 1 ; continue
            if row[x][y] is 'W': 
                w += 1
        if w > 0: black.append((i,j))    
for i in range(m):
    t = [0]
    for j in range(n):
        t += [t[j] + (row[j][i] == 'B')]
    col_sum += [t]

def row_check(r, s, e):
    if s > e: e, s = s, e
    return row_sum[r][e + 1] - row_sum[r][s] == e - s + 1

def col_check(c, s, e):
    if s > e: e,s = s,e
    return col_sum[c][e + 1] - col_sum[c][s] == e - s + 1

res = True
for i in black:
    for j in black:
        if i <= j:
            continue
        a = row_check(i[0], i[1], j[1]) and col_check(j[1], i[0], j[0])
        b = row_check(j[0], i[1], j[1]) and col_check(i[1], i[0], j[0])
        res = res and (a or b)
print('YES' if res else 'NO')


#author: riyan

def solve(n, m):
    grid = []
    for i in range(n):
        grid.append(input().strip())
        cnt = 0
        for j in range(1, m):
            if grid[i][j] != grid[i][j - 1]:
                cnt += 1
        if (cnt > 2) or (cnt == 2 and grid[i][0] == 'B'):
            #print('row2 check, cnt = ', cnt)
            return False
        
    for j in range(m):
        cnt = 0
        for i in range(1, n):
            #print(i, j, grid[i][j])
            if grid[i][j] != grid[i - 1][j]:
                cnt += 1
        if (cnt > 2) or (cnt == 2 and grid[0][j] == 'B'):
            #print('col2 check, cnt = ', cnt)
            return False

    bps = []
    for i in range(n):
        for j in range(m):
            if grid[i][j] == 'B':
                bp1 = (i, j)
                for k in range(len(bps)):
                    bp2 = bps[k]
                    if not ( (grid[bp1[0]][bp2[1]] == 'B') or (grid[bp2[0]][bp1[1]] == 'B') ):
                        #print(bp1, bp2)
                        return False
                bps.append((i, j))
                
    return True

def __starting_point():
    n, m = list(map(int, input().strip().split()))
 
    ans = solve(n, m)

    if ans:
        print('YES')
    else:
        print('NO')


__starting_point()

#author: riyan

def solve(n, m):
    grid = []
    for i in range(n):
        grid.append(input().strip())
        cnt = 0
        for j in range(1, m):
            if grid[i][j] != grid[i][j - 1]:
                cnt += 1
        if (cnt > 2) or (cnt == 2 and grid[i][0] == 'B'):
            #print('row2 check, cnt = ', cnt)
            return False
        
    for j in range(m):
        cnt = 0
        for i in range(1, n):
            #print(i, j, grid[i][j])
            if grid[i][j] != grid[i - 1][j]:
                cnt += 1
        if (cnt > 2) or (cnt == 2 and grid[0][j] == 'B'):
            #print('col2 check, cnt = ', cnt)
            return False

    bps = []
    for i in range(n):
        for j in range(m):
            if grid[i][j] == 'B':
                bp1 = (i, j)
                for k in range(len(bps)):
                    bp2 = bps[k]
                    if not ( (grid[bp1[0]][bp2[1]] == 'B') or (grid[bp2[0]][bp1[1]] == 'B') ):
                        #print(bp1, bp2)
                        return False
                bps.append((i, j))
                
    return True

def __starting_point():
    n, m = list(map(int, input().strip().split()))
 
    ans = solve(n, m)

    if ans:
        print('YES')
    else:
        print('NO')


__starting_point()

n, m = list(map(int, input().split()))
c = [input() for _ in ' ' * n]
def sol(n, m):
    for i in range(n):
        count = 0
        for j in range(1, m):
            if c[i][j] != c[i][j - 1]:
                count += 1
        if count > 2:
            return False
        if count == 2 and c[i][0]=='B':
            return False

    for j in range(m):
        count = 0
        for i in range(1, n):
            if c[i][j] != c[i - 1][j]:
                count += 1
        if count > 2:
            return False
        if count == 2 and c[0][j]=='B':
            return False

    for i in range(n):
        for j in range(m):
            if c[i][j] == 'B':
                for x in range(i, n):
                    for y in range(m):
                        if c[x][y] == 'B':
                            if c[i][y] == 'W' and c[x][j] == 'W':
                                return False
    return True


print('NYOE S'[sol(n, m)::2])


n, m = list(map(int, input().split()))
c = [input() for _ in ' ' * n]
def sol(n, m):
    for i in range(n):
        count = 0
        for j in range(1, m):
            if c[i][j] != c[i][j - 1]:
                count += 1
        if count > 2:
            return False
        if count == 2 and c[i][0]=='B':
            return False

    for j in range(m):
        count = 0
        for i in range(1, n):
            if c[i][j] != c[i - 1][j]:
                count += 1
        if count > 2:
            return False
        if count == 2 and c[0][j]=='B':
            return False

    for i in range(n):
        for j in range(m):
            if c[i][j] == 'B':
                for x in range(i, n):
                    for y in range(m):
                        if c[x][y] == 'B':
                            if c[i][y] == 'W' and c[x][j] == 'W':
                                return False
    return True


print('NYOE S'[sol(n, m)::2])