There is a grid with $n$ rows and $m$ columns. Some cells are colored black, and the rest of the cells are colored white.

In one operation, you can select some black cell and do exactly one of the following:

color all cells in its row black, or

color all cells in its column black.

You are given two integers $r$ and $c$. Find the minimum number of operations required to make the cell in row $r$ and column $c$ black, or determine that it is impossible.


-----Input-----

The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 100$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains four integers $n$, $m$, $r$, and $c$ ($1 \leq n, m \leq 50$; $1 \leq r \leq n$; $1 \leq c \leq m$) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively.

Then $n$ lines follow, each containing $m$ characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.


-----Output-----

For each test case, if it is impossible to make the cell in row $r$ and column $c$ black, output $-1$.

Otherwise, output a single integer — the minimum number of operations required to make the cell in row $r$ and column $c$ black.


-----Examples-----

Input
9
3 5 1 4
WBWWW
BBBWB
WWBBB
4 3 2 1
BWW
BBW
WBB
WWB
2 3 2 2
WWW
WWW
2 2 1 1
WW
WB
5 9 5 9
WWWWWWWWW
WBWBWBBBW
WBBBWWBWW
WBWBWBBBW
WWWWWWWWW
1 1 1 1
B
1 1 1 1
W
1 2 1 1
WB
2 1 1 1
W
B
Output
1
0
-1
2
2
0
-1
1
1


-----Note-----

The first test case is pictured below.

We can take the black cell in row $1$ and column $2$, and make all cells in its row black. Therefore, the cell in row $1$ and column $4$ will become black.

In the second test case, the cell in row $2$ and column $1$ is already black.

In the third test case, it is impossible to make the cell in row $2$ and column $2$ black.

The fourth test case is pictured below.

We can take the black cell in row $2$ and column $2$ and make its column black.

Then, we can take the black cell in row $1$ and column $2$ and make its row black.

Therefore, the cell in row $1$ and column $1$ will become black.

Difficulty: EASY

Solutions:
import sys
input = sys.stdin.readline
num_test_cases = int(input())
for test_case_ittr in range(num_test_cases):
	(num_rows, num_cols, row, col) = [int(x) for x in input().split()]
	i_row = row - 1
	j_col = col - 1
	grid = [input().strip() for i in range(num_rows)]
	if grid[i_row][j_col] == 'B':
		print(0)
		continue
	found_answer = False
	for i in range(num_rows):
		if grid[i][j_col] == 'B':
			print(1)
			found_answer = True
			break
	if found_answer:
		continue
	for j in range(num_cols):
		if grid[i_row][j] == 'B':
			print(1)
			found_answer = True
			break
	if found_answer:
		continue
	grid_has_a_black_square = False
	for i in range(num_rows):
		for j in range(num_cols):
			if grid[i][j] == 'B':
				grid_has_a_black_square = True
				break
		if grid_has_a_black_square:
			break
	print(2 if grid_has_a_black_square else -1)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	li = list(zip(*l))
	if l[r - 1][c - 1] == 'B':
		print(0)
		continue
	if 'B' in l[r - 1] or 'B' in li[c - 1]:
		print(1)
		continue
	c = 0
	for i in range(n):
		c += l[i].count('B')
	if c:
		print(2)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	r -= 1
	c -= 1
	arr = []
	for i in range(n):
		arr.append(input())
	if arr[r][c] == 'B':
		print(0)
		continue
	flag = True
	for row in arr:
		if 'B' in row:
			flag = False
			break
	if flag:
		print(-1)
		continue
	if 'B' in arr[r]:
		print(1)
		continue
	res = 2
	for i in range(n):
		if arr[i][c] == 'B':
			res = 1
			break
	print(res)


def stolb(length, Arr, elem, index):
	for k in range(length):
		if Arr[k][index] == elem:
			return True
	return False

def stroka(length, Arr, elem='B'):
	for k in range(length):
		if elem in Arr[k]:
			return True
	return False
for i in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	A = [input() for j in range(n)]
	if stroka(n, A):
		if A[r - 1][c - 1] == 'B':
			print(0)
		elif 'B' in A[r - 1]:
			print(1)
		elif stolb(n, A, 'B', c - 1):
			print(1)
		else:
			print(2)
	else:
		print(-1)


n = int(input())
for i in range(n):
	(a, b, c, d) = map(int, input().split())
	l = list((input() for i in range(a)))
	if l[c - 1][d - 1] == 'B':
		print(0)
	elif 'B' in l[c - 1] or 'B' in (l[i][d - 1] for i in range(a)):
		print(1)
	elif 'B' in ''.join(l):
		print(2)
	else:
		print(-1)


def int_input():
	a = int(input())
	return a

def int_list_input():
	a = list(map(int, input().split()))
	return a

def solve():
	(n, m, r, c) = int_list_input()
	arr = []
	for _ in range(n):
		arr.append(input())
	if arr[r - 1][c - 1] == 'B':
		print(0)
		return
	if arr[r - 1].count('B') >= 1:
		print(1)
		return
	for i in range(n):
		if arr[i][c - 1] == 'B':
			print(1)
			return
	for i in range(n):
		if arr[i].count('B') >= 1:
			print(2)
			return
	print(-1)
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	g = []
	f = True
	for i in range(n):
		x = input()
		g.append(x)
		if 'B' in x:
			f = False
	if f:
		print(-1)
	elif g[r - 1][c - 1] == 'B':
		print(0)
	elif 'B' in g[r - 1] or 'B' in [g[i][c - 1] for i in range(n)]:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	arr = [list(input()) for i in range(n)]
	f = False
	if arr[r - 1][c - 1] == 'B':
		print(0)
		continue
	for i in range(n):
		if arr[i].count('B') == 0:
			f = False
		else:
			f = True
			break
	if not f:
		print(-1)
		continue
	if arr[r - 1].count('B') > 0:
		print(1)
		continue
	f = False
	for i in range(n):
		if arr[i][c - 1] == 'B':
			f = True
	if f:
		print(1)
	else:
		print(2)


for iii in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	r -= 1
	c -= 1
	q = []
	for i in range(n):
		q.append(input())
	if q[r][c] == 'B':
		print(0)
		continue
	if q[r].count('B') > 0:
		print(1)
		continue
	flag = False
	for i in range(n):
		if q[i][c] == 'B':
			flag = True
			break
	if flag:
		print(1)
		continue
	for i in range(n):
		if q[i].count('B') > 0:
			flag = True
			break
	if flag:
		print(2)
		continue
	print(-1)


n = int(input())
for i in range(n):
	(m, n, r, c) = map(int, input().split())
	r -= 1
	c -= 1
	my_map = []
	have_black = False
	for j in range(m):
		row = list(input())
		if 'B' in row:
			have_black = True
		my_map.append(row)
	find = False
	if not have_black:
		print(-1)
		find = True
	elif my_map[r][c] == 'B' and (not find):
		print(0)
		find = True
	if not find:
		for k in range(m):
			if my_map[k][c] == 'B':
				print(1)
				find = True
				break
	if not find:
		for k in range(n):
			if my_map[r][k] == 'B':
				print(1)
				find = True
				break
	if not find:
		print(2)


