// Arup Guha
// 4/13/2020
// Solution to Code Jam Round 1A Problem: Square Dance
// I wrote this after contest after talking to Travis who pointed out that we only
// have to consider neighbors of removed items for the subsequent round.

import java.util.*;

public class c3 {
	
	public static int[] DX = {-1,0,0,1};
	public static int[] DY = {0,-1,1,0};
	
	public static int nR;
	public static int nC;
	public static int[][] grid;
	public static TreeSet[] rows;
	public static TreeSet[] cols;

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();
		
		// Process cases.
		for (int loop=1; loop<=numCases; loop++) {
		
			nR = stdin.nextInt();
			nC = stdin.nextInt();
			grid = new int[nR][nC];
			rows = new TreeSet[nR];
			cols = new TreeSet[nC];
			
			// Initially all cells are filled.
			for (int i=0; i<nR; i++) {
				rows[i] = new TreeSet<Integer>();
				for (int j=0; j<nC; j++) rows[i].add(j);
			}
			for (int i=0; i<nC; i++) {
				cols[i] = new TreeSet<Integer>();
				for (int j=0; j<nR; j++) cols[i].add(j);
			}
			
			// Read in the grid and store its sum.
			long curSum = 0;
			for (int i=0; i<nR; i++) {
				for (int j=0; j<nC; j++) {
					grid[i][j] = stdin.nextInt();
					curSum += grid[i][j];
				}
			}
			
			// First round sum.
			long res = curSum;
			
			// Initially everyone is a candidate.
			HashSet<Long> candidates = new HashSet<Long>();
			for (int i=0; i<nR; i++)
				for (int j=0; j<nC; j++)
					candidates.add(100000L*i+j);
			
			while (true) {
				
				// Returns locations that got removed.
				HashSet<Long> remList = next(candidates);
				
				// Done!
				if (remList.size() == 0) break;
				
				long remove = 0;
				candidates = new HashSet<Long>();
				
				// Go through everyone who was removed.
				for (Long code: remList) {
					
					// Add value.
					int rowVal = (int)(code/100000);
					int colVal = (int)(code%100000);
					remove += grid[rowVal][colVal];
					
					// Go all 4 sides to add candidates for next round.
					Integer lowc = (Integer)rows[rowVal].lower(colVal);
					if (lowc != null) candidates.add(100000L*rowVal+lowc);
					Integer highc = (Integer)rows[rowVal].higher(colVal);
					if (highc != null) candidates.add(100000L*rowVal+highc);
					Integer lowr = (Integer)cols[colVal].lower(rowVal);
					if (lowr != null) candidates.add(100000L*lowr + colVal);
					Integer highr = (Integer)cols[colVal].higher(rowVal);
					if (highr != null) candidates.add(100000L*highr + colVal);
				}
				
				// Update result for this round. We add all the points except the points for the removed people.
				curSum -= remove;
				res = res + curSum;
			}

			// Ta da!
			System.out.println("Case #"+loop+": "+res);
		}
	}
	
	// Runs an iteration.
	public static HashSet<Long> next(HashSet<Long> candidates) {
		
		HashSet<Long> remList = new HashSet<Long>();
		
		// Go through all candidates who might get removed.
		for (Long code: candidates) {
			
			// Get this candidate.
			int rowVal = (int)(code/100000);
			int colVal = (int)(code%100000);
				
			int nSum = 0, cnt = 0;
				
			// go left
			Integer left = (Integer)rows[rowVal].lower(colVal);
			if (left != null) {
				cnt++;
				nSum += grid[rowVal][left];
			}
			
			// right
			Integer right = (Integer)rows[rowVal].higher(colVal);
			if (right != null) {
				cnt++;
				nSum += grid[rowVal][right];
			}
			
			// up
			Integer up = (Integer)cols[colVal].lower(rowVal);
			if (up != null) {
				cnt++;
				nSum += grid[up][colVal];
			}
			
			// and down
			Integer down = (Integer)cols[colVal].higher(rowVal);
			if (down != null) {
				cnt++;
				nSum += grid[down][colVal];
			}
			
			// Condition for removal - just add to our removal list.
			if (cnt*grid[rowVal][colVal] < nSum) { 
				remList.add(100000L*rowVal+colVal);
			}
		}
		
		// Update our TreeSets...
		for (Long x: remList) {
			int r = (int)(x/100000);
			int c = (int)(x%100000);
			rows[r].remove(c);
			cols[c].remove(r);
		}
		
		// List of removed people.
		return remList;
	}
}