/*
 * mars.java
 *
 * Created on July 20, 2004, 4:01 PM
 */
import java.util.*;
import java.io.*;
import java.lang.*;

/**
 *
 * @author  jgauci
 */
public class mars {
    int bestTime[][];
    boolean been[][];
    int map[][];
    int numSquaresFound;
    int maxTime;
    int numRows,numCols;
    
    
    
    /** Creates a new instance of Distance */
    public mars() throws Exception {

        BufferedReader myReader = new BufferedReader(new FileReader("mars.in"));
        
        
        int numCases = Integer.parseInt(myReader.readLine());
        while((numCases--)>0)
        {
            numRows = Integer.parseInt(myReader.readLine());
            numCols = Integer.parseInt(myReader.readLine());
            maxTime = Integer.parseInt(myReader.readLine());
            map = new int[numRows][numCols];
            bestTime = new int[numRows][numCols];
            been = new boolean[numRows][numCols];
            numSquaresFound = 0;
            for(int c=0;c<numRows;c++)
                Arrays.fill(bestTime[c],Integer.MAX_VALUE);
            
            for(int a=0;a<numRows;a++)
            {
                String tmp = myReader.readLine();
                for(int b=0;b<numCols;b++)
                {
                    map[a][b] = Character.getNumericValue(tmp.charAt(b));
                }
            }
            
            squaresFound(numRows/2,numCols/2,0);
            System.out.println(numSquaresFound);
        }
        
    }
    
    public void squaresFound(int row,int col,int timeSpent)
    {
        if(row<0||col<0||row>=numRows||col>=numCols||timeSpent>=bestTime[row][col])
            return;
        
        bestTime[row][col]=timeSpent;
        
        int newTimeSpent = timeSpent + map[row][col];
        
        if(newTimeSpent>maxTime)
            return;
        
        if(!been[row][col])
        {
            been[row][col]=true;
            numSquaresFound++;
        }
        
        squaresFound(row-1,col,newTimeSpent);
        squaresFound(row+1,col,newTimeSpent);
        squaresFound(row,col-1,newTimeSpent);
        squaresFound(row,col+1,newTimeSpent);
        
        
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        new mars();
    }
    
}
