//* Problem  : D Youth Hostel Dorm
//* Origin   : NWERC-2007
//* Type     : Judge
//* Date     : 2009.07.26
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 8.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
#include "testlib.h"

using namespace std;

int n, m, k, nt, it;

vector<string> a, r;

int dx4[] = {-1, 0, 1, 0};
int dy4[] = {0, -1, 0, 1};

int f[32][32];

void check(vector<string> r, int k)
{
	memset(f, 0, sizeof(f));
	int x0, y0, cb = 0, ce = 0, cd = 0;
	if (r.size() != n)
		quitf(_wa, "Test %d. Wrong dimenions", it);
	for (int i = 0; i < r.size(); i++)
	{
		if (r[i].length() != m)
			quitf(_wa, "Test %d. Wrong dimenions", it);
		for (int j = 0; j < r[i].length(); j++)
		{
			switch(r[i][j])
			{
			case 'E':
				ce++; x0 = i; y0 = j; break;

			case 'B':
				cb++; break;

			case '.':
				cd++; break;

			default:
				quitf(_wa, "Test %d. Wrong character in output: %c", it, r[i][j]);
			}
		}
	}
	if (ce != 1)
		quitf(_wa, "Test %d. Wrong number of entrances: %d", ce);
	if (cb != k)
		quitf(_wa, "Test %d. Wrong number of beds: %d. Judges found: %d", it, cb, k);
	if (x0 != 0 && x0 != n - 1 && y0 != 0 && y0 != m - 1)
		quitf(_wa, "Test %d. Wrong entrance placement: %d %d", it, x0, y0);
	r[x0][y0] = '.';
	queue<pair<int, int > > q;
	q.push(make_pair(x0, y0)); f[x0][y0] = 1;
	int res = 0;
	while (!q.empty())
	{
		pair<int, int> t = q.front(); q.pop();
		int x = t.first, y = t.second;
		for (int k = 0; k < 4; k++)
		{
			int xx = x + dx4[k];
			int yy = y + dy4[k];
			if (xx < 0 || xx >= n) continue;
			if (yy < 0 || yy >= m) continue;
			if (f[xx][yy]) continue;
			if (r[xx][yy] == '.')
			{
				q.push(make_pair(xx, yy));
			}
			else if (r[xx][yy] == 'B')
			{
				res++;
			}
			f[xx][yy] = 1;
		}
	}
	if (res != k)
		quitf(_wa, "Test %d. Not all beds are achievable", it);
}

int main(int argc, char *argv[])
{
	registerTestlibCmd(argc, argv);
	
	nt = inf.readInt();
	for (it = 1; it <= nt; it++)
	{
		n = inf.readInt();
		m = inf.readInt();
		k = 0;
		a.clear();
		r.clear();
		for (int i = 0; i < n; i++)
		{
			a.push_back(ouf.readString());
			r.push_back(ans.readString());
			string s = r[r.size()-1];
			for (int j = 0; j < s.length(); j++)
				if (s[j] == 'B')
					k++;
		}
		check(a, k);
	}
	quitf(_ok, "OK. %d tests", nt);
}
