Program Ponork;

type radekpole = array[1..300] of byte;
var Pole: array[0..150] of radekpole;
	vstupu, tahu, radek, sloupcu, i, j, k, ra, sl: integer;
	ch: char;
    hledam: byte;
	body: integer;
	nasel: integer;
	kamenu: integer;
	
Procedure Vypln(x, y: integer);
begin
  if not (Pole[x, y]=hledam) then exit;
  Pole[x, y]:=0;
  Dec(Pole[0, y]);
  Dec(kamenu);
  Inc(nasel);
  if x>1 then Vypln(x-1, y);
  if x<radek then Vypln(x+1, y);
  if y>1 then Vypln(x, y-1);
  if y<sloupcu then Vypln(x, y+1);
end;

Procedure Setres;
var i, j: integer;
    spodniindex: integer;
begin
  for i:=sloupcu downto 1 do 
  if Pole[0, i]>0
  then begin
    spodniindex:=0; 
    for j:=radek downto 1
	do begin
      if (Pole[j, i]>0) and (spodniindex>0)
	  then begin
	    Pole[spodniindex, i]:=Pole[j, i];
		Dec(spodniindex);
      end
	  else spodniindex:=j;
 	end; 
  end;
  For j:=1 to sloupcu-1 
  do if Pole[0, j]=0
  then begin
    Pole[0, j]:=Pole[0, j+1];
	Pole[0, j+1]:=0;
  end;
  
end;


begin
  readln(vstupu);
  for i:=1 to vstupu
  do begin
    readln( radek, sloupcu);
	body:=0;
    for j:=1 to 150 do for k:=1 to 300 do Pole[j, k]:=0;
	for j:=1 to 300 do Pole[0, j]:=radek;
    for j:=1 to radek
	do begin
      for k:=1 to sloupcu 
	  do begin
        read(ch);
    	  Pole[j, k]:=ord(ch);
      end;
	  readln;
    end;
    kamenu:=radek*sloupcu;
	
	readln(tahu);
	for j:=1 to tahu
	do begin
      readln(ra,sl);
	  hledam:=Pole[ra, sl];
	  nasel:=0;
	  if hledam>0 then 
	  Vypln(ra, sl);  
	  if nasel>0 then Setres;
	  body:=body+sqr(nasel-2);
	end;
    
    if kamenu=0 
	then Inc(body, 1000);
	
    writeln('Game over!');
	writeln('Score dosazene v teto hre je ', body, ', bodu.');
	writeln('Byli bychom radi, kdybyste si zahrali jeste jednou.');
	writeln('Prejete si hrat znovu?');
	writeln('Prijemnou zabavu Vam preje firma ACMTENDO.');
  end;
  
end.
