1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>

using namespace std;
const int MAXN = 500;

int n, m;
int tab[MAXN + 5][MAXN + 5];

void gora()
{
	for (int i = 0; i < m; i++)
	{
		int l = 0;
		for (int j = 0; j < n; j++)
		{
			if (tab[j][i] != 0)
			{
				if (j != l)
				{
					tab[l][i] = tab[j][i];
					tab[j][i] = 0;
				}
				l++;
			}
		}
	}
}

void dol()
{
	for (int i = 0; i < m; i++)
	{
		int l = n - 1;
		for (int j = n - 1; j >= 0; j--)
		{
			if (tab[j][i] != 0)
			{
				if (j != l)
				{
					tab[l][i] = tab[j][i];
					tab[j][i] = 0;
				}
				l--;
			}
		}
	}
}

void lewo()
{
	for (int i = 0; i < n; i++)
	{
		int l = 0;
		for (int j = 0; j < m; j++)
		{
			if (tab[i][j] != 0)
			{
				if (j != l)
				{
					tab[i][l] = tab[i][j];
					tab[i][j] = 0;
				}
				l++;
			}
		}
	}
}

void prawo()
{
	for (int i = 0; i < n; i++)
	{
		int l = m - 1;
		for (int j = m - 1; j >= 0; j--)
		{
			if (tab[i][j] != 0)
			{
				if (j != l)
				{
					tab[i][l] = tab[i][j];
					tab[i][j] = 0;
				}
				l--;
			}
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m;
	string s;
	for (int i = 0; i < n; i++)
	{
		cin >> s;
		for (int j = 0; j < m; j++)
		{
			if (s[j] == '.') tab[i][j] = 0;
			else if (s[j] == 'B') tab[i][j] = 1;
			else if (s[j] == 'C') tab[i][j] = 2;
		}
	}
	int k;
	cin >> k >> s;
	for (int i = 0; i < k; i++)
	{
		if (s[i] == 'G') gora();
		else if (s[i] == 'D') dol();
		else if (s[i] == 'L') lewo();
		else if (s[i] == 'P') prawo();
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (tab[i][j] == 0) cout << '.';
			else if (tab[i][j] == 1) cout << 'B';
			else if (tab[i][j] == 2) cout << 'C';
		}
		cout << "\n";
	}

	return 0;
}