> File Name: 5725.cpp
> Author: jiangyuzhu
> Mail: 834138558@qq.com
> Created Time: 2016/8/25 13:57:24
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn = 1e3 + 5;
char s[maxn];
int x[maxn];
int y[maxn];
int cntx[maxn];
int cnty[maxn];
int main (void)
{
int T;scanf("%d", &T);
while(T--){
ll tot = 0;
mem(x, -1);mem(cntx, 0);
mem(y, -1);mem(cnty, 0);
int n, m;scanf("%d%d", &n, &m);
getchar();
for(int i = 0; i < n; i++){
gets(s);
for(int j = 0; j < m; j++){
if(s[j] == 'G'){
y[i] = j; x[j] = i;
}else{
cntx[i]++;cnty[j]++;
tot++;
}
}
}
ll ans = 0;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
ans += (j - i) * cntx[i] * cntx[j];
}
}
for(int i = 0; i < m; i++){
for(int j = i + 1; j < m; j++){
ans += (j - i) * cnty[i] * cnty[j];
}
}
ans *= 2;
int cnt = 0;
for(int i = 0; i < n; i++){
if(y[i] > y[i - 1]) cnt += y[i];
else if(y[i] == -1) cnt = 0;
else cnt = y[i];
ans += cnt * 4ll * (m - y[i] - 1);
}
cnt = 0;
for(int i = 0; i < m; i++){
if(x[i] > x[i - 1]) cnt += x[i];
else if(x[i] == -1) cnt = 0;
else cnt = x[i];
ans += cnt * 4ll * (n - x[i] - 1);
}
cnt = 0;
for(int i = n - 1; i >= 0; i--){
if(y[i + 1] > y[i]) cnt = 0;
ans += cnt * 4ll * (m - y[i] - 1);
if(y[i] > y[i + 1]) cnt += y[i];
else if(y[i] == -1) cnt = 0;
else cnt = y[i];
}
cnt = 0;
for(int i = m - 1; i >= 0; i--){
if(x[i + 1] > x[i]) cnt = 0;
ans += cnt * 4ll * (n - x[i] - 1);
if(x[i] > x[i + 1]) cnt += x[i];
else if(x[i] == -1) cnt = 0;
else cnt = x[i];
}
printf("%.4f\n", 1.0 * ans /(double)(tot * tot));
}
return 0;
}