num = int(input())
for i in range(num):
	(n, m, r, c) = map(int, input().split())
	my_list = []
	for y in range(n):
		stri = input()
		my_list.append(stri)
	have_black = False
	my_string = ''.join(my_list)
	if 'B' in my_string:
		have_black = True
	if not have_black:
		print(-1)
		continue
	elif my_list[r - 1][c - 1] == 'B':
		print(0)
		continue
	elif 'B' in my_list[r - 1]:
		print(1)
		continue
	else:
		column_have_black = False
		for x in range(n):
			if my_list[x][c - 1] == 'B':
				column_have_black = True
				break
		if column_have_black:
			print(1)
		else:
			print(2)


def eachcase():
	(n, m, r, c) = list(map(int, input().split()))
	b = []
	impossible = True
	for i in range(n):
		b.append(input())
		if 'B' in b[i]:
			impossible = False
	if impossible:
		print(-1)
		return
	if b[r - 1][c - 1] == 'B':
		print(0)
		return
	if 'B' in b[r - 1]:
		print(1)
		return
	for i in range(n):
		if b[i][c - 1] == 'B':
			print(1)
			return
	print(2)

def main():
	tcs = int(input())
	for tc in range(tcs):
		eachcase()
main()


for i in range(int(input())):
	(n, m, r, c) = list(map(int, input().split()))
	a = []
	sum = 0
	count = 0
	for i in range(n):
		a.append(list(input()))
		if 'B' not in a[i]:
			count += 1
		if a[i][c - 1] == 'B':
			sum += 1
	if count == n:
		print('-1')
	elif a[r - 1][c - 1] == 'B':
		print(0)
	elif sum > 0 or 'B' in a[r - 1]:
		print(1)
	else:
		print(2)


N = int(input())
for _ in range(N):
	a = []
	(n, m, r, c) = map(int, input().split())
	r -= 1
	c -= 1
	for x in range(n):
		a.append(input())
	result = -1
	if a[r][c] == 'B':
		print(0)
	else:
		for y in range(n):
			if a[y][c] == 'B':
				result = 1
				break
		if result == -1:
			for x in range(m):
				if a[r][x] == 'B':
					result = 1
					break
		if result == -1:
			for y in range(n):
				for x in range(m):
					if a[y][x] == 'B':
						result = 2
						break
		print(result)


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

def solve():
	ret = -1
	for i in range(n):
		for j in range(m):
			if bd[i][j] == 'B':
				if i == r or j == c:
					return 1
				else:
					ret = 2
	return ret
for _ in range(t):
	bd = []
	(n, m, r, c) = map(int, input().split())
	for _ in range(n):
		bd.append(input().strip())
	r -= 1
	c -= 1
	if bd[r][c] == 'B':
		print(0)
		continue
	print(solve())


t = int(input())
for _ in range(t):
	(n, m, r, c) = list(map(int, input().split()))
	(r, c) = (r - 1, c - 1)
	matrix = [[z for z in input()] for _ in range(n)]
	res = -1
	if matrix[r][c] == 'B':
		res = 0
	elif 'B' in matrix[r] or 'B' in [matrix[i][c] for i in range(n)]:
		res = 1
	else:
		for row in matrix:
			if 'B' in row:
				res = 2
	print(res)


n = int(input())
for i in range(n):
	steps = 0
	allwhite = 1
	ip = 0
	l = list(map(int, input().split()))
	(n, m, r, c) = (l[0], l[1], l[2], l[3])
	matrix = []
	for i in range(n):
		line = list(input())
		matrix.append(line)
	r = r - 1
	c = c - 1
	for i in matrix:
		if 'B' in i:
			allwhite = 0
	if allwhite == 1:
		steps = -1
	for i in matrix[r]:
		if i == 'B':
			steps = 1
			ip = 1
	for i in matrix:
		if i[c] == 'B':
			steps = 1
			ip = 1
	if allwhite == 0 and ip == 0:
		steps = 2
	if matrix[r][c] == 'B':
		steps = 0
	print(steps)


n = int(input())
for i in range(n):
	(a, b, c, d) = [int(i) for i in input().split()]
	tabs = []
	for j in range(a):
		tabs.append(input())
	tmp = ''.join(tabs)
	if 'B' not in tmp:
		print(-1)
		continue
	if tabs[c - 1][d - 1] == 'B':
		print(0)
		continue
	found = False
	for j in range(a):
		if tabs[j][d - 1] == 'B':
			found = True
			break
	for j in range(b):
		if tabs[c - 1][j] == 'B':
			found = True
			break
	if found:
		print(1)
	else:
		print(2)


t = int(input())

def isB(s):
	return s == 'B'
for _ in range(t):
	nmrc = [int(x) for x in input().split()]
	n = int(nmrc[0])
	m = int(nmrc[1])
	r = int(nmrc[2]) - 1
	c = int(nmrc[3]) - 1
	arr = []
	hasB = False
	for i in range(n):
		arr.append(input())
		if not hasB and arr[i].find('B') != -1:
			hasB = True
	if not hasB:
		print(-1)
	elif isB(arr[r][c]):
		print(0)
	else:
		isDirectColumOrRow = False
		for num in range(m):
			if isB(arr[r][num]):
				isDirectColumOrRow = True
				break
		if not isDirectColumOrRow:
			for num in range(n):
				if isB(arr[num][c]):
					isDirectColumOrRow = True
					break
		if isDirectColumOrRow:
			print(1)
		else:
			print(2)


t = int(input())
while t > 0:
	(n, m, r, c) = map(int, input().split())
	mat = list((input() for i in range(n)))
	if mat[r - 1][c - 1] == 'B':
		print('0')
	elif 'B' in mat[r - 1] or 'B' in (mat[i][c - 1] for i in range(n)):
		print('1')
	elif 'B' in ''.join(mat):
		print('2')
	else:
		print('-1')
	t -= 1


a = int(input())

def fun1(n, m, r, c, l):
	if l[r - 1][c - 1] == 'B':
		return 0
	b = False
	for i in range(n):
		if l[i][c - 1] == 'B':
			return 1
	for i in range(m):
		if l[r - 1][i] == 'B':
			return 1
	for i in range(n):
		for j in range(m):
			if l[i][j] == 'B':
				return 2
	return -1
while a:
	(n, m, r, c) = map(int, input().split())
	l = []
	for i in range(n):
		l += [input()]
	print(fun1(n, m, r, c, l))
	a -= 1


