/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

LL n,m,k;
string C;

char p[510][510];
char r1[510][510];
char r2[510][510];
char r3[510][510];
LL b[510][510];

void display()
{
    FOR(i,0,n-1)
    {
        FOR(j,0,m-1) cout << p[i][j];
        cout << "\n";
    }
}

void moveL()
{
    FOR(i,0,n-1)
    {
        LL k=0; // next free field to fill
        FOR(j,0,m-1) if (p[i][j]!='.') { p[i][k]=p[i][j]; k++; }
        FOR(j,k,m-1) p[i][j]='.';
    }
}

void moveR()
{
    FOR(i,0,n-1)
    {
        LL k=m-1; // next free field to fill
        FORD(j,m-1,0) if (p[i][j]!='.') { p[i][k]=p[i][j]; k--; }
        FORD(j,k,0) p[i][j]='.';
    }
}

void moveU()
{
    FOR(j,0,m-1)
    {
        LL k=0; // next free field to fill
        FOR(i,0,n-1) if (p[i][j]!='.') { p[k][j]=p[i][j]; k++; }
        FOR(i,k,n-1) p[i][j]='.';
    }
}

void moveD()
{
    FOR(j,0,m-1)
    {
        LL k=n-1; // next free field to fill
        FORD(i,n-1,0) if (p[i][j]!='.') { p[k][j]=p[i][j]; k--; }
        FORD(i,k,0) p[i][j]='.';
    }
}

void moveBL()
{
    FOR(i,0,n-1)
    {
        LL k=0; // next free field to fill
        FOR(j,0,m-1) if (b[i][j]!=-1) { b[i][k]=b[i][j]; k++; }
        FOR(j,k,m-1) b[i][j]=-1;
    }
}

void moveBR()
{
    FOR(i,0,n-1)
    {
        LL k=m-1; // next free field to fill
        FORD(j,m-1,0) if (b[i][j]!=-1) { b[i][k]=b[i][j]; k--; }
        FORD(j,k,0) b[i][j]=-1;
    }
}

void moveBU()
{
    FOR(j,0,m-1)
    {
        LL k=0; // next free field to fill
        FOR(i,0,n-1) if (b[i][j]!=-1) { b[k][j]=b[i][j]; k++; }
        FOR(i,k,n-1) b[i][j]=-1;
    }
}

void moveBD()
{
    FOR(j,0,m-1)
    {
        LL k=n-1; // next free field to fill
        FORD(i,n-1,0) if (b[i][j]!=-1) { b[k][j]=b[i][j]; k--; }
        FORD(i,k,0) b[i][j]=-1;
    }
}


void makeRandomBoard()
{
    FOR(i,0,n-1) FOR(j,0,m-1) p[i][j]='.';
    LL le = 3*(m/4);
    FOR(i,0,n-1)
    {
        if (le<=0) break;
        FOR(j,0,le-1) p[i][j]=(char)(65+rand()%20);
        le-=rand()%5;
    }
}

void makeRandomSeq(int len)
{
    string s(len, ' ');
    FOR(i,0,len-1)
    {
        int x=rand()%4;
        if (x==0) s[i]='G';
        else if (x==1) s[i]='D';
        else if (x==2) s[i]='L';
        else s[i]='P';
    }
    C=s; k=len;
}

void makeMove(char ch)
{
    if (ch=='G') moveU();
    else if (ch=='D') moveD();
    else if (ch=='P') moveR();
    else moveL();
}


void silly()
{
    FOR(i,0,k-1) makeMove(C[i]);
}

void makeRotations(int &corner, int moves)
{
    if (moves>=0)
    {
        // clockwise
        FOR(i,1,moves)
        {
            if (corner==0) { corner=1; moveR(); }
            else if (corner==1) { corner=2; moveD(); }
            else if (corner==2) { corner=3; moveL(); }
            else {corner=0; moveU(); }
        }
    }
    else
    {
        // anticlockwise
        FOR(i,1,-moves)
        {
            if (corner==0) { corner=3; moveD(); }
            else if (corner==1) { corner=0; moveL(); }
            else if (corner==2) { corner=1; moveU(); }
            else {corner=2; moveR(); }
        }
    }
}


LL code(LL i, LL j)
{
    return i*m+j;
}

LL perm[30][300000];
LL pe[300000];

