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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 500 + 5;
const int MAXD = 5e5 + 5;

int n,m;

int gettype(char c)
{
	return c == 'G' || c == 'D'? 0: 1;
}

void oper(int a[MAXN][MAXN], char c)
{
	if(c == 'G')
	{
		for(int j=1; j<=m; ++j)
		{
			int pos = 0;
			for(int i=1; i<=n; ++i)
				if(a[i][j] != 0)
					swap(a[i][j], a[++pos][j]);
		}
	}
	else if(c == 'D')
	{
		for(int j=1; j<=m; ++j)
		{
			int pos = n+1;
			for(int i=n; i>=1; --i)
				if(a[i][j] != 0)
					swap(a[i][j], a[--pos][j]);
		}
	}
	else if(c == 'L')
	{
		for(int i=1; i<=n; ++i)
		{
			int pos = 0;
			for(int j=1; j<=m; ++j)
				if(a[i][j] != 0)
					swap(a[i][j], a[i][++pos]);
		}
	}
	else if(c == 'P')
	{
		for(int i=1; i<=n; ++i)
		{
			int pos = m+1;
			for(int j=m; j>=1; --j)
				if(a[i][j] != 0)
					swap(a[i][j], a[i][--pos]);
		}
	}
}

char s[MAXN][MAXN], ops[MAXD], clr[MAXN * MAXN];

int main(void)
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=n; ++i)
		scanf("%s",s[i]+1);
	
	int d;
	scanf("%d%s",&d,ops+1);;
	
	static int begid[MAXN][MAXN];
	int pcnt = 0;
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=m; ++j)
			if(s[i][j] != '.')
			{
				begid[i][j] = ++pcnt;
				clr[pcnt] = s[i][j];
			}
	
	static char sta[MAXD];
	int tp = 0;
	for(int i=1; i<=d; ++i)
	{
		char c = ops[i];
		bool flag = 1;
		while(tp)
		{
			if(gettype(sta[tp]) == gettype(c)) --tp;
			if(tp >= 2 && sta[tp-1] == c)
			{
				flag = 0;
				break;
			}
			break;
		}
		if(flag) sta[++tp] = c;
	}
	
	int pos = 1;
	if(pos <= tp) oper(begid, sta[pos++]);
	if(pos <= tp) oper(begid, sta[pos++]);
	
	if(tp - pos + 1 >= 4)
	{
		int rem = tp - pos + 1;
		rem /= 4;
		
		static int id[MAXN][MAXN];
		memcpy(id, begid, sizeof(id));
		
		for(int i=0; i<4; ++i)
			oper(id, sta[pos+i]);
		
		static int to[MAXN * MAXN], res[MAXN * MAXN];
		for(int i=1; i<=n; ++i)
			for(int j=1; j<=m; ++j)
				if(id[i][j])
					to[begid[i][j]] = id[i][j];
		
		for(int i=1; i<=pcnt; ++i) if(!res[i])
		{
			vector<int> vec;
			for(int j=i; !res[j]; j=to[j])
				res[j] = -1, vec.emplace_back(j);
			
			for(int j=0; j<(int)vec.size(); ++j)
				res[vec[j]] = vec[(j+rem) % (int)vec.size()];
		}
		
		for(int i=1; i<=n; ++i)
			for(int j=1; j<=m; ++j)
				if(begid[i][j])
					begid[i][j] = res[begid[i][j]];
		
		pos += rem * 4;
	}
	
	while(pos <= tp) oper(begid, sta[pos++]);
	
	for(int i=1; i<=n; ++i, printf("\n"))
		for(int j=1; j<=m; ++j)
			putchar(begid[i][j]? clr[begid[i][j]]: '.');
	return 0;
}