for i in range(int(input())):
	mf = 0
	(n, m, r, c) = map(int, input().split())
	a = list((input() for i in range(n)))
	if a[r - 1][c - 1] == 'B':
		print(0)
	elif 'B' in a[r - 1] or 'B' in (a[i][c - 1] for i in range(n)):
		print(1)
	elif 'B' not in str(a):
		print(-1)
	else:
		print(2)


t = int(input())
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	grid = []
	for _ in range(n):
		a = input()
		grid.append(a)
	if 'B' == grid[r - 1][c - 1]:
		print(0)
	elif 'B' in grid[r - 1]:
		print(1)
	else:
		p = 0
		for x in range(n):
			if grid[x][c - 1] == 'B':
				print(1)
				p = 1
				break
		if p == 1:
			continue
		for row in range(n):
			for col in range(m):
				if 'B' == grid[row][col]:
					print(2)
					p = 1
					break
			if p == 1:
				break
		if p == 0:
			print(-1)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	l = [input() for _ in range(n)]
	r -= 1
	c -= 1
	if l[r][c] == 'B':
		print(0)
	elif 'B' in l[r] or 'B' in (l[z][c] for z in range(n)):
		print(1)
	elif 'B' in str(l):
		print(2)
	else:
		print(-1)


def splits(word):
	return [char for char in word]
T = int(input())
for i in range(T):
	(n, m, r, c) = [int(x) for x in input().split()]
	n -= 1
	m -= 1
	r -= 1
	c -= 1
	row = []
	have_black = False
	for j in range(n + 1):
		k = splits(input())
		if 'B' in k:
			have_black = True
		row.append(k)
	if row[r][c] == 'B':
		print(0)
	else:
		true = True
		for j in range(n + 1):
			if row[j][c] == 'B':
				print(1)
				true = False
				break
		if true == True:
			for j in range(m + 1):
				if row[r][j] == 'B':
					print(1)
					true = False
					break
		if true == True:
			if have_black == True:
				print(2)
			else:
				print(-1)


t = int(input())
while t > 0:
	t -= 1
	(n, m, r, c) = list(map(int, input().split()))
	a = []
	for i in range(n):
		b = input()
		a.append(b)
	ans = 0
	g = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] == 'B':
				g = 1
				break
		if g == 1:
			break
	if g == 0:
		print('-1')
	elif a[r - 1][c - 1] == 'B':
		print('0')
	else:
		ga = 0
		for j in range(m):
			if a[r - 1][j] == 'B':
				ga = 1
				break
		for i in range(n):
			if a[i][c - 1] == 'B':
				ga = 1
				break
			if ga == 1:
				break
		if ga == 1:
			print('1')
		else:
			print('2')


t = int(input())
for i in range(t):
	(h, w, r, c) = map(int, input().split())
	m = []
	bl = []
	bc = 0
	for i in range(h):
		row = input()
		if 'B' in row:
			bl += [i]
			bc += 1
		m += [row]
	if bc == 0:
		print(-1)
	elif m[r - 1][c - 1] == 'B':
		print(0)
	elif 'B' in m[r - 1] or 'B' in list((i[c - 1] for i in m)):
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	g = []
	r = r - 1
	c = c - 1
	h = 0
	d = 0
	for i in range(n):
		g.append(str(input()))
	for i in range(n):
		if 'B' in g[i]:
			d = 1
			break
	if d == 0:
		print(-1)
	elif g[r][c] == 'B' and d == 1:
		print(0)
	elif 'B' in g[r]:
		print(1)
	else:
		for i in range(n):
			if g[i][c] == 'B':
				h = 1
				break
		if h == 1:
			print(1)
		else:
			print(2)


t = int(input())
while t:
	(a, b, c, d) = map(int, input().split())
	w = []
	q = 1
	e = 2
	while a:
		s = input()
		w += [s]
		if 'B' in s:
			q = 0
		if s[d - 1] == 'B':
			e = 1
		a -= 1
	if q:
		print(-1)
	elif w[c - 1][d - 1] == 'B':
		print(0)
	elif 'B' in w[c - 1]:
		print(1)
	else:
		print(e)
	t -= 1


import sys, bisect, math
from collections import deque, Counter
input = lambda : sys.stdin.readline().strip()
ceil = lambda x: math.ceil(x)
mod = 10 ** 9 + 7

def solve():
	(n, m, r, c) = map(int, input().split())
	a = [input() for i in range(n)]
	w = 0
	for i in a:
		w += i.count('W')
	if w == n * m:
		return -1
	if a[r - 1][c - 1] == 'B':
		return 0
	b = 0
	for i in range(n):
		if a[i][c - 1] == 'B':
			b += 1
	for i in range(m):
		if a[r - 1][i] == 'B':
			b += 1
	if b > 0:
		return 1
	return 2
t = 1
t = int(input())
for _ in range(t):
	print(solve())


nt = int(input())
for t in range(nt):
	l = input().split()
	gr = []
	for i in range(int(l[0])):
		gr.append(input())
	if sum(['B' in j for j in gr]) == 0:
		ans = -1
	elif gr[int(l[2]) - 1][int(l[3]) - 1] == 'B':
		ans = 0
	elif 'B' in gr[int(l[2]) - 1] or 'B' in [kk[int(l[3]) - 1] for kk in gr]:
		ans = 1
	else:
		ans = 2
	print(ans)


TT = int(input())
for i in range(TT):
	(n, m, a, b) = map(int, input().split())
	foo = 0
	lst = []
	for i in range(n):
		lst.append(input())
		if 'B' in lst[i]:
			foo = 1
	if foo:
		if lst[a - 1][b - 1] == 'B':
			print('0')
		else:
			f = 0
			for i in range(n):
				if lst[i][b - 1] == 'B':
					f = 1
			for i in range(m):
				if lst[a - 1][i] == 'B':
					f = 1
			if f == 1:
				print('1')
			else:
				print('2')
	else:
		print('-1')


from itertools import permutations
import sys, math
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	A = []
	for i in range(n):
		s = input()
		A.append(s)
	if A[r - 1][c - 1] == 'B':
		print(0)
		continue
	ok = False
	for i in A:
		for j in i:
			if j == 'B':
				ok = True
	if not ok:
		print(-1)
	else:
		f = False
		for i in range(m):
			if A[r - 1][i] == 'B':
				f = True
		for i in range(n):
			if A[i][c - 1] == 'B':
				f = True
		if f:
			print(1)
		else:
			print(2)


for i in range(int(input())):
	(n, m, x, y) = list(map(int, input().split()))
	grid = []
	b = -1
	for i in range(n):
		st = input()
		if 'B' in st:
			b = 1
		arr = list(st)
		grid.append(arr)
	if b != 1:
		print(-1)
	else:
		x -= 1
		y -= 1
		ways = 0
		if grid[x][y] == 'B':
			ways = 0
		else:
			done = 2
			if 'B' in grid[x]:
				done = 1
			for i in range(n):
				if grid[i][y] == 'B':
					done = 1
			ways = done
		print(ways)


