#include<bits/stdc++.h>
using namespace std;
const int M = 1005;
struct PII{
int first;
int second;
};
PII tempPair;//临时结点
//这里不能写map,因为会与库函数map冲突
char Map[M][M];
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int n; //n行
int m; //m列
int k; //k次,这里明确了退出条件就无需再用check了
queue<PII> q; //广搜用到队列
int len; // 记录结点数量,方便后续计算k
bool pd(int x, int y){
if(x<1||x>n||y<1||y>n||Map[x][y]=='g') return false;
return true;
}
void bfs(){
while(!q.empty()&&k>0){
tempPair = q.front();
q.pop();
int x = tempPair.first;
int y = tempPair.second;
for(int i = 0; i < 4; i++){
int nowx = dx[i]+x;
int nowy = dy[i]+y;
if(!pd(nowx,nowy)) continue;
q.push({nowx,nowy});
Map[nowx][nowy] = 'g';
}
len--;//每取出一个结点就--
if(len==0){
//当len==0表示当前月扩展完成,k--
k--;
len = q.size(); //继续赋值下一层结点数
}
}
}
int main(){
cin>>n>>m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin>>Map[i][j];
if(Map[i][j]=='g'){
tempPair.first = i;
tempPair.second = j;
q.push(tempPair); //初始树结点加入队列
}
}
}
len = q.size();//记录第一层的结点数方便后续k的计算
cin>>k;
bfs();//广搜调整好长了k个月后的草分布图
//输出长了k个月后的草分布图
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cout<<Map[i][j];
}
cout<<endl;
}
return 0;
}