#include using namespace std; const int MOD = 1000000007; const int MaxN = (int)1e2 + 10; const int INF = (int)1e9; int Columns, Rows; char Map[MaxN][MaxN]; char NextMap[MaxN][MaxN]; char Moves[MaxN * MaxN]; bool IsValidCell(int x, int y) { return x >= 1 && x <= Rows && y >= 1 && y <= Columns && Map[x][y] == '0'; } void solve() { scanf("%d%d\n", &Rows, &Columns); for (int i = 1; i <= Rows; ++i) { scanf("%s", Map[i] + 1); } scanf("%s", Moves + 1); int LengthS = strlen(Moves + 1); string ValuableMoves; for (int it = LengthS, horMoved = 0, verMoved = 0; it >= 1; --it) { if (Moves[it] == 'L' || Moves[it] == 'R') { if (!horMoved) { horMoved = 1; ValuableMoves += Moves[it]; } } else { if (!verMoved) { verMoved = 1; ValuableMoves += Moves[it]; } } } ValuableMoves += Moves[1]; reverse(ValuableMoves.begin(), ValuableMoves.end()); for (auto& Dir : ValuableMoves) { if (Dir == 'L') { for (int i = 1; i <= Rows; ++i) { int cnt = 0; for (int j = 1; j <= Columns; ++j) { if (Map[i][j] == '1') { ++cnt; } Map[i][j] = '0'; } for (int j = 1; j <= cnt; ++j) { Map[i][j] = '1'; } } } if (Dir == 'R') { for (int i = 1; i <= Rows; ++i) { int cnt = 0; for (int j = 1; j <= Columns; ++j) { if (Map[i][j] == '1') { ++cnt; } Map[i][j] = '0'; } for (int j = 1; j <= cnt; ++j) { Map[i][Columns - j + 1] = '1'; } } } if (Dir == 'U') { for (int j = 1; j <= Columns; ++j) { int cnt = 0; for (int i = 1; i <= Rows; ++i) { if (Map[i][j] == '1') { ++cnt; } Map[i][j] = '0'; } for (int i = 1; i <= cnt; ++i) { Map[i][j] = '1'; } } } if (Dir == 'D') { for (int j = 1; j <= Columns; ++j) { int cnt = 0; for (int i = 1; i <= Rows; ++i) { if (Map[i][j] == '1') { ++cnt; } Map[i][j] = '0'; } for (int i = 1; i <= cnt; ++i) { Map[Rows - i + 1][j] = '1'; } } } } for (int i = 1; i <= Rows; ++i) { printf("%s\n", Map[i] + 1); } } int main() { // freopen("input.txt", "r", stdin); int NumberOfTests; scanf("%d", &NumberOfTests); while (NumberOfTests --> 0) { solve(); } return 0; }