t = int(input())
ans = []
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	a = []
	imp = True
	for i in range(n):
		s = input()
		if s.count('B') > 0:
			imp = False
		a.append(s)
	if imp:
		ans.append(-1)
		continue
	r -= 1
	c -= 1
	if a[r][c] == 'B':
		ans.append(0)
		continue
	if a[r].count('B') > 0:
		ans.append(1)
		continue
	for i in range(n):
		if a[i][c] == 'B':
			ans.append(1)
			break
	else:
		ans.append(2)
for i in ans:
	print(i)


from sys import stdin

def solve():
	for _ in range(int(stdin.readline().strip())):
		(n, m, r, c) = map(int, stdin.readline().rstrip().split())
		grid = []
		f = 0
		notfound = 1
		for _ in range(n):
			l = stdin.readline().rstrip()
			if l[c - 1] == 'B':
				f = 1
			if l.find('B') != -1:
				notfound = 0
			grid.append(l)
		if grid[r - 1][c - 1] == 'B':
			print(0)
			continue
		if notfound:
			print(-1)
			continue
		if grid[r - 1].find('B') != -1 or f:
			print(1)
			continue
		print(2)
solve()


t = int(input())
for i in range(t):
	grid = []
	black = False
	one = False
	(n, m, r, c) = [int(i) for i in input().split()]
	for j in range(n):
		row = list(input())
		if 'B' in row:
			black = True
		grid.append(row)
	if black:
		if grid[r - 1][c - 1] == 'B':
			print(0)
		elif 'B' in grid[r - 1]:
			print(1)
		else:
			for j in grid:
				if j[c - 1] == 'B':
					one = True
			if one:
				print(1)
			else:
				print(2)
	else:
		print(-1)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	a = []
	for i in range(n):
		a1 = list(str(input()))
		a.append(a1)
	if a[r - 1][c - 1] == 'B':
		print(0)
	else:
		flag = 0
		for i in range(n):
			if a[i][c - 1] == 'B':
				flag = 1
				break
		for j in range(m):
			if a[r - 1][j] == 'B':
				flag = 1
				break
		if flag == 1:
			print(1)
		else:
			flag = 0
			for i in range(n):
				for j in range(m):
					if a[i][j] == 'B':
						flag = 1
						break
				if flag == 1:
					break
			if flag == 1:
				print(2)
			else:
				print(-1)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	r -= 1
	c -= 1
	a = [input() for i in range(n)]
	if a[r][c] == 'B':
		print(0)
	elif 'B' in a[r]:
		print(1)
	else:
		ans = -1
		for i in range(n):
			if a[i][c] == 'B':
				ans = 1
				break
		if ans == -1:
			for i in range(n):
				if 'B' in a[i]:
					ans = 2
					break
		print(ans)


t = int(input())
for i in range(0, t):
	(a, b, c, d) = map(int, input().split())
	lis = []
	flag = False
	for j in range(0, a):
		k = input()
		lis.append(k)
		if 'B' in k:
			flag = True
	if flag == False:
		print(-1)
	else:
		k = ''
		for z in lis:
			k = k + z[d - 1]
		if lis[c - 1][d - 1] == 'B':
			print(0)
		elif 'B' in lis[c - 1]:
			print(1)
		elif 'B' in k:
			print(1)
		else:
			print(2)


for _ in range(int(input())):
	(n, m, r, c) = [int(x) for x in input().split()]
	mat = []
	bl = True
	for i in range(n):
		mat.append(input())
		if 'B' in mat[i]:
			bl = False
	if bl:
		print(-1)
	elif mat[r - 1][c - 1] == 'B':
		print(0)
	else:
		for i in range(n):
			if mat[i][c - 1] == 'B':
				bl = True
				break
		for j in range(m):
			if mat[r - 1][j] == 'B':
				bl = True
				break
		if bl:
			print(1)
		else:
			print(2)


def solve(n, m, r, c, grid):
	if not any(('B' in row for row in grid)):
		return -1
	if grid[r - 1][c - 1] == 'B':
		return 0
	for i in range(n):
		if grid[i][c - 1] == 'B':
			return 1
	for j in range(m):
		if grid[r - 1][j] == 'B':
			return 1
	return 2
T = int(input())
for _ in range(T):
	(n, m, r, c) = [int(x) for x in input().split()]
	grid = []
	for i in range(n):
		grid.append(input())
	result = solve(n, m, r, c, grid)
	print(result)


def solution():
	(n, m, r, c) = map(int, input().split())
	G = []
	for i in range(n):
		G.append(input())
	if G[r - 1][c - 1] == 'B':
		print(0)
		return
	flag = False
	for i in G:
		if 'B' in i:
			flag = True
	if not flag:
		print(-1)
		return
	if 'B' in G[r - 1] or 'B' in [x[c - 1] for x in G]:
		print(1)
	else:
		print(2)
try:
	import sys
	import os
	if os.path.exists('.cpl_files/1.in.txt'):
		sys.stdin = open('.cpl_files/1.in.txt', 'r')
		sys.stdout = open('.cpl_files/1.out.txt', 'w')
except:
	pass
t = int(input())
while t:
	solution()
	t -= 1


for _ in range(int(input())):
	(n, m, r, c) = (int(x) for x in input().split())
	field = [input() for _ in range(n)]
	if field[r - 1][c - 1] == 'B':
		print(0)
		continue
	if any((x == 'B' for x in field[r - 1])):
		print(1)
		continue
	if any((field[x][c - 1] == 'B' for x in range(n))):
		print(1)
		continue
	if any(('B' in row for row in field)):
		print(2)
		continue
	print(-1)


def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))
for _ in range(ii()):
	(n, m, r, c) = mi()
	a = []
	for i in range(n):
		a.append(input())
	for i in range(n):
		if 'B' in a[i]:
			break
	else:
		print(-1)
		continue
	if a[r - 1][c - 1] == 'B':
		print(0)
	elif any((a[i][c - 1] == 'B' for i in range(n))) or any((a[r - 1][j] == 'B' for j in range(m))):
		print(1)
	else:
		print(2)


for t in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	S = [input() for j in range(n)]
	if S[r - 1][c - 1] == 'B':
		print(0)
	else:
		flag = 0
		for i in range(n):
			if S[i][c - 1] == 'B':
				flag = 1
				break
		for i in range(m):
			if S[r - 1][i] == 'B':
				flag = 1
				break
		if flag:
			print(1)
		else:
			flag = 0
			for i in range(n):
				for j in range(m):
					if S[i][j] == 'B':
						flag = 1
						break
			if flag:
				print(2)
			else:
				print(-1)


for _ in range(int(input())):
	(n, m, r, c) = list(map(int, input().split()))
	mat = []
	hasBlack = False
	for i in range(n):
		z = str(input())
		if 'B' in z:
			hasBlack = True
		mat.append(z)
	r -= 1
	c -= 1
	found = False
	if not hasBlack:
		print(-1)
	elif mat[r][c] == 'B':
		print(0)
	elif 'B' in mat[r]:
		print(1)
	else:
		for i in range(n):
			if mat[i][c] == 'B':
				found = True
		if found:
			print(1)
		else:
			print(2)


