#include <bits/stdc++.h>
using namespace std;
const int MAX = 201;
int n, m;
string todo;
string moves;
string s[MAX];
int row[MAX], col[MAX];
int _row[MAX], _col[MAX];
void transpose_row(bool rev) {
fill(_row, _row + n, 0);
for(int j = 0; j < m; ++j) {
_row[0] += 1;
_row[col[j]] -= 1;
}
row[0] = _row[0];
for(int i = 1; i < n; ++i) {
_row[i] += _row[i-1];
row[i] = _row[i];
}
if (rev) {
for(int i = 0; i < n/2; ++i) {
swap(row[i], row[n-i-1]);
}
}
}
void transpose_col(bool rev) {
fill(_col, _col + m, 0);
for(int i = 0; i < n; ++i) {
_col[0] += 1;
_col[row[i]] -= 1;
}
col[0] = _col[0];
for(int j = 1; j < m; ++j) {
_col[j] += _col[j-1];
col[j] = _col[j];
}
if (rev) {
for(int j = 0; j < m/2; ++j) {
swap(col[j], col[m-j-1]);
}
}
}
void move_up() {
for(int j = 0; j < m; ++j) {
for(int i = 0; i < col[j]; ++i) {
s[i][j] = '1';
}
for(int i = col[j]; i < n; ++i) {
s[i][j] = '0';
}
}
}
void move_down() {
for(int j = 0; j < m; ++j) {
for(int i = n-1; i >= n-col[j]; --i) {
s[i][j] = '1';
}
for(int i = n-col[j]-1; i >= 0; --i) {
s[i][j] = '0';
}
}
}
void move_left() {
for(int i = 0; i < n; ++i) {
for(int j = 0; j < row[i]; ++j) {
s[i][j] = '1';
}
for(int j = row[i]; j < m; ++j) {
s[i][j] = '0';
}
}
}
void move_right() {
for(int i = 0; i < n; ++i) {
for(int j = m-1; j >= m-row[i]; --j) {
s[i][j] = '1';
}
for(int j = m-row[i]-1; j >= 0; --j) {
s[i][j] = '0';
}
}
}
string compress(string &s) {
string x = "";
for(int i = 0; i < s.length(); ++i) {
int j = i;
while(s[i] == s[j] && j < s.length()) {
++j;
}
x += s[i];
i = j - 1;
}
return x;
}
void apply(int i) {
todo += moves[i];
if (moves[i] == 'L') {
transpose_col(0);
}
else if (moves[i] == 'R') {
transpose_col(1);
}
else if (moves[i] == 'D') {
transpose_row(1);
}
else {
transpose_row(0);
}
}
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while(t--) {
cin >> n >> m;
for(int i = 0; i < n; ++i) {
cin >> s[i];
}
for(int i = 0; i < n; ++i) {
row[i] = 0;
for(int j = 0; j < m; ++j) {
row[i] += s[i][j] - '0';
}
}
for(int j = 0; j < m; ++j) {
col[j] = 0;
for(int i = 0; i < n; ++i) {
col[j] += s[i][j] - '0';
}
}
todo.clear();
cin >> moves;
moves = compress(moves);
for(int i = 0; i < moves.length(); ++i) {
int j = i;
while(j < moves.length()) {
if ((moves[i] == 'L' || moves[i] == 'R') &&
(moves[j] == 'L' || moves[j] == 'R')) {
j += 1;
}
else if ((moves[i] == 'U' || moves[i] == 'D') &&
(moves[j] == 'U' || moves[j] == 'D')) {
j += 1;
}
else {
apply(j-1);
i = j - 1;
break;
}
}
}
apply(moves.length()-1);
for(int i = max(0, (int)todo.length()-2); i < todo.length(); ++i) {
if (todo[i] == 'L') {
move_left();
}
else if (todo[i] == 'R') {
move_right();
}
else if (todo[i] == 'D') {
move_down();
}
else {
move_up();
}
}
for(int i = 0; i < n; ++i) {
cout << s[i] << "\n";
}
}
return 0;
}