program Dwite5 (input,output);
 var map: array [1..5,1..10,1..10] of char;
     l, w: array [1..5] of integer;
     water: array [1..5] of integer;
     totA: array [1..5] of integer;
     subA: array [1..5] of integer;
     previous: char;
     DATA5, OUT5: text;
     i,j,k: integer;
     settle: boolean;
     x,y: integer;

 begin
   assign (DATA5,'DATA5.txt');
   reset (DATA5);
   assign (OUT5,'OUT5.txt');
   rewrite (OUT5);
   for i:= 1 to 5 do
    begin
        totA[i]:=0;
        subA[i]:=0;
        readln (DATA5, water[i]);
        readln (DATA5, w[i]);
        readln (DATA5, l[i]);
        for y:=1 to l[i] do
         begin
          for x:= 1 to w[i] do
           begin
            read (DATA5,map[i,y,x]);

            if (map[i,y,x]='A') then
             totA[i]:=totA[i]+1;
           end;
          readln (DATA5);
         end;
       readln (DATA5);
    end;

   for i:= 1 to 5 do
    begin
     for j:=1 to water[i] do
      begin
        settle:=false;
        x:=1;
        y:=1;
        map[i,1,1]:='*';
        previous:='.';
        repeat
         if (map[i,y+1,x]<>'#') and (map[i,y+1,x]<>'*') and (y+1<=l[i]) then
          begin
            map[i,y,x]:=previous;
            previous:=map[i,y+1,x];
            map[i,y+1,x]:='*';
            y:=y+1;
          end
         else
          if (map[i,y,x+1]<>'#') and (map[i,y,x+1]<>'*')and (x+1<=w[i]) then
           begin
            map[i,y,x]:=previous;
            previous:=map[i,y,x+1];
            map[i,y,x+1]:='*';
            x:=x+1;
           end
          else
             if (x<=w[i]) and (y<=l[i]) then
              settle:=true;
         until (settle=true);
        end;
     end;
     for i:=1 to 5 do
      begin
       for y:=1 to l[i] do
        begin
         for x:=1 to w[i] do
           begin

             if (map[i,y,x]='A') then
               subA[i]:=subA[i]+1;
           end;
         end;
       subA[i]:=totA[i]-subA[i];
      end;

     for i:= 1 to 5 do
      begin
       writeln (OUT5, subA[i]);
      end;
     close(OUT5);
  end.

