t = int(input())
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	li = []
	y = 0
	for i in range(n):
		st = input()
		for i in st:
			if i == 'B':
				y = 1
				break
		nli = [char for char in st]
		li.append(nli)
	if y == 0:
		print(-1)
	elif li[r - 1][c - 1] == 'B':
		print(0)
	else:
		y = 0
		nli = li[r - 1]
		for i in range(m):
			if nli[i] == 'B':
				y = 1
		for i in range(n):
			if li[i - 1][c - 1] == 'B':
				y = 1
		if y == 1:
			print(1)
		else:
			print(2)


import collections
from collections import defaultdict
for _ in range(int(input())):
	(n, m, r, c) = [int(x) for x in input().split()]
	r -= 1
	c -= 1
	a = []
	for i in range(n):
		a.append(input())
	if a[r][c] == 'B':
		print(0)
		continue
	if 'B' in a[r]:
		print(1)
	elif 'B' in [a[j][c] for j in range(n)]:
		print(1)
	elif len([x for x in a if 'B' in x]) > 0:
		print(2)
	else:
		print(-1)


for _ in range(int(input())):
	(N, M, R, C) = map(int, input().split())
	L = []
	ctr = 0
	for __ in range(N):
		S = input()
		L.append(S)
		if ctr == 0:
			if S[C - 1] == 'B':
				ctr = 1
	if L[R - 1][C - 1] == 'B':
		print(0)
	elif ctr == 1:
		print(1)
	elif 'B' in L[R - 1]:
		print(1)
	else:
		for i in L:
			if 'B' in i:
				print(2)
				break
		else:
			print(-1)


a = int(input())
for i in range(a):
	(n, m, r, c) = map(int, input().split())
	arr = []
	for i in range(n):
		arr.append(input())
	if arr[r - 1][c - 1] == 'B':
		print(0)
	else:
		black = False
		if arr[r - 1].count('B') >= 1:
			black = True
		else:
			for i in range(n):
				if arr[i][c - 1] == 'B':
					black = True
		if black:
			print(1)
		else:
			black = False
			for i in range(n):
				if arr[i].count('B') >= 1:
					black = True
			if black:
				print(2)
			else:
				print(-1)


def main():
	k = int(input())
	for _ in range(k):
		(n, m, r, c) = map(int, input().split())
		r -= 1
		c -= 1
		arr = [input() for _ in range(n)]
		foo = False
		if arr[r][c] == 'B':
			print(0)
			continue
		for i in range(n):
			if arr[i][c] == 'B':
				print(1)
				foo = True
				break
		if foo:
			continue
		for j in range(m):
			if arr[r][j] == 'B':
				print(1)
				foo = True
				break
		if foo:
			continue
		for i in range(n):
			for j in range(m):
				if arr[i][j] == 'B':
					print(2)
					foo = True
					break
			if foo:
				break
		if foo:
			continue
		print(-1)
main()


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	l = []
	for i in range(n):
		l.append(input())
	r -= 1
	c -= 1
	if l[r][c] == 'B':
		print(0)
		continue
	if l == ['W' * m] * n:
		print(-1)
		continue
	ans = -1
	for i in range(n):
		if l[i][c] == 'B':
			ans = 1
			break
	for i in range(m):
		if l[r][i] == 'B':
			ans = 1
			break
	if ans == 1:
		print(1)
	else:
		print(2)


import math

def run(function):

	def wrapper(*args, **kwargs):
		testcases = int(input())
		for _ in range(testcases):
			function(*args, **kwargs)
	return wrapper

def map_input(datatype=int) -> map:
	return map(datatype, input().split())

def list_input(datatype=int) -> list:
	return list(map(datatype, input().split()))

@run
def solve() -> None:
	(n, m, r, c) = map_input()
	mtx = []
	for i in range(n):
		arr = input()
		mtx.append(arr)
	if mtx[r - 1][c - 1] == 'B':
		print(0)
	else:
		flag = False
		for i in range(n):
			if 'B' in mtx[i][c - 1]:
				flag = True
				break
		if flag == True or 'B' in mtx[r - 1]:
			print(1)
		else:
			_flag = False
			for i in range(n):
				if mtx[i].count('B') != 0:
					_flag = True
					break
			if _flag is True:
				print(2)
			else:
				print(-1)
solve()


def res():
	(n, m, r, c) = map(int, input().split())
	r = r - 1
	c = c - 1
	field = []
	isB = False
	isNear = False
	for i in range(n):
		l = []
		st = input()
		for j in range(m):
			if st[j] == 'W':
				l.append(0)
			else:
				isB = True
				if i == r or j == c:
					isNear = True
				l.append(1)
		field.append(l)
	if not isB:
		return -1
	if field[r][c]:
		return 0
	if not isNear:
		return 2
	return 1
t = int(input())
for _ in range(t):
	r = res()
	print(r)


T = int(input())

def solve():
	if grid[r][c] == 'B':
		return 0
	for i in range(m):
		if grid[r][i] == 'B':
			return 1
	for i in range(n):
		if grid[i][c] == 'B':
			return 1
	return 2
for t in range(T):
	(n, m, r, c) = map(int, input().split())
	r -= 1
	c -= 1
	grid = []
	for i in range(n):
		grid.append(list(input()))
	black = 0
	for row in grid:
		black += row.count('B')
	if black == 0:
		print(-1)
	else:
		print(solve())


__author__ = 'corlet'

def check_col(mat, row, col):
	for i_row in range(len(mat)):
		if mat[i_row][col] == 'B':
			return True
	return False

def check_row(mat):
	for i_row in mat:
		if 'B' in i_row:
			return True
	return False

def main():
	t = int(input())
	while t:
		(n, m, r, c) = map(int, input().split())
		mat = [input() for i in range(n)]
		r -= 1
		c -= 1
		if mat[r][c] == 'B':
			print(0)
		elif 'B' in mat[r] or check_col(mat, r, c):
			print(1)
		elif check_row(mat):
			print(2)
		else:
			print(-1)
		t -= 1
main()


t = int(input())
for _ in range(t):
	(n, m, r, c) = [int(i) for i in input().split()]
	store = []
	for i in range(n):
		rowI = input()
		store.append(rowI)
	if store[r - 1][c - 1] == 'B':
		print(0)
	else:
		isPresent = False
		for i in range(n):
			for j in range(m):
				if store[i][j] == 'B':
					isPresent = True
					break
			if isPresent:
				break
		if not isPresent:
			print(-1)
		else:
			flag = False
			for i in range(n):
				if store[i][c - 1] == 'B':
					flag = True
					break
			if not flag:
				for i in range(m):
					if store[r - 1][i] == 'B':
						flag = True
						break
			if flag:
				print(1)
			else:
				print(2)


