#include <iostream>
#include <vector>
using namespace std;
int r, c;
char m[35][35];
vector <int> v;
void read()
{
cin>>r>>c;
for (int i=0; i<=r+1; i++)
{
for (int j=0; j<=c+1; j++)
{
if (i==0 || i==r+1 || j==0 || j==c+1)
m[i][j] = 'X';
else
{
cin>>m[i][j];
}
}
}
int n;
cin>>n;
for (int i=0; i<n; i++)
{
int a;
cin>>a;
v.push_back(a);
}
}
int findWay(int i, int j)
{
if (m[i+1][j] == 'X')
{
m[i][j] = 'O';
return 1;
}
else if (m[i+1][j] == '.')
{
findWay(i+1, j);
}
else if (m[i+1][j] == 'O')
{
if (m[i][j-1] == '.' && m[i+1][j-1] == '.')
{
findWay(i+1, j-1);
return 1;
}
else if (m[i][j+1]=='.' && m[i+1][j+1] == '.')
{
findWay(i+1, j+1);
return 1;
}
else
{
m[i][j] = 'O';
return 1;
}
}
}
void out()
{
for (int i=1; i<=r; i++)
{
for (int j=1; j<=c; j++)
{
cout<<m[i][j];
}
cout<<endl;
}
}
void check()
{
cout<<r<<" "<<c<<endl;
for (int i=0; i<=r+1; i++)
{
for (int j=0; j<=c+1; j++)
{
cout<<m[i][j];
}
cout<<endl;
}
cout<<v.size()<<endl;
for (int i=0; i<v.size(); i++)
cout<<v[i]<<endl;
}
int main()
{
// freopen("input.txt", "r", stdin);
read();
for (int i=0; i<v.size(); i++)
{
findWay(1, v[i]);
}
out();
// check();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IHIsIGM7CmNoYXIgbVszNV1bMzVdOwp2ZWN0b3IgPGludD4gdjsKCnZvaWQgcmVhZCgpCnsKICAgIGNpbj4+cj4+YzsKICAgIGZvciAoaW50IGk9MDsgaTw9cisxOyBpKyspCiAgICB7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPD1jKzE7IGorKykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChpPT0wIHx8IGk9PXIrMSB8fCBqPT0wIHx8IGo9PWMrMSkKICAgICAgICAgICAgICAgIG1baV1bal0gPSAnWCc7CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY2luPj5tW2ldW2pdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIGludCBuOwogICAgY2luPj5uOwogICAgZm9yIChpbnQgaT0wOyBpPG47IGkrKykKICAgIHsKICAgICAgICBpbnQgYTsKICAgICAgICBjaW4+PmE7CiAgICAgICAgdi5wdXNoX2JhY2soYSk7CiAgICB9Cn0KCmludCBmaW5kV2F5KGludCBpLCBpbnQgaikKewogICAgaWYgKG1baSsxXVtqXSA9PSAnWCcpCiAgICB7CiAgICAgICAgbVtpXVtqXSA9ICdPJzsKICAgICAgICByZXR1cm4gMTsKICAgIH0KICAgIGVsc2UgaWYgKG1baSsxXVtqXSA9PSAnLicpCiAgICB7CiAgICAgICAgZmluZFdheShpKzEsIGopOwogICAgfQogICAgZWxzZSBpZiAobVtpKzFdW2pdID09ICdPJykKICAgIHsKICAgICAgICBpZiAobVtpXVtqLTFdID09ICcuJyAmJiBtW2krMV1bai0xXSA9PSAnLicpCiAgICAgICAgewogICAgICAgICAgICBmaW5kV2F5KGkrMSwgai0xKTsKICAgICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYgKG1baV1baisxXT09Jy4nICYmIG1baSsxXVtqKzFdID09ICcuJykKICAgICAgICB7CiAgICAgICAgICAgIGZpbmRXYXkoaSsxLCBqKzEpOwogICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgbVtpXVtqXSA9ICdPJzsKICAgICAgICAgICAgcmV0dXJuIDE7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIG91dCgpCnsKICAgIGZvciAoaW50IGk9MTsgaTw9cjsgaSsrKQogICAgewogICAgICAgIGZvciAoaW50IGo9MTsgajw9YzsgaisrKQogICAgICAgIHsKICAgICAgICAgICAgY291dDw8bVtpXVtqXTsKICAgICAgICB9CiAgICAgICAgY291dDw8ZW5kbDsKICAgIH0KfQoKdm9pZCBjaGVjaygpCnsKICAgIGNvdXQ8PHI8PCIgIjw8Yzw8ZW5kbDsKICAgIGZvciAoaW50IGk9MDsgaTw9cisxOyBpKyspCiAgICB7CiAgICAgICAgZm9yIChpbnQgaj0wOyBqPD1jKzE7IGorKykKICAgICAgICB7CiAgICAgICAgICAgIGNvdXQ8PG1baV1bal07CiAgICAgICAgfQogICAgICAgIGNvdXQ8PGVuZGw7CiAgICB9CiAgICBjb3V0PDx2LnNpemUoKTw8ZW5kbDsKICAgIGZvciAoaW50IGk9MDsgaTx2LnNpemUoKTsgaSsrKQogICAgICAgIGNvdXQ8PHZbaV08PGVuZGw7Cn0KCmludCBtYWluKCkKewovLyAgICBmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAgIHJlYWQoKTsKICAgIGZvciAoaW50IGk9MDsgaTx2LnNpemUoKTsgaSsrKQogICAgewogICAgICAgIGZpbmRXYXkoMSwgdltpXSk7CiAgICB9CiAgICBvdXQoKTsKLy8gICAgY2hlY2soKTsKICAgIHJldHVybiAwOwp9