Read problems statements in Mandarin Chinese, Russian and Vietnamese as well. 

You are given a structure of a water reservoir. The reservoir is a 2 dimensional structure of height N and width M. It can be thought of divided into M vertical sections, each of equal width. The reservoir height is of N blocks. Each block of the reservoir can contain either water, brick or air. We will denote the water by character 'W', brick by 'B' and air by 'A'.

You are given representation of reservoir at a fixed instant. Can you tell whether the current state of reservoir is stable, i.e. it will remain on the same state forever or not?

For example, let the reservoir be 

WW
BB

This is not stable, as the water in the block (1, 1) can overflow to the left, similarly water in block (1, 2) can overflow to the right too.

BWB
BBB

This is stable, as the water at block (1, 2) is entrapped between bricks from all sides.

AWA
BBB

This is not stable, as the water at block (1, 2) might overflow to the left or the right.

BAA
ABB

This is not stable, as there is a brick at block (1, 1) and air at (2, 1). The brick will go down and air will come up, so its not stable too.

BWAAB
BWBWB
BBBBB

This is not stable too, as there is water at block (1, 2) and air at (1, 3), and (1, 4). The water will move towards those blocks.

BBB
BAB
BBB

So brick at (1, 2) is loose and without support, and hence will fill the space beneath it, which is right now filled with air. That is, the brick will go down and replace the air

BBB
BWB
BBB

This is also unstable due to the same reason as the above.

Now, you are given description of reservoir. Can you tell whether the current state of reservoir is stable or not?

------ Input ------ 

The first line of input contains an integer T denoting number of test cases. Description of T test cases follows.
The first line of each test case, contains two integers N and M denoting the height and width of reservoir, respectively.
Each of the next N lines a string of length M. The j-th character in the i-th of the line denotes content at block (i, j). The characters can be 'B' denoting brick, 'A' denoting air, 'W' denoting water.

------ Output ------ 

For each test case, output a line containing "yes" or "no" (without quotes) corresponding to the situation whether the reservoir is stable or not?

------ Constraints ------ 

$1 ≤ T ≤ 50$
$1 ≤ N, M ≤ 1000$

Subtask #1: (15 points)
$There is no water in the reservoir.$

Subtask #2: (25 points)
$1 ≤ N, M ≤ 100$
Subtask #3: (60 points)
$original constraints$

----- Sample Input 1 ------ 
7
2 2
WW
BB
2 3
BWB
BBB
2 3
AWA
BBB
2 3
BAA
ABB
3 5
BWAAB
BWBWB
BBBBB
3 3
BBB
BAB
BBB
3 3
BBB
BWB
BBB
----- Sample Output 1 ------ 
no
yes
no
no
no
no
no
----- explanation 1 ------ 
All the examples are explained in the problem statement itself.

Difficulty: MEDIUM_HARD
Tags: Mathematics

Matrices

Implementation

Starter code:

Solutions:
def solution():
	(h, w) = map(int, input().split())
	R = [list('A' * (w + 2)) for i in range(h + 2)]
	R[-1] = list('B' * (w + 2))
	for r in range(1, h + 1):
		row = input()
		for c in range(1, w + 1):
			R[r][c] = row[c - 1]
	for r in range(1, h + 1):
		for c in range(1, w + 1):
			if R[r][c] == 'W':
				if R[r][c - 1] == 'A' or R[r][c + 1] == 'A' or R[r + 1][c] == 'A':
					print('no')
					return
			if R[r][c] == 'B':
				if R[r + 1][c] == 'A' or R[r + 1][c] == 'W':
					print('no')
					return
	print('yes')
p = int(input())
while p > 0:
	p = p - 1
	solution()


for t in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		s = input()
		a.append(s)
	f = 1
	for i in range(n):
		for j in range(m):
			if i + 1 < n and a[i][j] == 'B' and (a[i + 1][j] != 'B'):
				f = 0
				break
			if a[i][j] == 'W':
				if j == 0 or j == m - 1 or i == n - 1:
					f = 0
					break
				if a[i][j - 1] == 'A' or a[i][j + 1] == 'A' or a[i + 1][j] == 'A':
					f = 0
					break
		if f == 0:
			break
	if f == 1:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	l = []
	for i in range(n):
		l.append(input())
	ans = True
	for i in range(n):
		for j in range(m):
			if l[i][j] == 'W':
				if j == 0 or j == m - 1 or i == n - 1:
					ans = False
				elif l[i][j - 1] == 'A' or l[i][j + 1] == 'A' or l[i + 1][j] == 'A':
					ans = False
			if l[i][j] == 'B':
				if i < n - 1:
					if l[i + 1][j] != 'B':
						ans = False
	if ans:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	(N, M) = input().split(' ')
	A = []
	for i in range(int(N)):
		stri = input()
		B = []
		B.extend(stri)
		A.append(B)
	A.reverse()
	flag = 1
	for i in range(len(A)):
		for j in range(len(A[i])):
			if A[i][j] == 'A':
				if i != len(A) - 1:
					if j != len(A[i]) - 1:
						if A[i][j + 1] == 'W':
							flag = 0
							break
					if A[i + 1][j] == 'B' or A[i + 1][j] == 'W':
						flag = 0
						break
				else:
					if j == 0 or j == len(A[i]) - 1:
						continue
					if A[i][j + 1] == 'W':
						flag = 0
						break
			if A[i][j] == 'W':
				if i != len(A) - 1:
					if j == 0 or j == len(A[i]) - 1:
						flag = 0
						break
					if A[i][j + 1] == 'A':
						flag = 0
						break
					if A[i + 1][j] == 'B':
						flag = 0
						break
				else:
					if j == 0 or j == len(A[i]) - 1:
						flag = 0
						break
					if A[i][j + 1] == 'A':
						flag = 0
						break
		if flag == 0:
			break
	print('yes') if flag == 1 else print('no')