def function(m, n, r, c, mat):
	black_found = False
	for i in range(m):
		if 'B' in mat[i]:
			black_found = True
			break
	if not black_found:
		return -1
	r -= 1
	c -= 1
	if mat[r][c] == 'B':
		return 0
	if 'B' in mat[r] or 'B' in [mat[i][c] for i in range(m)]:
		return 1
	return 2
t = int(input())
for _ in range(t):
	vals = list(map(int, input().split()))
	mat = []
	for i in range(vals[0]):
		s = input()
		mat.append(list(s))
	print(function(vals[0], vals[1], vals[2], vals[3], mat))


t = int(input())
for i in range(t):
	(n, m, nn, nm) = map(int, input().split())
	nn -= 1
	nm -= 1
	a = []
	z = 0
	for i in range(n):
		a1 = input()
		a.append(a1)
		z += a1.count('B')
	if z == 0:
		print(-1)
	elif a[nn][nm] == 'B':
		print(0)
	else:
		t = 0
		z = 0
		for i in range(n):
			if a[i][nm] == 'B':
				print(1)
				t = 1
				break
		if t == 0:
			for i in range(m):
				if a[nn][i] == 'B':
					print(1)
					t = 1
					break
		if t == 0:
			print(2)


def solve():
	(n, m, r, c) = list(map(int, list(input().split())))
	li = []
	for i in range(n):
		li.append(input())
	r -= 1
	c -= 1
	cnt = 0
	for element in li:
		if 'B' in element:
			cnt += 1
	if cnt == 0:
		print(-1)
		return
	if li[r][c] == 'B':
		print(0)
		return
	for element in li[r]:
		if element == 'B':
			print(1)
			return
	for i in range(n):
		if li[i][c] == 'B':
			print(1)
			return
	print(2)
q = int(input())
for i in range(q):
	solve()


n = int(input())
for i in range(n):
	(r, c, x, y) = map(int, input().split())
	matrix = []
	for j in range(r):
		matrix.append(input())
	x = x - 1
	y = y - 1
	if matrix[x][y] == 'B':
		print(0)
		continue
	elif 'B' in matrix[x]:
		print(1)
		continue
	flag = 0
	for j in range(r):
		if matrix[j][y] == 'B':
			flag = 1
			break
	if flag == 1:
		print(1)
		continue
	flag1 = 0
	for j in range(r):
		if 'B' in matrix[j]:
			flag1 = 1
			break
	if flag1 == 1:
		print(2)
	else:
		print(-1)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	board = [input() for _ in range(n)]
	ans = -1
	for s in board:
		if 'B' in s:
			ans = 2
	if 'B' in board[r - 1]:
		ans = 1
	for i in range(n):
		if 'B' == board[i][c - 1]:
			ans = 1
	if 'B' == board[r - 1][c - 1]:
		ans = 0
	print(ans)


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	l = []
	for i in range(n):
		help = input()
		l.append(help)
	test = False
	test2 = l[r - 1]
	if test2[c - 1] == 'B':
		print(0)
	else:
		for i in l:
			if 'B' in i:
				test = True
				break
		if test == True:
			if 'B' in l[r - 1]:
				print('1')
			else:
				test1 = False
				for i in l:
					if i[c - 1] == 'B':
						test1 = True
						break
				if test1 == True:
					print(1)
				else:
					print(2)
		else:
			print(-1)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	s = []
	for i in range(n):
		s.append(input())
	if s[r - 1][c - 1] == 'B':
		print(0)
	elif s[r - 1].count('B') != 0:
		print(1)
	else:
		temp = -1
		for i in range(n):
			if s[i][c - 1] == 'B':
				temp = 1
				break
		if temp != 1:
			for i in range(n):
				for j in range(m):
					if s[i][j] == 'B':
						temp = 2
						break
		print(temp)


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	a = []
	k = False
	bc = False
	for i in range(n):
		a.append(input())
		if a[i][c - 1] == 'B':
			k = True
		if a[i].count('B') > 0:
			bc = True
	if a[r - 1][c - 1] == 'B':
		print(0)
		continue
	if a[r - 1].count('B') >= 1 or k:
		print(1)
		continue
	if bc:
		print(2)
		continue
	print(-1)


for _ in range(int(input())):
	(n, m, r, c) = list(map(int, input().split()))
	l = []
	for i in range(n):
		l.append(list(input()))
	r = r - 1
	c = c - 1
	if l[r][c] == 'B':
		print(0)
		continue
	key = 0
	for i in range(0, m):
		if l[r][i] == 'B':
			print(1)
			key = 1
			break
	if key == 0:
		for i in range(0, n):
			if l[i][c] == 'B':
				print(1)
				key = 1
				break
	if key == 0:
		for i in range(0, n):
			if l[i].count('B') > 0:
				print(2)
				key = 1
				break
	if key == 0:
		print(-1)


def fun(a, x):
	for i in a:
		if i[x] == 'B':
			return True
	return False

def Fun(a):
	for i in a:
		if 'B' in i:
			return False
	return True
t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	a = []
	for i in range(n):
		a.append(input())
	if Fun(a):
		print(-1)
	elif a[r - 1][c - 1] == 'B':
		print(0)
	elif 'B' in a[r - 1] or fun(a, c - 1):
		print(1)
	else:
		print(2)


t = int(input())
for _ in range(t):
	(n, m, r, c) = list(map(int, input().split()))
	arr = []
	for i in range(n):
		x = list(input())
		arr.append(x)
	f = 0
	for i in range(n):
		if 'B' in arr[i]:
			f = 1
			break
	if f:
		if arr[r - 1][c - 1] == 'B':
			print(0)
			continue
		elif 'B' in arr[r - 1]:
			print(1)
			continue
		else:
			flag = 0
			for j in range(n):
				if arr[j][c - 1] == 'B':
					print(1)
					flag = 1
					break
			if flag == 0:
				print(2)
				continue
	else:
		print(-1)


nt = int(input())
ans = []

def soln(mat, r, c):
	if mat[r][c] == 'B':
		return 0
	for x in range(m):
		if mat[r][x] == 'B':
			return 1
	for x in range(n):
		if mat[x][c] == 'B':
			return 1
	for x in range(n):
		for y in range(m):
			if mat[x][y] == 'B':
				return 2
	return -1
for x in range(nt):
	inp = list(map(int, input().split()))
	(n, m, r, c) = (inp[0], inp[1], inp[2], inp[3])
	mat = []
	for x in range(n):
		t = list(input())
		mat.append(t)
	ans.append(soln(mat, r - 1, c - 1))
