/* Chris Poon
   BHCSI - 2006
   Solution to Lex Returns
   A standard recursion flood fill problem.
   Squares are NOT connected diagonally.
  */

import java.io.*;
import java.util.*;
class lex 
{

	//planetMatrix is character matrix that represents the matrix input.
	public static char planetMatrix[][]=new char[100][100];
	public static int c, m, n;  //the # of cases, rows, columns.

	public static int grow(int i, int j){
		//the recursive method that food fills.

		//here are the terminating base cases.
		if (i<0 || i>=m || j<0 || j>=n) return 0;
		if (planetMatrix[i][j]!='~') return 0;
		
		planetMatrix[i][j]='.';   //<--- prevent infinite recursion!
		int sum=1;
		sum+=grow(i-1, j);
		sum+=grow(i, j-1);
		sum+=grow(i, j+1);
		sum+=grow(i+1, j);
		return sum;
	}

	public static int startGrowth(){
		//the driver method that finds where the
		// entry point is to begin the reaction.
		// returns the number of squares connected
		// which will be at least 1, including itself
		int i,j;
		int sum=1;
		for (i=0;i<m;i++){
			for (j=0;j<n; j++){
				if (planetMatrix[i][j]=='X'){
					sum+=grow(i-1, j);
					sum+=grow(i, j-1);
					sum+=grow(i, j+1);
					sum+=grow(i+1, j);
				}
			}
		}
		return sum;
	}

	public static void main(String[] args) throws IOException
	{
		//reads the input and executes the driver method.
		
		Scanner in=new Scanner(new File("lex.in"));
		c=in.nextInt();

		for (int x=0;x<c; x++){
			m=in.nextInt();
			n=in.nextInt();
			for (int i=0; i<m; i++){
				String oneRow=in.next();
				for (int j=0;j<n; j++)
					planetMatrix[i][j]=oneRow.charAt(j);
			}
			System.out.println(startGrowth()*5); // times 5 to get the square mileage.
		}
		in.close();
	}
}