for T in range(int(input())):
	(N, M) = map(int, input().split())
	layers = list()
	stable = 'yes'
	for n in range(N):
		layer = input()
		if 'WA' in layer or 'AW' in layer or layer[0] == 'W' or (layer[-1] == 'W'):
			stable = 'no'
		layers.append(list(layer))
	layers = list(map(''.join, list(zip(*layers))))
	for i in range(M):
		if 'BA' in layers[i] or 'BW' in layers[i]:
			stable = 'no'
	print(stable)


t = int(input())

def fun():
	for j in range(n):
		for k in range(m):
			if l[j][k] is 'W':
				if k is 0 or k is m - 1 or (j < n - 1 and l[j + 1][k] is 'A') or (k < m - 1 and l[j][k + 1] is 'A'):
					print('no')
					return
			elif l[j][k] is 'A':
				if k < m - 1 and l[j][k + 1] is 'W':
					print('no')
					return
			elif l[j][k] is 'B':
				if j < n - 1 and (l[j + 1][k] is 'A' or l[j + 1][k] is 'W'):
					print('no')
					return
	print('yes')
for i in range(t):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	l = [['E'] * m for i in range(n)]
	for j in range(n):
		s = input()
		for k in range(m):
			l[j][k] = s[k]
	fun()


t = int(input())
for i in range(t):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	l = [['E'] * m for i in range(n)]
	for j in range(n):
		s = input()
		for k in range(m):
			l[j][k] = s[k]
	flag = True
	for j in range(n):
		if flag is False:
			break
		for k in range(m):
			if l[j][k] is 'W':
				if k is 0 or k is m - 1 or (j < n - 1 and l[j + 1][k] is 'A') or (k < m - 1 and l[j][k + 1] is 'A') or (k > 0 and l[j][k - 1] is 'A') or (j > 0 and l[j - 1][k] is 'B'):
					flag = False
					break
			elif l[j][k] is 'A':
				if k < m - 1 and l[j][k + 1] is 'W' or (k > 0 and l[j][k - 1] is 'W') or (j > 0 and (l[j - 1][k] is 'W' or l[j - 1][k] is 'B')):
					flag = False
					break
	if flag:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	flag = True
	for i in range(n):
		a.append(list(input()))
	for i in range(n):
		if a[i][0] == 'W' or a[i][m - 1] == 'W':
			print('no')
			flag = False
			break
	else:
		for j in range(m):
			if a[n - 1][j] == 'W':
				flag = False
				print('no')
				break
	if flag:
		for i in range(n - 1):
			if flag:
				for j in range(m):
					if a[i][j] == 'W':
						if a[i][j + 1] == 'A' or a[i][j - 1] == 'A' or a[i + 1][j] == 'A':
							print('no')
							flag = False
							break
					elif a[i][j] == 'B' and (a[i + 1][j] == 'A' or a[i + 1][j] == 'W'):
						print('no')
						flag = False
						break
	if flag:
		print('yes')


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	rep = []
	pos = 1
	for i in range(n):
		s = input()
		if s[0] == 'W' or s[len(s) - 1] == 'W':
			pos = 0
		rep.append(s)
		for j in range(m):
			if rep[i][j] == 'W':
				if i != 0:
					if rep[i - 1][j] == 'B':
						pos = 0
				if j != 0:
					if rep[i][j - 1] == 'A':
						pos = 0
			if rep[i][j] == 'A':
				if i != 0:
					if rep[i - 1][j] == 'B' or rep[i - 1][j] == 'W':
						pos = 0
				if j != 0:
					if rep[i][j - 1] == 'W':
						pos = 0
	if pos == 1:
		print('yes')
	else:
		print('no')


def water(i, j, N, M, my_list=[]):
	if j == 0 or j == M - 1:
		return False
	if i < N - 1:
		if my_list[i + 1][j] == 'B':
			return False
	if my_list[i][j + 1] == 'A':
		return False
	return True

def air(i, j, N, M, my_list=[]):
	if i < N - 1:
		if my_list[i + 1][j] == 'B' or my_list[i + 1][j] == 'W':
			return False
	if j < M - 1:
		if my_list[i][j + 1] == 'W':
			return False
	return True
T = int(input())
for i in range(T):
	(N, M) = map(int, input().strip().split(' '))
	L = []
	for j in range(N):
		d = input().strip().split(' ')
		for k in d:
			L.insert(0, k)
	flag = True
	for j in range(N):
		for k in range(M):
			if L[j][k] == 'W':
				flag = water(j, k, N, M, L)
			if L[j][k] == 'A':
				flag = air(j, k, N, M, L)
			if flag == False:
				break
		if flag == False:
			break
	if flag == True:
		print('yes')
	else:
		print('no')


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	a = list()
	a.append('A' * m)
	for i in range(n):
		a.append(input())
	x = 1
	while x < n + 1:
		for j in range(m):
			z = a[x][j]
			if z == 'W':
				if j == 0 or j == m - 1 or a[x][j - 1] == 'A' or (a[x][j + 1] == 'A') or (a[x - 1][j] == 'B'):
					x = n + 1
					break
			elif z == 'A' and a[x - 1][j] != 'A':
				x = n + 1
				break
		x += 1
	if x == n + 2:
		print('no')
	else:
		print('yes')