for a in ans:
	print(a)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	l = []
	for i in range(n):
		s = input()
		l.append(s)
	if l[r - 1][c - 1] == 'B':
		print(0)
		continue
	f = 0
	ans = 4
	if 'B' in l[r - 1]:
		print(1)
		continue
	for i in l:
		if i[c - 1] == 'B':
			ans = min(ans, 1)
			f = 1
			break
		if 'B' in i:
			ans = min(ans, 2)
			f = 1
	if f == 0:
		print(-1)
	else:
		print(ans)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	arr_r = []
	flag = 0
	for i in range(n):
		row = input()
		if 'B' in row:
			flag = 1
		arr_r.append(row)
	if arr_r[r - 1][c - 1] == 'B':
		print(0)
	elif flag == 0:
		print(-1)
	elif 'B' in arr_r[r - 1]:
		print(1)
	else:
		for i in arr_r:
			if i[c - 1] == 'B':
				print(1)
				break
		else:
			print(2)


from operator import truediv
t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split(' '))
	a = []
	for i in range(n):
		a.append(input())
	flag1 = False
	flag = False
	if a[r - 1][c - 1] == 'B':
		print(0)
	else:
		for i in range(n):
			if a[i][c - 1] == 'B':
				print(1)
				flag = True
				break
		if not flag:
			for i in range(m):
				if a[r - 1][i] == 'B':
					print(1)
					flag = True
					break
		if not flag:
			for i in a:
				for j in i:
					if j == 'B':
						print(2)
						flag1 = True
						break
				if flag1:
					break
		if not flag and (not flag1):
			print(-1)


import sys
input = sys.stdin.readline
t = int(input())
R = []
for _ in range(t):
	(n, m, r, c) = map(int, input()[:-1].split())
	r -= 1
	c -= 1
	is_black_exist = False
	row_exist = False
	col_exist = False
	g = []
	for i in range(n):
		g.append(input()[:-1])
		if not is_black_exist:
			if 'B' in g[-1]:
				is_black_exist = True
		if i == r:
			if 'B' in g[-1]:
				row_exist = True
		if g[i][c] == 'B':
			col_exist = True
	if not is_black_exist:
		R.append('-1')
	elif g[r][c] == 'B':
		R.append('0')
	elif row_exist or col_exist:
		R.append('1')
	else:
		R.append('2')
print('\n'.join(R))


t = int(input())
for _ in range(t):
	(n, m, r, c) = [int(x) for x in input().split()]
	r -= 1
	c -= 1
	mat = []
	cb = False
	for _ in range(n):
		stringa = input()
		mat.append(stringa)
		if 'B' in stringa and (not cb):
			cb = True
	if mat[r][c] == 'B':
		print(0)
	elif not cb:
		print(-1)
	elif 'B' in mat[r] or 'B' in [row[c] for row in mat]:
		print(1)
	else:
		print(2)


t = int(input())
while t:
	t -= 1
	(n, m, r, c) = map(int, input().split())
	li = []
	f = 0
	for i in range(0, n):
		s = input()
		if s.count('B') >= 1:
			f = 1
		li.append(s)
	if li[r - 1][c - 1] == 'B':
		print('0')
	else:
		k = 0
		for i in range(0, m):
			if li[r - 1][i] == 'B':
				k = 1
		for i in range(0, n):
			if li[i][c - 1] == 'B':
				k = 1
		if k == 1:
			print('1')
		elif f == 1:
			print('2')
		else:
			print('-1')


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	board = []
	for i in range(n):
		board.append([i for i in input()])
	flag = False
	for i in board:
		if 'B' in i:
			flag = True
			break
	if not flag:
		print(-1)
	else:
		(r, c) = (r - 1, c - 1)
		if board[r][c] == 'B':
			print(0)
		else:
			ansFound = False
			for i in board[r]:
				if i == 'B':
					print(1)
					ansFound = True
					break
			if not ansFound:
				for i in range(n):
					if board[i][c] == 'B':
						print(1)
						ansFound = True
						break
				if not ansFound:
					print(2)


t = int(input())
for _ in range(t):
	s = input()
	s = s.split()
	for i in range(4):
		s[i] = int(s[i])
	col = []
	for j in range(s[0]):
		temp = input()
		col.append(temp)
	if col[s[2] - 1][s[3] - 1] == 'B':
		print(0)
		continue
	black = 0
	flag = 0
	white = 1
	for i in range(s[0]):
		if 'B' in col[i]:
			white = 0
		if i == s[2] - 1:
			if 'B' in col[i]:
				flag = 1
				break
		if col[i][s[3] - 1] == 'B':
			flag = 1
			break
	if flag == 1 and white == 0:
		print(1)
	elif flag == 0 and white == 0:
		print(2)
	else:
		print(-1)


t = int(input())

def col(li, c):
	for r in li:
		if 'B' == r[c]:
			return True
	return False
for u in range(t):
	i = input()
	i = i.split()
	n = int(i[0])
	m = int(i[1])
	r = int(i[2])
	c = int(i[3])
	li = []
	flag = 0
	for j in range(n):
		k = input()
		li.append(k)
		if 'B' in k:
			flag = 1
	if flag == 0:
		print(-1)
		continue
	if r - 1 >= n or c - 1 >= m:
		print(-1)
		continue
	if li[r - 1][c - 1] == 'B':
		print(0)
	elif li[r - 1][c - 1] == 'W' and ('B' in li[r - 1] or col(li, c - 1) == True):
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	arr = []
	count = 0
	for i in range(n):
		arr.append(input().strip())
		count += arr[-1].count('B')
	if arr[r - 1][c - 1] == 'B':
		print(0)
	elif count == 0:
		print(-1)
	elif 'B' in arr[r - 1]:
		print(1)
	else:
		cnt = 0
		for i in range(n):
			if arr[i][c - 1] == 'B':
				cnt += 1
				break
		print([2, 1][cnt])


t = int(input())
for i in range(t):
	(n, m, r, c) = input().split()
	r = int(r) - 1
	c = int(c) - 1
	(black, row_col, this) = (0, 0, 0)
	for j in range(int(n)):
		rows = input()
		if 'B' in rows:
			black += 1
			if r == j and rows[c] == 'B':
				this = 1
			if r == j or rows[c] == 'B':
				row_col += 1
	if this == 1:
		print(0)
	elif row_col > 0:
		print(1)
	elif black > 0:
		print(2)
	else:
		print(-1)


t = int(input())
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	a = []
	for i in range(n):
		s = input()
		a.append(s)
	if a[r - 1][c - 1] == 'B':
		print(0)
		continue
	else:
		ans = None
		for i in range(n):
			if a[i][c - 1] == 'B':
				ans = 1
				break
		for j in range(m):
			if a[r - 1][j] == 'B':
				ans = 1
				break
		if ans != None:
			print(ans)
			continue
		ans = -1
		for i in range(n):
			for j in range(m):
				if a[i][j] == 'B':
					ans = 2
					break
		print(ans)


