#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define _fi first
#define _se second
using namespace std;
const int N = 1e3;
int n, m, a[N + 3][N + 3];
int cnt;
vector<pair<int, int> >blk[N * N + 3]; /ock
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
bool lim(int x, int y){
if(x < 1 || x > n || y < 1 || y > m)
return false;
return true;
}
void bfs(int sx, int sy){
queue<pair<int, int> >que;
que.push(make_pair(sx, sy));
int ux, uy, vx, vy;
while(!que.empty()){
ux = que.front().first;
uy = que.front().second;
que.pop();
// printf("u:(%d,%d)\n", ux, uy);
for(int t = 0; t < 4; t++){
vx = ux + dx[t];
vy = uy + dy[t];
if(!lim(vx, vy) || a[vx][vy] != 0)
continue;
// printf("v(%d,%d)\n", vx, vy);
a[vx][vy] = a[ux][uy];
blk[a[ux][uy]].emplace_back(vx, vy);
que.push(make_pair(vx, vy));
}
}
}
bool cov[N * N + 3]; //covered
void sol(){
scanf("%d%d\n", &n, &m);
for(int i = 1; i <= n; i++){
char ch;
for(int j = 1; j <= m; j++){
ch = getchar();
if(ch == '.')
a[i][j] = 0;
else
a[i][j] = -1;
}
getchar();
}
cnt = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(a[i][j] == 0){
cnt++;
blk[cnt].clear();
a[i][j] = cnt;
blk[cnt].emplace_back(i, j);
bfs(i, j);
}
// printf("%d %d\n", n, m);
// for(int i = 1; i <= n; i++){
// for(int j = 1; j <= m ;j++)
// printf("%3d", a[i][j]);
// printf("\n");
// }
memset(cov, 0, (cnt + 1) * sizeof(bool));
for(int px = 1 - n; px <= n - 1; px++){
for(int py = 1 - m; py <= m - 1; py++){
if(px == 0 && py == 0)
continue;
for(int i = 1; i <= cnt; i++)
if(!cov[i]){
int col = 0;
for(int j = 0, nx, ny; j < blk[i].size() && col != -1; j++){
nx = blk[i][j]._fi - px;
ny = blk[i][j]._se - py;
if(!lim(nx, ny) || a[nx][ny] == -1)
col = -1;
else{
if(col == 0)
col = a[nx][ny];
else {
if(col != a[nx][ny])
col = -1;
}
}
}
if(col > 0){
// printf("block%d covered at p(%d,%d)\n", i, px, py);
cov[i] = true;
}
}
}
}
int ans = 0;
for(int i = 1; i <= cnt; i++)
if(!cov[i])
ans += blk[i].size();
printf("%d\n", ans);
// printf("\n");
}
int main(){
// freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
int T;
scanf("%d\n", &T);
while(T--)
sol();
return 0;
}