T = int(input())
while T > 0:
	T = T - 1
	D = input()
	M = int(D.split(' ')[0])
	N = int(D.split(' ')[1])
	num = M
	res = []
	(i, j, flag) = (0, 0, 0)
	while num > 0:
		num = num - 1
		res.append(input())
	for i in range(M):
		if res[i].startswith('W') or res[i].endswith('W'):
			flag = 1
			break
		elif res[i].count('AW') > 0 or res[i].count('WA') > 0:
			flag = 1
			break
		elif i < M - 1:
			for j in range(N):
				if res[i][j] == 'B' and res[i + 1][j] != 'B':
					flag = 1
					break
				if res[i][j] == 'W' and res[i + 1][j] == 'A':
					flag = 1
					break
		if flag == 1:
			break
	if flag == 1:
		print('no')
	else:
		print('yes')


def validate(val_hor, m, n):
	val_ver = list(zip(*val_hor))
	for i in val_ver:
		j = 1
		while j < n:
			if i[j - 1] > i[j]:
				return 'no'
			j += 1
	for i in val_hor:
		if i[0] == 2 or i[m - 1] == 2:
			return 'no'
		j = 1
		while j < m - 1:
			if i[j] == 2 and (i[j - 1] == 1 or i[j + 1] == 1):
				return 'no'
			j += 1
	return 'yes'

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = [int(i) for i in input().split()]
		val_hor = [[1 if i == 'A' else 2 if i == 'W' else 3 for i in input()] for _ in range(n)]
		print(validate(val_hor, m, n))
main()


reservoirs = int(input().strip())
for i in range(0, reservoirs):
	list1 = []
	(rows, columns) = map(int, input().strip().split())
	for j in range(0, rows):
		list1.append([x for x in input().strip()])
	list2 = list(zip(*list1))
	flag = 0
	for i in range(0, columns):
		element1 = ''.join(list2[i])
		if 'BA' in element1 or 'WA' in element1 or 'BW' in element1:
			print('no')
			flag = 1
			break
	if flag != 1:
		for i in range(0, rows):
			element = ''.join(list1[i])
			if 'AW' in element or 'WA' in element or element[0] == 'W' or (element[columns - 1] == 'W'):
				print('no')
				flag = 1
				break
	if flag != 1:
		print('yes')


import sys

def main():
	for _ in range(int(sys.stdin.readline())):
		(N, M) = map(int, sys.stdin.readline().strip().split())
		resv = []
		resv.append('A' * (M + 2))
		for i in range(N):
			resv.append('A' + sys.stdin.readline().strip() + 'A')
		resv.append('B' * (M + 2))
		isStable = True
		for i in range(1, N + 1):
			for j in range(1, M + 1):
				tile = resv[i][j]
				if tile == 'B':
					if resv[i + 1][j] == 'A' or resv[i + 1][j] == 'W':
						isStable = False
						break
				elif tile == 'W':
					if resv[i][j - 1] == 'A' or resv[i][j + 1] == 'A' or resv[i + 1][j] == 'A':
						isStable = False
						break
			if not isStable:
				break
		if isStable:
			print('yes')
		else:
			print('no')
main()


def built(wall, N, M):
	for i in range(N):
		if wall[i][0] == 'W':
			return 'no'
		if wall[i][M - 1] == 'W':
			return 'no'
	for i in range(N):
		for j in range(0, M):
			if wall[i][j] == 'B':
				if i + 1 <= N - 1:
					if wall[i + 1][j] != 'B':
						return 'no'
			if wall[i][j] == 'W':
				if i + 1 <= N - 1:
					if wall[i + 1][j] == 'A':
						return 'no'
				if j - 1 >= 0 and j + 1 <= M - 1:
					if wall[i][j - 1] == 'A' or wall[i][j + 1] == 'A':
						return 'no'
	return 'yes'

def main(args):
	T = int(input())
	while T > 0:
		T -= 1
		(N, M) = list(map(int, input().split()))
		wall = []
		for i in range(N):
			wall.append(input())
		print(built(wall, N, M))
import sys
sys.exit(main(sys.argv))


t = int(input())
i = 0
while i < t:
	(n, m) = input().strip().split(' ')
	n = int(n)
	m = int(m)
	s = ''
	j = 0
	flag = 0
	while j < n:
		s1 = input()
		if flag == 0:
			if j == 0 and s1.count('W') == 0:
				s += s1
			elif j == 0 and s1.count('W') > 0:
				s += s1
				if s1[0] == 'W' or s1[m - 1] == 'W':
					flag = 1
				else:
					for k in range(1, m - 1):
						if s1[k] == 'W':
							if s1[k - 1] == 'A' or s1[k + 1] == 'A':
								flag = 1
								break
			elif j > 0:
				if s1[0] == 'W' or s1[m - 1] == 'W':
					flag = 1
				else:
					for k in range(m):
						if s[k] == 'B' and (s1[k] == 'W' or s1[k] == 'A'):
							flag = 1
							break
						if s[k] == 'W' and s1[k] == 'A':
							flag = 1
							break
						if s1[k] == 'W':
							if s1[k - 1] == 'A' or s1[k + 1] == 'A':
								flag = 1
								break
		j += 1
	if flag == 0:
		print('yes')
	else:
		print('no')
	i += 1


