/*
 * Fire.java
 *
 * Created on July 2, 2004, 6:14 PM
 */
import java.io.*;
import java.util.*;
import java.lang.*;
/**
 *
 * @author  jgauci
 */
public class Fire {
    int matrix[][];
    int numRows,numCols;
    
    /** Creates a new instance of Fire */
    public Fire() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("Fire.in"));
        int numCases = Integer.parseInt(br.readLine());
        while((numCases--)>0) {
            String s = br.readLine();
            StringTokenizer st = new StringTokenizer(s);
            numRows = Integer.parseInt(st.nextToken());
            numCols = Integer.parseInt(st.nextToken());
            matrix = new int[numRows][numCols];
            for(int a=0;a<numRows;a++) {
                String s2 = br.readLine();
                for(int b=0;b<numCols;b++) {
                    if(s2.charAt(b)=='*')
                        matrix[a][b]=-1;
                    else
                        matrix[a][b]=s2.charAt(b) - '0';
                }
            }
            
            for(int y=0;y<matrix.length;y++)
                for(int x=0;x<matrix[y].length;x++) {
                    floodBurn(y,x);
                }
            
            for(int g=0;g<numRows;g++) {
                for(int h=0;h<numCols;h++)
                    if(matrix[g][h]==-1)
                        System.out.print("*");
                    else
                        System.out.print(matrix[g][h]);
                System.out.println();
            }
            System.out.println();
        }
    }
    
    public void floodBurn(int row,int col) {
        if(row<0||col<0||row>=matrix.length||col>=matrix[0].length)
            return;
        
        if(matrix[row][col]==-1)
            return;
        
        int count=0;
        
        if(row>0&&matrix[row-1][col]==-1)
            count++;
        
        if(col>0&&matrix[row][col-1]==-1)
            count++;
        if(col<(numCols-1)&&matrix[row][col+1]==-1)
            count++;
        
        if(row<(numRows-1)&&matrix[row+1][col]==-1)
            count++;
        
        if(count>=matrix[row][col]) {
            matrix[row][col]=-1;
            
            floodBurn(row-1,col);
            
            floodBurn(row,col-1);
            floodBurn(row,col+1);
            
            floodBurn(row+1,col);
        }
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        new Fire();
    }
    
}