for t in range(int(input())):
	(n, m, r, c) = list(map(int, input().split()))
	f = False
	ans = 2
	for i in range(n):
		s = input()
		if i == r - 1 and s[c - 1] == 'B':
			ans = 0
		elif i == r - 1 and 'B' in s:
			ans = min(1, ans)
		elif s[c - 1] == 'B':
			ans = min(1, ans)
		if not f and 'B' in s:
			f = True
	if f:
		print(ans)
	else:
		print(-1)


t = int(input())
for i in range(t):
	(n, m, r, c) = map(int, input().split())
	ls = []
	b = 0
	for j in range(n):
		s = input()
		if b == 0:
			d = s.count('B')
			b = b + d
		ls.append(s)
	aa = 0
	for j in ls:
		if j[c - 1] == 'B':
			aa = aa + 1
			break
	d = ls[r - 1]
	for j in d:
		if j == 'B':
			aa = aa + 1
			break
	ss = d[c - 1]
	if b == 0:
		print(-1)
	elif ss == 'B':
		print(0)
	elif aa > 0:
		print(1)
	else:
		print(2)


def main():
	t = int(input())
	for i in range(t):
		datos = list(map(int, input().split()))
		matriz = []
		posible = False
		for j in range(datos[0]):
			fila = input()
			if posible == False:
				if 'B' in fila:
					posible = True
			matriz.append(fila)
		if posible == False:
			print(-1)
		elif matriz[datos[2] - 1][datos[3] - 1] == 'B':
			print(0)
		elif 'B' in matriz[datos[2] - 1]:
			print(1)
		else:
			res = False
			m = 0
			while m < datos[0]:
				if 'B' == matriz[m][datos[3] - 1]:
					print(1)
					res = True
					break
				m += 1
			if res == False:
				print(2)
main()


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	arr = []
	flag = 1
	for i in range(n):
		temp = str(input())
		if flag:
			if 'B' in temp:
				flag = 0
		arr.append(temp)
	if arr[r - 1][c - 1] == 'B':
		print(0)
		continue
	if flag == 1:
		print(-1)
		continue
	row = arr[r - 1]
	cr_black = 0
	for i in range(m):
		if row[i] == 'B':
			cr_black = 1
			break
	cc_black = 0
	for i in range(n):
		if arr[i][c - 1] == 'B':
			cc_black = 1
			break
	if cc_black + cr_black == 0:
		print(2)
	else:
		print(1)


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	g = [[1 * (x == 'B') for x in input()] for i in range(n)]
	sumrow = sum(g[r - 1])
	sumcol = sum([g[i][c - 1] for i in range(n)])
	if g[r - 1][c - 1] == 1:
		print(0)
	elif sumrow > 0 or sumcol > 0:
		print(1)
	elif sum([sum(g[x]) for x in range(n)]) > 0:
		print(2)
	else:
		print(-1)


for i in range(int(input())):
	(s, b, n, m) = list(map(int, input().split()))
	a = []
	t = set()
	flag = 0
	ans = 1
	for i in range(s):
		x = list(input())
		if i == n - 1:
			if 'B' in x:
				flag = 1
			if x[m - 1] == 'B':
				flag = 2
				ans = 2
		if x[m - 1] == 'B':
			flag = 1
		t.update(set(x))
		a.append(x)
	if 'B' in t:
		if ans == 2:
			print(0)
		elif flag == 1:
			print(1)
		else:
			print(2)
	else:
		print(-1)


def colCheck(arr, col):
	for i in arr:
		if i[col] == 'B':
			return True
for _ in range(int(input())):
	(n, m, r, c) = map(int, input().split())
	flag = False
	arr = []
	for i in range(n):
		s = input()
		if 'B' in s:
			flag = True
		arr.append(s[::1])
	if flag == False:
		print(-1)
		continue
	row = r - 1
	col = c - 1
	if arr[row][col] == 'B':
		print(0)
	elif 'B' in arr[row]:
		print(1)
	elif colCheck(arr, col):
		print(1)
	else:
		print(2)


T = int(input())

def main(x, y, l):
	flag = False
	if l[x][y] == 'B':
		return 0
	for line in l:
		if line.find('B') != -1:
			flag = True
			break
	if not flag:
		return -1
	if l[x].find('B') != -1:
		return 1
	else:
		for i in range(len(l)):
			if i == x:
				continue
			flag = False
			if l[i][y] == 'B':
				flag = True
				break
		if flag:
			return 1
		else:
			return 2

class case:

	def __init__(self, Line):
		temp = Line.split()
		self.rows = int(temp[0])
		self.x = int(temp[2]) - 1
		self.y = int(temp[3]) - 1
		self.Lines = []
cases = []
for i in range(T):
	line = input()
	cases.append(case(line))
	for _ in range(cases[i].rows):
		cases[i].Lines.append(input())
for i in range(T):
	print(main(cases[i].x, cases[i].y, cases[i].Lines))


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	arr = []
	flag = 0
	for i in range(n):
		arr.append(input())
		if flag == 0 and 'B' in arr[i]:
			flag = 1
	if flag == 0:
		print(-1)
	else:
		(c1, c2) = (0, 0)
		for i in range(m):
			if arr[r - 1][i] == 'B':
				c1 += 1
		for i in range(n):
			if arr[i][c - 1] == 'B':
				c2 += 1
		if arr[r - 1][c - 1] == 'B':
			print(0)
		elif c1 >= 1 or c2 >= 1:
			print(1)
		else:
			print(2)


for _ in range(int(input())):
	(a, b, c, d) = map(int, input().split())
	blank = 0
	k = []
	yy = False
	for i in range(a):
		temp = input()
		if temp.count('B') != 0:
			blank = +1
		k.append(temp)
	if blank == 0:
		print(-1)
	elif k[c - 1][d - 1] == 'B':
		print(0)
	else:
		for i2 in range(a):
			if k[i2][d - 1] == 'B':
				yy = True
		if k[c - 1].count('B') != 0 or yy == True:
			print(1)
		else:
			print(2)


t = int(input())
for _ in range(t):
	(n, m, r, c) = map(int, input().split())
	l = []
	for _ in range(n):
		l.append(input())
	if l[r - 1][c - 1] == 'B':
		print(0)
	elif 'B' in l[r - 1]:
		print(1)
	else:
		for i in l:
			if i[c - 1] == 'B':
				print(1)
				break
		else:
			var = False
			for i in l:
				if 'B' in i:
					var = True
			if var:
				print(2)
			else:
				print(-1)


def solve(matrix, x, y):
	if matrix[x][y] == 'B':
		return 0
	for i in range(len(matrix)):
		if matrix[i][y] == 'B':
			return 1
	for j in range(len(matrix[0])):
		if matrix[x][j] == 'B':
			return 1
	for i in matrix:
		for j in i:
			if j == 'B':
				return 2
	return -1
t = int(input())
for i in range(t):
	(n, m, r, c) = input().split()
	(n, m, r, c) = (int(n), int(m), int(r), int(c))
	matrix = n * [None]
	for j in range(n):
		matrix[j] = list(input())
	print(solve(matrix, r - 1, c - 1))