t = int(input())
for t0 in range(t):
	s = [int(i) for i in input().strip().split()]
	(n, m) = (s[0], s[1])
	wall = []
	for i in range(n):
		s = list(input().strip())
		wall.append(s)
	fl = 0
	for i in range(n):
		for j in range(m):
			if wall[i][j] == 'B':
				if i < n - 1 and wall[i + 1][j] != 'B':
					fl = 1
					break
			elif wall[i][j] == 'A':
				if j < m - 1 and wall[i][j + 1] == 'W':
					fl = 1
					break
				if j > 0 and wall[i][j - 1] == 'W':
					fl = 1
					break
			else:
				if j == 0 or j == m - 1 or i == n - 1:
					fl = 1
					break
				if j < m - 1 and wall[i][j + 1] == 'A':
					fl = 1
					break
				if j > 0 and wall[i][j - 1] == 'A':
					fl = 1
					break
				if i < n - 1 and wall[i + 1][j] == 'A':
					fl = 1
					break
		if fl == 1:
			break
	if fl == 0:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	(n, m) = (int(x) for x in input().split())
	r = [input() for i in range(n)]

	def stable():
		for i in reversed(range(n)):
			for j in range(m):
				if r[i][j] == 'B':
					if i < n - 1 and (r[i + 1][j] == 'W' or r[i + 1][j] == 'A'):
						return False
				elif r[i][j] == 'W':
					if i == n - 1 or j == 0 or j == m - 1 or (i < n - 1 and r[i + 1][j] == 'A') or (j > 0 and r[i][j - 1] == 'A') or (j < m - 1 and r[i][j + 1] == 'A'):
						return False
		return True
	print('yes' if stable() else 'no')


from itertools import combinations

def isStable(mat, i, j):
	type = mat[i][j]
	if type == 'B' and i < n - 1 and (mat[i + 1][j] == 'A'):
		return False
	if type == 'W':
		if j == 0 or j == m - 1 or i == n - 1 or (mat[i + 1][j] == 'A') or (mat[i][j - 1] == 'A') or (mat[i][j + 1] == 'A'):
			return False
	return True

def checkStability(mat, n, m):
	for i in range(n):
		for j in range(m):
			if not isStable(blocks, i, j):
				yield 'no'
for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	blocks = []
	for i in range(n):
		blocks.append(input())
	result = checkStability(blocks, n, m)
	try:
		print(next(result))
	except StopIteration:
		print('yes')


T = int(input().strip())
for test in range(0, T):
	stable = 1
	figure = []
	(N, M) = [int(temp) for temp in input().strip().split(' ')]
	for block in range(0, N):
		tempString = input().strip()
		figure.insert(block, tempString)
	for block in range(0, N):
		for section in range(0, M):
			if figure[block][section] == 'W':
				if section > 0:
					if figure[block][section - 1] == 'A':
						stable = 0
						break
				else:
					stable = 0
					break
				if section < M - 1:
					if figure[block][section + 1] == 'A':
						stable = 0
						break
				else:
					stable = 0
					break
				if block < N - 1:
					if figure[block + 1][section] == 'A':
						stable = 0
						break
				else:
					stable = 0
					break
			if figure[block][section] == 'B':
				if block < N - 1:
					if figure[block + 1][section] != 'B':
						stable = 0
						break
		if stable == 0:
			break
	if stable:
		print('yes')
	else:
		print('no')


def getPos(x, y):
	if x >= n or x < 0 or y >= m or (y < 0):
		return '.'
	return s[x][y]

def calcRes():
	for x in range(n):
		for y in range(m):
			if s[x][y] == 'B':
				c = getPos(x + 1, y)
				if c != '.' and c != 'B':
					return False
			elif s[x][y] == 'W':
				if getPos(x + 1, y) == 'A' or getPos(x + 1, y) == '.' or (getPos(x, y - 1) != 'B' and getPos(x, y - 1) != 'W') or (getPos(x, y + 1) != 'B' and getPos(x, y + 1) != 'W'):
					return False
	return True
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = [input() for x in range(n)]
	print(['no', 'yes'][calcRes()])


for i in range(int(input())):
	(n, m) = map(int, input().split())
	ip = []
	b = 0
	for j in range(n):
		ip.append(str(input()))
	for j in range(n):
		for k in range(m):
			if ip[j][k] == 'A':
				if j == 0 and k == 0:
					try:
						if ip[j][k + 1] == 'W':
							b = 1
							break
					except:
						pass
				elif j == 0 and k != 0:
					if ip[j][k - 1] == 'W':
						b = 1
						break
					try:
						if ip[j][k + 1] == 'W':
							b = 1
							break
					except:
						pass
				elif j != 0 and k == 0:
					if ip[j - 1][k] != 'A':
						b = 1
						break
				else:
					if ip[j - 1][k] != 'A' or ip[j][k - 1] == 'W':
						b = 1
						break
					try:
						if ip[j][k + 1] == 'W':
							b = 1
							break
					except:
						pass
			elif ip[j][k] == 'B':
				try:
					if ip[j + 1][k] != 'B':
						b = 1
						break
				except:
					pass
			elif k == 0 or k == m - 1 or j == n - 1:
				b = 1
				break
		if b == 1:
			break
	if b == 1:
		print('no')
	else:
		print('yes')


