https://www.lanqiao.cn/courses/31016/learning/?id=1898133&compatibility=false
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
typedef vector<long long> VI;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#define pb(i) push_back(i)
#define int long long
#define INF 0x3f3f3f3f
#define oz 998244353
#define endl '\n'
#define N 1010
const int mod = 1e9 + 7;
int dx[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int p[N], si[N];
int find(int x) {
if (x != p[x]) p[x] = find(p[x]);
return p[x];
}
//size[find(b)] += size[find(a)];
//p[find(a)] = find(b);
int n, m, k;
char g[N][N];
queue<PII> q;
void solve() {
cin >> n >> m;
rep(i, 1, n)
rep(j, 1, m) {
cin >> g[i][j];
if (g[i][j] == 'g') {
q.push({i, j});
}
}
int len = q.size();
cin >> k;
while (!q.empty() && k) { // BFS 并记录层数
auto t = q.front();
q.pop();
rep(i, 0, 3) {
int x = dx[i][0] + t.first, y = dx[i][1] + t.second;
if (x < 1 || x > n || y < 1 || y > m || g[x][y] == 'g')continue;
g[x][y] = 'g';
q.push({x, y});
}
len --;
if (!len) {
k --;
len = q.size(); // len更新为下一层的层数
}
}
rep(i, 1, n) {
rep(j, 1, m) {
cout << g[i][j];
}
cout << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while (T --)
solve();
return 0;
}