program GO;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var a,b,c: array [0..20,0..20] of integer;
    t,k,n,i,j,cc,kol:integer;
    s:string;


PROCEDURE proverka(x,y:integer);
begin
  if b[x,y]=1 then begin

  b[x,y]:=0;
  if a[x+1,y]=1 then begin cc:=cc+1; a[x+1,y]:=0 end; if a[x+1,y]=2 then proverka(x+1,y);
  if a[x,y+1]=1 then begin cc:=cc+1; a[x,y+1]:=0 end; if a[x,y+1]=2 then proverka(x,y+1);
  if a[x-1,y]=1 then begin cc:=cc+1; a[x-1,y]:=0 end; if a[x-1,y]=2 then proverka(x-1,y);
  if a[x,y-1]=1 then begin cc:=cc+1; a[x,y-1]:=0 end; if a[x,y-1]=2 then proverka(x,y-1);
  end;

end;

begin
reset(input,'GO.IN');
//rewrite(output,'output.txt');

readln(t);

for k := 1 to t do begin
//

fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
kol:=0;

readln(n);

for i := 1 to n do begin
  readln(s);
  for j := 1 to n do begin
   if s[j]='.' then a[i,j]:=1;
    if s[j]='B' then begin a[i,j]:=2; b[i,j]:=1; end;
  end;
end;


for i := 1 to n do
  for j := 1 to n do begin
    cc:=0;
    c:=a;
    proverka(i,j);
    a:=c;
    if cc=1 then inc(kol);
  end;

writeln(kol);






//
end;
end.