def main():
	from sys import stdin, stdout
	for _ in range(int(stdin.readline())):
		(n, m) = map(int, stdin.readline().split())
		flag = True
		R = []
		for i in range(n):
			R.append(stdin.readline().strip())
		for i in range(n):
			for j in range(m):
				if R[i][j] == 'A':
					if i > 0:
						if R[i - 1][j] != 'A':
							flag = False
							break
				elif R[i][j] == 'W':
					if j == 0 or j == m - 1:
						flag = False
						break
					if R[i][j + 1] == 'A' or R[i][j - 1] == 'A':
						flag = False
						break
					if i > 0:
						if R[i - 1][j] == 'B':
							flag = False
							break
				elif i < n - 1:
					if R[i + 1][j] != 'B':
						flag = False
						break
			if not flag:
				break
		if flag:
			stdout.write('yes\n')
		else:
			stdout.write('no\n')
main()


t = int(input())
for i in range(t):
	(n, m) = [int(x) for x in input().split()]
	l = []
	for j in range(n):
		mm = input()
		l.append(mm)
	a = 0
	c = 0
	while a < n:
		b = 0
		while b < m:
			if l[a][b] == 'W':
				if b == 0 or b == m - 1 or a == n - 1:
					print('no')
					c += 1
					break
				elif l[a][b + 1] == 'A' or l[a][b - 1] == 'A':
					print('no')
					c += 1
					break
				elif a < n - 1 and l[a + 1][b] == 'A':
					print('no')
					c += 1
					break
				elif a > 0 and l[a - 1][b] == 'B':
					print('no')
					c += 1
					break
			elif l[a][b] == 'A':
				if a > 0 and (l[a - 1][b] == 'B' or l[a - 1][b] == 'W'):
					print('no')
					c += 1
					break
			b += 1
		a += 1
		if c != 0:
			break
	if c == 0:
		print('yes')


t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(list(input()))
	flag = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == 'W':
				if j == 0 or j == m - 1 or i == n - 1:
					print('no')
					flag = 1
					break
				elif a[i + 1][j] == 'A' or a[i][j + 1] == 'A' or a[i][j - 1] == 'A':
					print('no')
					flag = 1
					break
			elif a[i][j] == 'B':
				if i == n - 1:
					pass
				elif a[i + 1][j] != 'B':
					print('no')
					flag = 1
					break
		if flag == 1:
			break
	if flag == 0:
		print('yes')


t = int(input())
ans = []
for i in range(t):
	(n, m) = list(map(int, input().split()))
	res = []
	for j in range(n):
		ip = input()
		res.append(ip)
	stable = True
	for j in range(n):
		for k in range(m):
			ele = res[j][k]
			if ele == 'B':
				if j != n - 1 and res[j + 1][k] != 'B':
					stable = False
					break
			elif ele == 'W':
				if k == 0 or k == m - 1 or j == n - 1:
					stable = False
					break
				elif res[j][k - 1] == 'A' or res[j][k + 1] == 'A' or res[j + 1][k] == 'A':
					stable = False
					break
	if stable:
		ans.append('yes')
	else:
		ans.append('no')
for x in ans:
	print(x)


def check(string, M):
	if string[0] == 'W' or string[M - 1] == 'W':
		return False
	for i in range(M):
		if i > 0 and string[i - 1] == 'W' and (string[i] == 'A'):
			return False
		if i < M - 1 and string[i + 1] == 'W' and (string[i] == 'A'):
			return False
	return True

def checkTwo(stringA, stringB, M):
	for i in range(M):
		if stringA[i] == 'B' and stringB[i] == 'A':
			return False
		if stringA[i] == 'W' and stringB[i] == 'A':
			return False
	return True
T = int(input())
while T > 0:
	(N, M) = input().split(' ')
	N = int(N)
	M = int(M)
	ans = 'yes'
	string = []
	for i in range(N):
		temp = input()
		if i == N - 1 and 'W' in temp:
			continue
		elif check(temp, M) == True:
			string.append(temp)
	if len(string) < N:
		ans = 'no'
	else:
		for i in range(1, len(string)):
			stringupper = string[i - 1]
			stringlower = string[i]
			if checkTwo(stringupper, stringlower, M) == False:
				ans = 'no'
				break
	print(ans)
	T = T - 1


t = int(input())
for _ in range(0, t):
	(n, m) = map(int, input().split())
	a = []
	for i in range(0, n):
		a.append(input())
	flag = True
	flag1 = True
	for i in range(0, n - 1):
		for j in range(0, m):
			if a[i][j] == 'B' and a[i + 1][j] != 'B':
				print('no')
				flag = False
				break
		if flag == False:
			break
	if flag:
		for i in range(0, n):
			(bfpos, blpos, wfpos, wlpos) = (10000, 0, -1, -1)
			bFlag = False
			wFlag = False
			for j in range(1, m - 1):
				if a[i][j] == 'W' and (a[i][j - 1] == 'A' or a[i][j + 1] == 'A' or a[i + 1][j] == 'A'):
					print('no')
					flag1 = False
					break
			if flag1 == False:
				break
			if 'B' in a[i]:
				bfpos = a[i].index('B')
			if 'B' in a[i]:
				blpos = a[i].rindex('B')
			if 'W' in a[i]:
				wfpos = a[i].index('W')
			if 'W' in a[i]:
				wlpos = a[i].rindex('W')
			if (bfpos > wfpos or blpos < wlpos) and wfpos > -1:
				print('no')
				flag1 = False
				break
		if flag1:
			print('yes')


def test(data, n, m):
	if any(map(lambda x: 'AW' in x or 'WA' in x, data)):
		return False
	for i in range(n - 1):
		if any(map(lambda k: data[i][k] == 'B' and data[i + 1][k] != 'B', range(m))):
			return False
	return True
