#include <bits/stdc++.h>
#define rep(i, l, r) for (int i (l); i <= r; ++ i)
#define rrp(i, l, r) for (int i (r); i >= l; -- i)
#define pii pair <int, int>
#define eb emplace_back
#define ls p << 1
#define rs ls | 1
#define inf 1000000000
using namespace std;
constexpr int N = 100 + 5;
typedef unsigned long long ull;
typedef long long ll;
inline ll rd () {
ll x = 0, f = 1;
char ch = getchar ();
while (! isdigit (ch)) {
if (ch == '-') f = -1;
ch = getchar ();
}
while (isdigit (ch)) {
x = (x << 1) + (x << 3) + ch - 48;
ch = getchar ();
}
return x * f;
}
int n, m;
char s[N][N];
int id[N][N][2];
vector <int> e[N * N << 1];
int col[N * N << 1], match[N * N << 1];
int vis[N * N << 1];
int stk[N * N << 1], top;
int chg[N * N << 1];
bool dfs (int u) {
for (auto v : e[u]) {
if (vis[v]) continue;
vis[v] = 1; stk[++ top] = v;
if (! match[v] || dfs (match[v])) return match[v] = u, 1;
} return 0;
}
void dfs (int u, int now) {
if (col[u] != -1) return ;
col[u] = now;
for (auto v : e[u]) dfs (v, ! now);
}
queue <int> q;
int p[N];
void solve () {
n = rd (), m = rd ();
rep (i, 1, n) scanf ("%s", s[i] + 1);
rep (i, 1, n) rep (j, 1, m) rep (k, 0, 1) id[i][j][k] = 0;
int tot = 0, ans = 0;
rep (i, 1, n - 1) {
rep (j, 1, m - 1) {
if (s[i][j] == '?' || s[i][j + 1] == '?' || s[i + 1][j] == '?' || s[i + 1][j + 1] == '?') {
if (s[i][j] != 'W' && s[i + 1][j] != 'B' && s[i][j + 1] != 'B' && s[i + 1][j + 1] != 'W') id[i][j][0] = ++ tot;
if (s[i][j] != 'B' && s[i + 1][j] != 'W' && s[i][j + 1] != 'W' && s[i + 1][j + 1] != 'B') id[i][j][1] = ++ tot;
} else {
ans += s[i][j] == s[i + 1][j + 1] && s[i][j] != s[i + 1][j] && s[i][j] != s[i][j + 1];
}
}
}
rep (i, 1, n - 1) {
rep (j, 1, m - 1) {
rep (k, 0, 1) {
if (! id[i][j][k]) continue;
if (id[i][j][! k]) e[id[i][j][k]].eb (id[i][j][! k]);
if (id[i][j + 1][k]) e[id[i][j][k]].eb (id[i][j + 1][k]), e[id[i][j + 1][k]].eb (id[i][j][k]);//,cout<<id[i][j][k]<<" "<<id[i][j+1][k]<<endl;
if (id[i + 1][j][k]) e[id[i][j][k]].eb (id[i + 1][j][k]), e[id[i + 1][j][k]].eb (id[i][j][k]);//,cout<<id[i][j][k]<<" "<<id[i+1][j][k]<<endl;
if (id[i + 1][j + 1][! k]) e[id[i][j][k]].eb (id[i + 1][j + 1][! k]), e[id[i + 1][j + 1][! k]].eb (id[i][j][k]);//,cout<<id[i][j][k]<<" "<<id[i+1][j+1][!k]<<endl;
if (id[i + 1][j - 1][! k]) e[id[i][j][k]].eb (id[i + 1][j - 1][! k]), e[id[i + 1][j - 1][! k]].eb (id[i][j][k]);
}
}
}
fill (col + 1, col + tot + 1, -1);
mt19937 rnd (time (0));
rep (i, 1, tot) p[i] = i;
shuffle (p + 1, p + tot + 1);
rep (i, 1, tot) shuffle (e[i].begin (), e[i].end (), rnd);
rep (i, 1, tot) {
if (col[i] == -1) dfs (i, 0);
}
rep (i, 1, tot) {
if (! col[p[i]]) ans -= dfs (p[i]);
while (top) vis[stk[top --]] = 0;
}
rep (i, 1, tot) if (col[i] && match[i]) match[match[i]] = i;
rep (i, 1, tot) vis[i] = -1;
rep (i, 1, tot) {
if (! match[i]) {
vis[i] = 1;
for (auto v : e[i]) if (vis[v] == -1) vis[v] = 0, q.push (v);
}
}
while (! q.empty ()) {
int u = q.front ();
q.pop ();
int v = match[u];
if (vis[v] == -1) {
vis[v] = 1;
for (auto w : e[v]) {
if (vis[w] == -1) vis[w] = 0, q.push (w);
}
}
}
rep (i, 1, tot) if (vis[i] == -1 && vis[match[i]] == -1 && col[i] == 0) vis[i] = 1;
rep (i, 1, n - 1) {
rep (j, 1, m - 1) {
if (vis[id[i][j][0]] == 1) s[i][j] = 'B', s[i + 1][j] = 'W', s[i][j + 1] = 'W', s[i + 1][j + 1] = 'B';
if (vis[id[i][j][1]] == 1) s[i][j] = 'W', s[i + 1][j] = 'B', s[i][j + 1] = 'B', s[i + 1][j + 1] = 'W';
}
}
printf ("%d\n", ans + tot);
rep (i, 1, n) {
rep (j, 1, m) if (s[i][j] != '?') putchar (s[i][j]); else putchar ('B');
puts ("");
}
rep (i, 1, tot) e[i].clear (), match[i] = 0, vis[i] = 0;
}
int main () {
// freopen ("1.in", "r", stdin);
// freopen ("1.out", "w", stdout);
for (int T (rd ()); T; -- T) solve ();
}