#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[256];
char mp[2005][2005];
inline int t(int i, int j) { return mp[i][j] - '0'; }
inline int nxt(int i, int j) { return (((t(i, j) * 3 + t(i - 1, j)) * 3 + t(i + 1, j)) * 3 + t(i, j - 1)) * 3 + t(i, j + 1); }
int vis[2005][2005];
int main()
{
    int T, n, m, a, b;
    ll k;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d%d%lld%s", &n, &m, &a, &b, &k, s);
        a--, b--;
        int ans = 0;
        for (int i = 0; i < n; i++) scanf("%s", mp + i);
        k = min(k, 2000ll * n * m);
        while (k--)
        {
            //printf("** %d %d\n", a, b);
            char op = s[nxt(a, b)];
            switch (op)
            {
            case 'U':
                if (t(a - 1, b) != 1) a--;
                break;
            case 'D':
                if (t(a + 1, b) != 1) a++;
                break;
            case 'L':
                if (t(a, b - 1) != 1) b--;
                break;
            case 'R':
                if (t(a, b + 1) != 1) b++;
                break;
            case 'P':
                if (mp[a][b] == '2') ans++;
                mp[a][b] = '0';
                break;
            case 'I':
                break;
            default:
                break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