T = int(input())
for t in range(T):
	(n, m) = map(int, input().split())
	data = ['A' + input() + 'A' for i in range(n)]
	if test(data, n, m + 2):
		print('yes')
	else:
		print('no')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	ho = []
	for i in range(n):
		ho.append(input())
	ve = ['' for x in range(m)]
	for i in range(n):
		for j in range(m):
			ve[j] += ho[i][j]
	flag = 1
	for i in range(n):
		if 'WA' in ho[i] or 'AW' in ho[i]:
			flag = 0
			break
	for i in range(m):
		if 'BA' in ve[i] or 'WA' in ve[i] or 'BW' in ve[i]:
			flag = 0
			break
	if 'W' in ve[0] or 'W' in ve[m - 1]:
		flag = 0
	if flag:
		print('yes')
	else:
		print('no')


for t in range(int(input())):
	(N, M) = map(int, input().split())
	G = []
	for i in range(N):
		G.append(input())
	f = True
	for i in range(0, N - 1):
		if G[i][0] == 'W' or G[i][-1] == 'W':
			f = False
			break
	if N > 1:
		for j in range(0, M):
			if G[-1][j] == 'A' and G[-2][j] != 'A':
				f = False
				break
	for j in range(0, M):
		if G[-1][j] == 'W':
			f = False
			break
	for i in range(0, N - 1):
		for j in range(1, M - 1):
			if G[i][j] == 'B':
				if G[i + 1][j] != 'B':
					f = False
					break
			elif G[i][j] == 'W':
				if G[i + 1][j] == 'A' or G[i][j - 1] == 'A' or G[i][j + 1] == 'A':
					f = False
					break
		if not f:
			break
	if f:
		print('yes')
	else:
		print('no')


import sys
test = int(input(''))
for i in range(0, test):
	(h, w) = [int(n) for n in input().split()]
	a = ''
	for j in range(0, w):
		a = a + 'A'
	res = ['A' + a + 'A']
	for j in range(0, h):
		res.append('A' + input() + 'A')
	a = ''
	for j in range(0, w):
		a = a + 'B'
	res.append('B' + a + 'B')
	flag = 1
	for j in range(1, h + 1):
		for k in range(1, w + 2):
			if res[j][k] == 'B':
				if res[j + 1][k] == 'W' or res[j + 1][k] == 'A':
					flag = 0
					print('no')
					break
			elif res[j][k] == 'W':
				if res[j][k - 1] == 'A' or res[j][k + 1] == 'A' or res[j + 1][k] == 'A':
					flag = 0
					print('no')
					break
		if flag == 0:
			break
	if flag == 1:
		print('yes')


for k in range(int(input())):
	(n, m) = map(int, input().split())
	a = []
	flag = True
	for i in range(n):
		a.append(input())
	if a[0][0] == 'W' or a[n - 1][-1] == 'W':
		flag = False
	if a[0][0] == 'B':
		try:
			if a[1][0] != 'B':
				flag = False
		except:
			pass
	if a[0][-1] == 'B':
		try:
			if a[1][-1] != 'B':
				flag = False
		except:
			pass
	if True:
		x = 0
		for y in range(1, m - 1):
			if a[x][y] == 'W':
				try:
					if a[x][y - 1] == 'A' or a[x][y + 1] == 'A' or a[x + 1][y] == 'A':
						flag = False
						break
				except:
					if a[x][y - 1] == 'A' or a[x][y + 1] == 'A':
						flag = False
						break
			if a[x][y] == 'B':
				try:
					if not a[x + 1][y] == 'B':
						flag = False
						break
				except:
					pass
		for x in range(1, n - 1):
			if a[x][0] == 'W' or a[x][-1] == 'W':
				flag = False
				break
			for y in range(m):
				if a[x][y] == 'W':
					try:
						if a[x][y - 1] == 'A' or a[x][y + 1] == 'A' or a[x + 1][y] == 'A':
							flag = False
							break
					except:
						if a[x][y - 1] == 'A' or a[x][y + 1] == 'A':
							flag = False
							break
				if a[x][y] == 'B':
					try:
						if not a[x + 1][y] == 'B':
							flag = False
							break
					except:
						pass
			if not flag:
				break
		x = n - 1
		if a[x][0] == 'W' or a[x][-1] == 'W':
			flag = False
		else:
			for y in range(1, m - 1):
				if a[x][y] == 'W':
					if a[x][y - 1] == 'A' or a[x][y + 1] == 'A':
						flag = False
						break
	if flag:
		print('yes')
	else:
		print('no')


t = int(input())
for i in range(t):
	(d, c) = list(map(int, input().split()))
	a = [['X'] * (c + 2) for j in range(d + 2)]
	for j in range(d):
		a[j + 1][1:c + 1] = list(input())
	b = 0
	check = 0
	for x in range(1, d + 1):
		for y in range(1, c + 1):
			if a[x][y] is 'B':
				continue
			elif a[x][y] is 'W':
				if a[x + 1][y] == 'X' or a[x][y - 1] == 'X' or a[x][y + 1] == 'X':
					b = 1
					break
				elif a[x + 1][y] == 'A' or a[x][y - 1] == 'A' or a[x][y + 1] == 'A':
					b = 1
					break
				else:
					continue
			elif a[x - 1][y] == 'W' or a[x][y - 1] == 'W' or a[x][y + 1] == 'W':
				b = 1
				break
			elif a[x - 1][y] == 'B':
				b = 1
				break
			else:
				continue
		if b == 1:
			check = 1
			break
	print('no' if check == 1 else 'yes')


