from collections import deque
def bfs_chao(chaoping, n, m, k):
directions = [(0, 1), (0, -1), (-1, 0), (1, 0)]
# 初始化包含所有 'g' 的集合
chao_g = set()
for i in range(n):
for j in range(m):
if chaoping[i][j] == "g":
chao_g.add((i, j))
queue = deque(chao_g)
result = [list(row) for row in chaoping] # 深拷贝二维字符列表
for _ in range(k):
for _ in range(len(queue)):
x, y = queue.popleft()
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < n and 0 <= ny < m and result[nx][ny] == '.':
result[nx][ny] = "g"
queue.append((nx, ny))
return result
# 输入处理
n, m = map(int, input().strip().split())
chaoping = [list(input().strip()) for _ in range(n)] # 每行读取为字符列表
k = int(input())
# 获取 BFS 结果并打印
result = bfs_chao(chaoping, n, m, k)
for row in result:
print(''.join(row))