rt
#define dpp(x) dp[x.bx][x.by][x.r1x][x.r1y][x.r2x][x.r2y]
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 15;
bool a[MAXN][MAXN];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
struct node1 {
int bx, by, r1x, r1y, r2x, r2y;
};
struct Node {
vector<node1> s;
int fl = -1; // -1:不合法, 0:红(帅)必胜, 1:黑(卒)必胜
int f1 = 0, d = -1145141919, c; //深度与入度
int f2 = 0; //是否是被堵死的
int vis = 0;
} dp[MAXN][MAXN][MAXN][MAXN][MAXN][MAXN];
int summ(node1 x) {
return x.bx + x.by + x.r1x + x.r1y + x.r2x + x.r2y;
}
void init() {
for (int i1 = 0; i1 < 11; ++i1)
for (int i2 = 0; i2 < 11; ++i2)
for (int i3 = 0; i3 < 11; ++i3)
for (int i4 = 0; i4 < 11; ++i4)
for (int i5 = 0; i5 < 11; ++i5)
for (int i6 = 0; i6 < 11; ++i6) {
node1 ii = {i1, i2, i3, i4, i5, i6};
dpp(ii).fl = -1, dpp(ii).f1 = 0, dpp(ii).d = -1145141919, dpp(ii).f2 = 0, dpp(ii).c = 0, dpp(ii).vis = 0;
dpp(ii).s.clear();
}
}
int main() {
int id, t;
cin >> id >> t;
while (t--) {
init();
int n, m;
node1 s;
s.r1x = -1;
cin >> n >> m;
for (int i = 0, c(getchar()); i < n; ++i) {
while (c < 35) c = getchar();
for (int j = 0; j < m; ++j) {
a[i][j] = 0;
if (c == '#') {
a[i][j] = 1;
} else if (c == 'O') {
if (s.r1x < 0) {
s.r1x = i, s.r1y = j;
} else {
s.r2x = i, s.r2y = j;
}
} else if (c == 'X') {
s.bx = i, s.by = j;
}
c = getchar();
}
}
queue<node1> q;
queue<pair<node1, int>> q2;
q.push(s);
while (!q.empty()) {
node1 x = q.front();
q.pop();
int f = abs(summ(x) - summ(s)) % 2;
if (f) { // 黑
bool f2 = 1;
for (int i = 1, nx, ny; i < 4; ++i) {
nx = x.bx + dx[i], ny = x.by + dy[i];
if (min(nx, ny) > -1 && nx < n && ny < m && !a[nx][ny]) {
f2 = 0;
node1 nxx = {nx, ny, x.r1x, x.r1y, x.r2x, x.r2y};
dpp(nxx).s.push_back(x); // 建边
++dpp(x).c;
if (dpp(nxx).f1) { // 已经搜到过
continue;
}
dpp(nxx).f1 = 1; // 标记
if ((nx == x.r1x && ny == x.r1y) || (nx == x.r2x && ny == x.r2y) || (nx == 0)) { // 黑必胜
dpp(nxx).fl = 1;
dpp(nxx).d = 0;
q2.push({nxx, 1});
dpp(nxx).vis = 1;
} else {
q.push(nxx);
}
}
}
if (f2) {
dpp(x).fl = 0;
dpp(x).f2 = 1;
dpp(x).d = 0;
q2.push({x, 0});
dpp(x).vis = 1;
}
} else { // 红
bool f2 = 1;
for (int i = 0, nx1, ny1; i < 4; ++i) {
nx1 = x.r1x + dx[i], ny1 = x.r1y + dy[i];
if (min(nx1, ny1) > -1 && nx1 < n && ny1 < m && !a[nx1][ny1] && !(nx1 == x.r2x && ny1 == x.r2y)) {
f2 = 0;
node1 nxx = {x.bx, x.by, nx1, ny1, x.r2x, x.r2y};
dpp(nxx).s.push_back(x); // 建边
++dpp(x).c;
if (dpp(nxx).f1) { // 已经搜到过
continue;
}
dpp(nxx).f1 = 1; // 标记
if (nx1 == x.bx && ny1 == x.by) { // 红必胜
dpp(nxx).fl = 0;
dpp(nxx).d = 0;
q2.push({nxx, 0});
dpp(nxx).vis = 1;
} else {
q.push(nxx);
}
}
}
for (int i = 0, nx2, ny2; i < 4; ++i) {
nx2 = x.r2x + dx[i], ny2 = x.r2y + dy[i];
if (min(nx2, ny2) > -1 && nx2 < n && ny2 < m && !a[nx2][ny2] && !(nx2 == x.r1x && ny2 == x.r1y)) {
f2 = 0;
node1 nxx = {x.bx, x.by, x.r1x, x.r1y, nx2, ny2};
dpp(nxx).s.push_back(x); // 建边
++dpp(x).c;
if (dpp(nxx).f1) { // 已经搜到过
continue;
}
dpp(nxx).f1 = 1; // 标记
if (nx2 == x.bx && ny2 == x.by) { // 红必胜
dpp(nxx).fl = 0;
dpp(nxx).d = 0;
q2.push({nxx, 0});
dpp(nxx).vis = 1;
} else {
q.push(nxx);
}
}
}
if (f2) {
dpp(x).fl = 1;
dpp(x).f2 = 1;
dpp(x).d = 0;
q2.push({x, 1});
dpp(x).vis = 1;
}
}
}
while (!q2.empty()) {
auto x = q2.front();
q2.pop();
// bool f = (abs(summ(x.first) - summ(s)) + dpp(x.first).f2) % 2;
bool f = (abs(summ(x.first) - summ(s))) % 2;
if (x.second == f) {
for (auto nx : dpp(x.first).s) {
if (dpp(nx).vis) {
continue;
}
--dpp(nx).c;
dpp(nx).d = max(dpp(nx).d, dpp(x.first).d + 1);
if (dpp(nx).c == 0) {
dpp(nx).vis = 1;
dpp(nx).fl = x.second;
q2.push({nx, x.second});
}
}
} else {
for (auto nx : dpp(x.first).s) {
if (dpp(nx).vis) {
if (dpp(nx).d > dpp(x.first).d + 1) {
dpp(nx).d = dpp(x.first).d + 1;
}
continue;
}
dpp(nx).vis = 1;
dpp(nx).d = dpp(x.first).d + 1;
dpp(nx).fl = x.second;
q2.push({nx, x.second});
}
}
}
if (dpp(s).fl == 0) {
cout << "Red " << dpp(s).d;
} else if (dpp(s).fl == 1) {
cout << "Black " << dpp(s).d;
} else {
cout << "Tie";
}
putchar(10);
}
return 0;
}