import sys
t = int(sys.stdin.readline())

def is_stable(n, m, d):
	for i in range(1, m - 1):
		if d[0][i] == 'W' and (d[0][i - 1] == 'A' or d[0][i + 1] == 'A'):
			print('no')
			return
	(lw, rw) = (d[0][0], d[0][-1])
	for i in range(n):
		if d[i][0] == 'W' or d[i][-1] == 'W':
			print('no')
			return
		if d[i][0] == 'B' and lw != 'B':
			print('no')
			return
		if d[i][-1] == 'B' and rw != 'B':
			print('no')
			return
		lw = d[i][0]
		rw = d[i][-1]
	for i in range(1, n):
		for j in range(1, m - 1):
			if d[i][j] == 'B' and d[i - 1][j] != 'B':
				print('no')
				return
			elif d[i][j] == 'W':
				if d[i - 1][j] == 'A':
					print('no')
					return
				elif d[i][j - 1] == 'A':
					print('no')
					return
				elif d[i][j + 1] == 'A':
					print('no')
					return
	print('yes')
while t:
	(n, m) = [int(x) for x in sys.stdin.readline().split()]
	d = []
	d = [sys.stdin.readline().strip() for i in range(n)]
	is_stable(n, m, d[::-1])
	t = t - 1


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = []
	check = False
	for i in range(n):
		s = input()
		a.append(s)
	for k in range(n):
		for j in range(m):
			if a[k][j] == 'W':
				if j == 0 or j == m - 1:
					check = True
					break
				elif a[k][j - 1] == 'A' or a[k][j + 1] == 'A':
					check = True
					break
				elif k != n - 1 and a[k + 1][j] == 'A':
					check = True
					break
				elif k != 0 and a[k - 1][j] == 'B':
					check = True
					break
			elif a[k][j] == 'A':
				if k != 0 and a[k - 1][j] == 'B':
					check = True
					break
		if check:
			break
	if check:
		print('no')
	else:
		print('yes')


import re
t = int(input())
for a0 in range(t):
	(N, M) = input().strip().split(' ')
	(N, M) = [int(N), int(M)]
	wall = []
	s = 0
	for a in range(N):
		A = input()
		wall.append(list(A))
	pattern1 = 'WA'
	pattern2 = 'AW'
	for a1 in range(N):
		if re.search(pattern1, ''.join(wall[a1])):
			s = 1
			break
		elif re.search(pattern2, ''.join(wall[a1])):
			s = 1
			break
		elif wall[a1][0] == 'W' or wall[a1][M - 1] == 'W':
			s = 1
			break
	ptrn1 = '[BW][A]'
	ptrn2 = 'BW'
	m = 0
	while s == 0 and m < M:
		col = ''
		for n in range(N):
			col = col + wall[n][m]
		if re.search(ptrn1, col):
			s = 1
			break
		elif re.search(ptrn2, col):
			s = 1
			break
		else:
			m += 1
	if s == 1:
		print('no')
	else:
		print('yes')


import re
reg = re.compile('(WA|AW)')

def fun(a):
	prev = ' ' * len(a[0])
	for wid in a:
		if wid[0] == 'W' or wid[-1] == 'W':
			return False
		if reg.search(wid):
			return False
		for (j, k) in zip(prev, wid):
			if [j, k] in [['B', 'W'], ['B', 'A']]:
				return False
		prev = wid
	return True
t = int(input())
for _ in range(t):
	(h, w) = map(int, input().split())
	a = []
	for _ in range(h):
		a.append(input())
	if fun(a):
		print('yes')
	else:
		print('no')


for t in range(int(input())):
	(n, m) = map(int, input().split())
	r = ['X' * (m + 2)]
	for i in range(n):
		r += ['X' + input() + 'X']
	r += ['X' * (m + 2)]
	flag = False
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if r[i][j] == 'W':
				if 'A' in [r[i][j + 1], r[i][j - 1], r[i + 1][j]]:
					flag = True
				elif 'X' in [r[i][j + 1], r[i][j - 1], r[i + 1][j]]:
					flag = True
			elif r[i][j] == 'B':
				if r[i + 1][j] == 'A':
					flag = True
			if flag:
				break
		if flag:
			break
	if flag:
		print('no')
	else:
		print('yes')


t = int(input())
for i in range(t):
	a = []
	(n, m) = map(int, input().split())
	for j in range(n):
		b = input()
		a.append(b)
	tag = 0
	for j in range(n):
		if tag == 1:
			break
		for k in range(m):
			if a[j][k] == 'B':
				try:
					if a[j + 1][k] == 'A':
						tag = 1
						break
				except:
					tag = 0
			elif a[j][k] == 'A':
				try:
					if a[j][k - 1] == 'W':
						tag = 1
						break
				except:
					tag = 0
				try:
					if a[j][k + 1] == 'W':
						tag = 1
						break
				except:
					tag = 0
			else:
				if k == 0 or k == m - 1 or j == n - 1:
					tag = 1
					break
				try:
					if a[j][k + 1] == 'A':
						tag = 1
						break
				except:
					tag = 0
				try:
					if a[j][k - 1] == 'A':
						tag = 1
						break
				except:
					tag = 0
				try:
					if a[j + 1][k] == 'A':
						tag = 1
						break
				except:
					tag = 0
	if tag == 0:
		print('yes')
	else:
		print('no')


