#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define LINF 999999999999999999LL
#define N (1<<20)
#define M 1000000007
#define MAX 1000000
using namespace std;
int v[55][55][5];
int vx[5];
int vy[5];
int d[5];
int t[5];
bool cy[5][4];
vector<pair<int, int>> ct[5];
int fo[5];
vector<int> q[5];
int iter[5];
int main () {
int n, m, k;
cin>>n>>m>>k;
int r, c;
cin>>r>>c;
for (int i = 0; i < k; i++) {
char c;
cin>>vy[i]>>vx[i]>>c;
if (c == 'L') d[i] = 0;
if (c == 'U') d[i] = 1;
if (c == 'R') d[i] = 2;
if (c == 'D') d[i] = 3;
for (int y = 1; y <= n; y++) {
for (int x = 1; x <= m; x++) {
cin>>c;
v[y][x][i] = c - '0';
}
}
}
for (int i = 0; i < k; i++) {
int l = 1;
int qa = MAX;
int ts = 1;
while (qa--) {
l++;
ts++;
int X = v[vy[i]][vx[i]][i];
int x = vx[i];
int y = vy[i];
d[i] = (d[i] + X) % 4;
if (d[i] == 0 && x == 1) d[i] = 2;
else if (d[i] == 1 && y == 1) d[i] = 3;
else if (d[i] == 2 && x == m) d[i] = 0;
else if (d[i] == 3 && y == n) d[i] = 1;
if (d[i] == 0) vx[i] -= 1;
if (d[i] == 1) vy[i] -= 1;
if (d[i] == 2) vx[i] += 1;
if (d[i] == 3) vy[i] += 1;
if (vy[i] == r && vx[i] == c) {
t[i] = l;
q[i].push_back(-l);
break;
}
}
if (qa <= 0) cout<<-1<<endl, exit(0);
l = 0;
while (qa--) {
ts++;
l++;
int X = v[vy[i]][vx[i]][i];
int x = vx[i];
int y = vy[i];
d[i] = (d[i] + X) % 4;
if (d[i] == 0 && x == 1) d[i] = 2;
else if (d[i] == 1 && y == 1) d[i] = 3;
else if (d[i] == 2 && x == m) d[i] = 0;
else if (d[i] == 3 && y == n) d[i] = 1;
if (d[i] == 0) vx[i] -= 1;
if (d[i] == 1) vy[i] -= 1;
if (d[i] == 2) vx[i] += 1;
if (d[i] == 3) vy[i] += 1;
if (vy[i] == r && vx[i] == c) {
if (cy[i][d[i]]) {
fo[i] = d[i];
int x = 0;
for (; x < 4; x++) {
if (ct[i][x].second == d[i]) break;
}
int j = x + 1;
if (j == ct[i].size()) j = x;
for (int w = 0; w < 6000000; w++) {
q[i].push_back(-ts);
ts += ct[i][j].first;
j++;
if (j == ct[i].size()) j = x;
}
break;
}
else {
cy[i][d[i]] = 1;
ct[i].push_back({l, d[i]});
q[i].push_back(-ts);
l = 0;
}
}
}
}
while (true) {
for (int i = 0; i < k; i++) if (iter[i] == q[i].size()) cout<<-1<<endl, exit(0);
//for (int i = 0; i < k; i++) cout<<-q[i].top()<<" ";
//cout<<endl;
int x = q[0][iter[0]];
bool b = true;
for (int i = 1; i < k; i++) {
if (q[i][iter[i]] != x) {
if (q[i][iter[i]] < x) iter[0]++;
else iter[i]++;
b = false;
break;
}
}
if (b) cout<<-x<<endl, exit(0);
}
}