            #include <bits/stdc++.h>
            #include <ext/pb_ds/assoc_container.hpp>
            #include <ext/pb_ds/tree_policy.hpp>
            using namespace std;
            using namespace __gnu_pbds;
            #define rep(i,a,b) for(ll i =a; i<b;++i)
            #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
            #define endl "\n"
            #define pb push_back
            #define F first
            #define S second
            #define print(a)       for(auto x : a) cout << x << " "; cout << endl
            #define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
            #define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
            #define mk(x,y)  make_pair(x,y)
            typedef  long long int ll;
            const ll N =  1e2+ 5 ;
            const ll mod = 1e9+7 ;
            ll n , m ,ans[N][N] ,vis[N][N];
            char a[15][15] ;
            ll dx[] = { -1 ,0 , 0, 1};
            ll dy[] = { 0 ,-1, 1, 0};
    ll pow_mod_m(ll a , ll n)
    {
        if(!n)
            return 1;
        ll pt =  pow_mod_m(a,n/2);
        pt *= pt ;
        if(n&1)
            pt *= a;
        return pt;
    }

    /*bool comp(const ll &p1, const ll &p2)
    {
        if(order[p1] == order[p2])
            return p2 > p1;
        return order[p1] > order[p2];
    }*/


    int main()
    {
         IOS;
         int ts =1;
         cin >> ts;
         vector < pair <ll , ll> > v;
         while(ts --)
         {
            memset(vis , 0, sizeof(vis));
            cin >> n >> m;
            rep(i,1,n+1) rep(j,1,m+1) ans[i][j] = mod;
            rep(i,1,n+1)
            {
                rep(j,1,m+1) {cin >> a[i][j] ;
                if(a[i][j] =='B') ans[i][j] = 0,v.pb(mk(i,j));
                if(a[i][j] == 'W') ans[i][j] = -1;
                }
            }
            queue < pair <pair <ll,ll>,ll > > q;
            for(auto u : v) q.push(mk(u,0)) , vis[u.F][u.S] = 1;

            while(q.empty() == false)
            {
                auto p = q.front();
                ///cout << p.F.F << " " << p.F.S << endl;
                q.pop();
                rep(i,0,4)
                {
                        if(p.F.F + dx[i] >=  1 and p.F.S + dy[i] >=1 and p.F.F + dx[i] <=  n and p.F.S + dy[i] <= m and !vis[p.F.F + dx[i]][p.F.S+dy[i]] and a[p.F.F+dx[i]][p.F.S+dy[i]] != 'W' )
                        {
                            auto z = mk(mk(p.F.F + dx[i] , p.F.S + dy[i] ) , p.S+1);
                            ans[p.F.F+dx[i]][p.F.S+dy[i]] = p.S + 1;
                            q.push(z);
                            vis[p.F.F +dx[i]][p.F.S+dy[i]] = 1;
                        }
                        else
                        {
                            ans[p.F.F +dx[i]][p.F.S + dy[i]] = min(ans[p.F.F +dx[i]][p.F.S + dy[i]],p.S + 1);
                        }
                }
            }
            rep(i,1,n+1)
            {
                rep(j,1,m+1)
                {
                    if(ans[i][j] == mod) cout << -1 << " ";
                    else cout << ans[i][j] << " ";
                }
                cout << endl;
            }
            v.clear();
         }
    }