void smart()
{
    LL s=0;
    while (s<=k-1)
    {
        if (C[s]=='L' || C[s]=='P') s++;
        else break;
    }
    if (s==0)
    {
        // there was no L nor P at the beginning
        while (s<=k-1)
        {
            if (C[s]=='G' || C[s]=='D') s++;
            else break;
        }
        if (s>0) s--;
    }
    else
    {
        // there were L or P at the beginning
        s--;
    }
    //cout << "After reduction: ";
    //FOR(i,s,k-1) cout << C[i]; cout << "\n";
    if (s==k-1)
    {
        // there is nothing more
        char ch=C[s];
        if (ch=='G') moveU();
        else if (ch=='D') moveD();
        else if (ch=='P') moveR();
        else moveL();
        //cout << "gotowe!\n";
        return;
    }
    string the2=C.substr(s,2);
    FOR(i,0,1) makeMove(the2[i]);
    s+=2;
    if (s>k-1) return;
    int corner0=0;
    if (the2=="LG"||the2=="GL") corner0=0;
    else if (the2=="PG"||the2=="GP") corner0=1;
    else if (the2=="PD"||the2=="DP") corner0=2;
    else if (the2=="LD"||the2=="DL") corner0=3;
    else cout << "Sth wrong\n";
    //cout << "Initial corner=" << corner0 << "\n";
    LL moves=0; int corner=corner0;
    FOR(i,s,k-1)
    {
        char ch=C[i];
        if (ch=='G')
        {
            if (corner==3) { corner=0; moves++;}
            else if (corner==2) { corner=1; moves--; }
        }
        else if (ch=='D')
        {
            if (corner==0) { corner=3; moves--;}
            else if (corner==1) { corner=2; moves++; }
        }
        else if (ch=='L')
        {
            if (corner==1) { corner=0; moves--;}
            else if (corner==2) { corner=3; moves++; }
        }
        else if (ch=='P')
        {
            if (corner==0) { corner=1; moves++;}
            else if (corner==3) { corner=2; moves--; }
        }
    }
    //cout << "Moves=" << moves << "\n";
    corner=corner0;
    LL absMoves=moves; if (moves<0) absMoves=-moves;
    if (absMoves<10)  // tu ma byc 10 !!!!!!!!!!!!!!!!!!!!!!!!!!
    {
        makeRotations(corner, moves);
        // cout << "gotowe!!\n";
        return;
    }
    while (corner!=0)
    {
        if (moves>0) { makeRotations(corner, 1); moves--; }
        else { makeRotations(corner, -1); moves++; }
    }
    //cout << "Moves left: " << moves << "\n";
    //display();
    // ------ do tego miejsca jest dobrze
    FOR(i,0,n-1) FOR(j,0,m-1)
    {
        if (p[i][j]=='.') b[i][j]=-1;
        else b[i][j]=code(i,j);
        //cout << i << "," << j << " (" << code(i,j) << ") b=" << b[i][j] << "\n";
    }
    if (moves>0) { moveBR(); moveBD(); moveBL(); moveBU(); }
    else { moveBD(); moveBR(); moveBU(); moveBL(); }
    /*
    FOR(i,0,n-1)
    {
        FOR(j,0,m-1) cout << b[i][j] << " ";
        cout << "\n";
    }
    */
    FOR(i,0,n-1) FOR(j,0,m-1)
    {
        if (b[i][j]==-1) perm[0][code(i,j)]=code(i,j);
        else perm[0][b[i][j]]=code(i,j);
    }
    //FOR(i,0,n-1) FOR(j,0,m-1) cout << i << "," << j << " (" << code(i,j) << ") -> " << perm[0][code(i,j)] << "\n";
    LL exp=1; LL lastPot=0;
    absMoves=moves; if (moves<0) absMoves=-moves;
    LL fullCircles=absMoves/4;
    while (exp<fullCircles)
    {
        exp=exp*2; lastPot++;
        FOR(i,0,n-1) FOR(j,0,m-1) perm[lastPot][code(i,j)]=perm[lastPot-1][ perm[lastPot-1][code(i,j)] ];
    }
    //cout << "fullCircles=" << fullCircles << " exp=" << exp << " lastPot=" << lastPot << "\n";
    LL pot=lastPot;
    //FOR(k,0,n*m) pe[k]=k;
    while (abs(moves)>=4)
    {
        while(exp>fullCircles) { exp=exp/2; pot--; };
        // perm[pot] jest permutacja złożoną 2^pot razy
        FOR(i,0,n-1) FOR(j,0,m-1)
        {
            if (p[i][j]=='.') r3[i][j]='.';
            else
            {
                LL targetCode=perm[pot][code(i,j)];
                LL i1=targetCode / m; LL j1=targetCode % m;
                r3[i1][j1]=p[i][j];
            }
        }
        FOR(i,0,n-1) FOR(j,0,m-1) p[i][j]=r3[i][j];
        fullCircles-=exp; if (moves>0) moves-=4*exp; else if (moves<0) moves+=4*exp;
    }
    corner=0;
    makeRotations(corner, moves);
}

bool oneTest()
{
    // copy P to R1:
    FOR(i,0,n-1) FOR(j,0,m-1) r1[i][j]=p[i][j];
    silly();
    //cout << "After silly:\n";
    //display();
    // copy P to R2:
    FOR(i,0,n-1) FOR(j,0,m-1) r2[i][j]=p[i][j];
    // copy R1 to P:
    FOR(i,0,n-1) FOR(j,0,m-1) p[i][j]=r1[i][j];
    smart();
    //cout << "After smart:\n";
    //display();
    // compare P with R2:
    FOR(i,0,n-1) FOR(j,0,m-1) if (p[i][j]!=r2[i][j])
    {
        cout << "Error!\n";
        return false;
    }
    return true;
}


/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

	cin >> n; cin >> m;
	string line;
	FOR(i,0,n-1)
	{
	    cin >> line;
	    FOR(j,0,m-1) p[i][j]=line[j];
	}
    cin >> k;
    cin >> C;
    FOR(i,0,k-1) if (C[i]=='R') C[i]='P';

    smart();
    display();
    /*
    n=23; m=34;
    FOR(z,1,10000)
    {
        makeRandomBoard();
        makeRandomSeq(1+rand()%50);
        cout << "Seq: "; FOR(i,0,k-1) cout << C[i]; cout << "\n";
        bool ok = oneTest();
        if (!ok) return 0;
    }
    cout << "All OK!\n";
    */

 /*
	while(true)
    {
        draw();
        cin >> line;
        if (line=="G") moveU();
        else if (line=="D") moveD();
        else if (line=="L") moveL();
        else if (line=="P") moveR();
        else break;
    }
*/
	return 0;
}