for _ in range(int(input())):
	inp = []
	(n, m) = map(int, input().split())
	for i in range(n):
		inp += [list(input())]
	flag = True
	inp += [[0 for x in range(m)]]
	for i in range(n):
		for j in range(m):
			if inp[i][j] == 'B':
				if inp[i + 1][j] == 'A' or inp[i + 1][j] == 'W':
					flag = False
			elif inp[i][j] == 'W':
				if j == 0 or j == m - 1:
					flag = False
				else:
					if inp[i + 1][j] == 'A':
						flag = False
					if inp[i][j - 1] == 'A' or inp[i][j + 1] == 'A':
						flag = False
		if not flag:
			break
	if flag:
		print('yes')
	else:
		print('no')


def solve(N, M, l):
	for col in range(M):
		ch = l[N - 1][col]
		if ch not in ['B', 'A']:
			return 'no'
	for row in reversed(range(N)):
		for col in range(M):
			ch = l[row][col]
			if ch == 'B':
				None
			elif ch in ['A']:
				if row > 0 and l[row - 1][col] in ['B', 'W']:
					return 'no'
				if col < M - 1 and l[row][col + 1] == 'W':
					return 'no'
			elif ch in ['W']:
				if row > 0 and l[row - 1][col] in ['B']:
					return 'no'
				if col < M - 1 and l[row][col + 1] in ['A']:
					return 'no'
				if col == 0:
					return 'no'
	return 'yes'
T = int(input())
while T > 0:
	(N, M) = [int(x) for x in input().split()]
	l = []
	for i in range(N):
		l.append(input())
	print(solve(N, M, l))
	T -= 1


for _ in range(int(input())):
	(r, c) = map(int, input().split())
	arr = [input() for _ in range(r)]
	flag = False
	if arr[r - 1][0] == 'W' or arr[r - 1][c - 1] == 'W':
		print('no')
		continue
	for i in range(c - 1):
		if arr[r - 1][i] == 'W' and arr[r - 1][i + 1] == 'A' or (arr[r - 1][i] == 'A' and arr[r - 1][i + 1] == 'W'):
			print('no')
			flag = True
			break
	if flag:
		continue
	for i in range(0, r):
		if arr[i][0] == 'W' or arr[i][c - 1] == 'W':
			print('no')
			break
	else:
		for i in range(r - 2, -1, -1):
			for j in range(c):
				if arr[i][j] == 'B':
					if arr[i + 1][j] != 'B':
						print('no')
						flag = True
						break
				elif arr[i][j] == 'W':
					if arr[i + 1][j] == 'A':
						print('no')
						flag = True
						break
					if j != c - 1 and arr[i][j + 1] == 'A':
						print('no')
						flag = True
						break
				elif arr[i][j] == 'A':
					if j != c - 1 and arr[i][j + 1] == 'W':
						print('no')
						flag = True
						break
			if flag:
				break
		else:
			print('yes')


t = int(input())
for i in range(t):
	(n, m) = tuple([int(x) for x in input().split()])
	l = []
	for j in range(n):
		l.append(list(input()))
	answer = 'yes'
	for j in range(n):
		for k in range(m):
			if l[j][k] == 'W':
				if k == 0 or k == m - 1 or j == n - 1:
					answer = 'no'
					break
				if l[j][k + 1] == 'A' or l[j][k - 1] == 'A' or l[j + 1][k] == 'A':
					answer = 'no'
					break
			if l[j][k] == 'B' and j < n - 1:
				if l[j + 1][k] == 'A':
					answer = 'no'
					break
		if answer == 'no':
			break
	print(answer)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	mat = []
	for j in range(n):
		lis = list(input())
		mat.append(lis)
	flag = True
	for j in range(n - 1, -1, -1):
		for k in range(m):
			if mat[j][k] == 'A':
				if j != 0:
					if mat[j - 1][k] == 'B' or mat[j - 1][k] == 'W':
						flag = False
			elif mat[j][k] == 'W':
				if k == 0 or k == m - 1:
					flag = False
				elif mat[j][k - 1] == 'A' or mat[j][k + 1] == 'A':
					flag = False
				elif j != 0:
					if mat[j - 1][k] == 'B':
						flag = False
	if flag:
		print('yes')
	else:
		print('no')


def checka(i, j, n, m, tank):
	flag = 0
	if i == 0:
		if j > 0:
			if tank[i][j - 1] == 'W':
				flag = 1
		if j < m - 1:
			if tank[i][j + 1] == 'W':
				flag = 1
	if i > 0:
		if tank[i - 1][j] == 'W' or tank[i - 1][j] == 'B':
			flag = 1
	if flag:
		return False
	else:
		return True

def checkw(i, j, n, m, tank):
	flag = 0
	if j == 0 or j == m - 1:
		flag = 1
	if j > 0:
		if tank[i][j - 1] == 'A':
			flag = 1
	if j < m - 1:
		if tank[i][j + 1] == 'A':
			flag = 1
	if i > 0:
		if tank[i - 1][j] == 'B':
			flag = 1
	if flag:
		return False
	else:
		return True
T = int(input())
for t_ in range(T):
	(n, m) = map(int, input().split(' '))
	tank = []
	for i in range(n):
		s = input()
		tank.append(list(s))
	flag = 0
	for i in range(n):
		for j in range(m):
			if tank[i][j] == 'A':
				if not checka(i, j, n, m, tank):
					flag = 1
			elif tank[i][j] == 'W':
				if not checkw(i, j, n, m, tank):
					flag = 1
	print('no') if flag else print('yes')