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
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const ll q=502;
ll n,m,p,t[q][q];
int main()
{
    cin>>n>>m;
    for(ll i=1; i<=n; i++){
        for(ll j=1; j<=m; j++){
            char c; cin>>c;
            if(c=='.')
                t[i][j]=0;
            else if(c=='B')
                t[i][j]=1;
            else
                t[i][j]=2;
        }
    }
    cin>>p;
    for(ll i=1; i<=p; i++){
        char c; cin>>c;
        ll wsk;
        if(c=='G'){
            for(ll k=1; k<=m; k++){
                wsk=1;
                for(ll l=1; l<=n; l++){
                    if(t[l][k]!=0){
                        if(wsk!=l){
                           t[wsk][k]=t[l][k];
                            t[l][k]=0;
                        }
                        wsk++;
                    }
                    //cout<<t[k][l]<<"\n";    
                }
                //cout<<"\n";
            }
                
        }
        if(c=='D'){
            for(ll k=1; k<=m; k++){
                wsk=n;
                for(ll l=n; l>=1; l--){
                    if(t[l][k]!=0){
                        if(wsk!=l){
                            t[wsk][k]=t[l][k];
                            t[l][k]=0;
                        }
                        wsk--;
                    }
                    //cout<<t[k][l]<<"\n";    
                }
                //cout<<"\n";
            }
                
        }
        if(c=='L'){
            for(ll k=1; k<=n; k++){
                wsk=1;
                for(ll l=1; l<=m; l++){
                    if(t[k][l]!=0){
                        if(wsk!=l){
                            t[k][wsk]=t[k][l];
                            t[k][l]=0;
                        }
                        wsk++;
                    }
                    //cout<<t[k][l]<<"\n";    
                }
                //cout<<"\n";
            }
                
        }
        if(c=='P'){
            for(ll k=1; k<=n; k++){
                wsk=m;
                for(ll l=m; l>=1; l--){
                    if(t[k][l]!=0){
                        if(wsk!=l){
                            t[k][wsk]=t[k][l];
                            t[k][l]=0;
                        }
                        wsk--;
                    }
                    //cout<<t[k][l]<<"\n";    
                }
                //cout<<"\n";
            }
                
        }
        
         
    }
    for(ll h=1; h<=n; h++){
            for(ll j=1; j<=m; j++){
                //cout<<t[i][j]<<" ";
                ll x=t[h][j];
                if(x==0)
                    cout<<".";
                else if(x==1)
                    cout<<"B";
                else 
                    cout<<"C";
            }
            cout<<"\n";
        }   
        //cout<<"\n";

    return 0;
}