import java.util.*;
import java.io.*;

public class life
{
	static boolean isLiving(char[][] grid, int size, int i, int j)
	{
		if (i >= 0 && j >= 0 && i < size && j < size)
			if (grid[i][j] == '#')
				return true;
		return false;
	}

	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(new File("life.in"));
		
		int numcases = sc.nextInt();
		
		for (int cnt=1; cnt<= numcases; cnt++) {

			System.out.println("Simulation #"+cnt+":\n");		

			int size = sc.nextInt();
			int numiter = sc.nextInt();

			char[][] grid = new char[size][size];

			// Initialize the grid from the file 
			for (int i = 0; i < size; i++)
			{
				String s = sc.next();
				for (int j = 0; j < size; j++)
					grid[i][j] = s.charAt(j);
			}

			// Do numiter iterations
			for (int n = 0; n < numiter; n++)
			{
				char[][] nextgrid = new char[size][size];

				for (int i = 0; i < size; i++)
				{
					for (int j = 0; j < size; j++)
					{
						// Count the neighbors
						int c = 0;
					
						if (isLiving(grid,size,i-1,j-1)) c++;
						if (isLiving(grid,size,i-1,j)) c++;
						if (isLiving(grid,size,i-1,j+1)) c++;
						if (isLiving(grid,size,i,j-1)) c++;
						if (isLiving(grid,size,i,j+1)) c++;
						if (isLiving(grid,size,i+1,j-1)) c++;
						if (isLiving(grid,size,i+1,j)) c++;
						if (isLiving(grid,size,i+1,j+1)) c++;

						// These are the four game rules
						if (isLiving(grid,size,i,j))
							if (c < 2 || c > 3)
								nextgrid[i][j] = '.';
							else
								nextgrid[i][j] = '#';
						else
						if (c == 3)
							nextgrid[i][j] = '#';
						else
							nextgrid[i][j] = '.';

						// Display the next generation
						System.out.print(nextgrid[i][j]);
					}
					System.out.println();
				}
				System.out.println();

				// The new generation is now the currrent one
				grid = nextgrid;
			}
			
			
		}				
	}
}