/// Author : Nguyễn Thái Sơn - Ti20 - THPT chuyên Lương Thế Vinh
/// Training VOI 2023
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
//#include <ext/rope>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")
//using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define fi first
#define se second
#define TASK "test"
#define pb push_back
#define EL cout << endl
#define Ti20_ntson int main()
#define in(x) cout << x << endl
#define all(x) (x).begin(),(x).end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define cntbit(x) __builtin_popcount(x)
#define FOR(i,l,r) for (int i = l; i <= r; i++)
#define FORD(i,l,r) for (int i = l; i >= r; i--)
#define Debug(a,n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> vii;
typedef unsigned long long ull;
typedef vector<vector<int>> vvi;
const int N = 55;
const int oo = INT_MAX;
const int mod = 1e9 + 7;
const int d4x[4] = {-1, 0, 1, 0} , d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, m, dp[N][N][N];
char c[N][N];
string s;
vii st, ft;
inline void Read_Input() {
memset(dp, 0x3f, sizeof(dp));
cin >> n >> m;
FOR(i, 1, n)
FOR(j, 1, m) {
cin >> c[i][j];
if (c[i][j] == 'S')
st = vii(i, j);
if (c[i][j] == 'G')
ft = vii(i, j);
}
cin >> s;
}
inline void Mn(int &u, int v) {
if (u > v) u = v;
}
inline void Solve() {
dp[st.fi][st.se][0] = 0;
int Ans = mod;
#define t3 tuple<int, int, int>
FOR(i, 0, s.size()) {
priority_queue<t3, vector<t3>, greater<t3>> Q;
FOR(x, 1, n)
FOR(y, 1, m)
Q.push({dp[x][y][i], x, y});
// cout << "CHECK " << i << endl;
while (Q.size()) {
auto[w, x, y] = Q.top(); Q.pop();
if (dp[x][y][i] < w) continue;
// cout << w << " " << x << " " << y << endl;
for (int k = 0; k < 4; k++) {
int nx = x + d4x[k];
int ny = y + d4y[k];
if (nx < 1 || nx > n) continue;
if (ny < 1 || ny > m) continue;
if (c[nx][ny] == '#') continue;
// cout << "UPD " << nx << " " << ny << endl;
if (dp[nx][ny][i] > w + 1) {
dp[nx][ny][i] = w + 1;
Q.push({w + 1, nx, ny});
}
}
}
// cout << "MATRIX " << i << endl;
if (i < s.size())
FOR(x, 1, n)
FOR(y, 1, m) {
// cout << "CHECK " << x << " " << y << " " << dp[x][y][i] << endl;
Mn(dp[x][y][i + 1], dp[x][y][i] + 1);
int nx = x, ny = y;
if (s[i] == 'U')
nx--;
if (s[i] == 'D')
nx++;
if (s[i] == 'L')
ny--;
if (s[i] == 'R')
ny++;
// cout << nx << " " << ny << endl;
if (nx < 1 || nx > n) {
Mn(dp[x][y][i + 1], dp[x][y][i]);
continue;
}
if (ny < 1 || ny > m) {
Mn(dp[x][y][i + 1], dp[x][y][i]);
continue;
}
if (c[nx][ny] == '#')
Mn(dp[x][y][i + 1], dp[x][y][i]);
else Mn(dp[nx][ny][i + 1], dp[x][y][i]);
}
Ans = min(Ans, dp[ft.fi][ft.se][i]);
}
cout << Ans;
}
Ti20_ntson {
// freopen(TASK".INP","r",stdin);
// freopen(TASK".OUT","w",stdout);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T = 1;
while (T -- ) {
Read_Input();
Solve();
}
}
Ly8vIEF1dGhvciA6IE5ndXnhu4VuIFRow6FpIFPGoW4gLSBUaTIwIC0gVEhQVCBjaHV5w6puIEzGsMahbmcgVGjhur8gVmluaAovLy8gVHJhaW5pbmcgVk9JIDIwMjMKCiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+Ci8vI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPgovLyNpbmN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BGT1IoaSwgMCwgcy5zaXplKCkpIHsKICAgICAgICBwcmlvcml0eV9xdWV1ZTx0MywgdmVjdG9yPHQzPiwgZ3JlYXRlcjx0Mz4+IFE7CiAgICAgICAgRk9SKHgsIDEsIG4pCiAgICAgICAgICAgIEZPUih5LCAxLCBtKQogICAgICAgICAgICAgICAgUS5wdXNoKHtkcFt4XVt5XVtpXSwgeCwgeX0pOwovLyAgICAgICAgY291dCA8PCAiQ0hFQ0sgIiA8PCBpIDw8IGVuZGw7CiAgICAgICAgd2hpbGUgKFEuc2l6ZSgpKSB7CiAgICAgICAgICAgIGF1dG9bdywgeCwgeV0gPSBRLnRvcCgpOyBRLnBvcCgpOwogICAgICAgICAgICBpZiAoZHBbeF1beV1baV0gPCB3KSBjb250aW51ZTsKLy8gICAgICAgICAgICBjb3V0IDw8IHcgPDwgIiAiIDw8IHggPDwgIiAiIDw8IHkgPDwgZW5kbDsKICAgICAgICAgICAgZm9yIChpbnQgayA9IDA7IGsgPCA0OyBrKyspIHsKICAgICAgICAgICAgICAgIGludCBueCA9IHggKyBkNHhba107CiAgICAgICAgICAgICAgICBpbnQgbnkgPSB5ICsgZDR5W2tdOwogICAgICAgICAgICAgICAgaWYgKG54IDwgMSB8fCBueCA+IG4pIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgaWYgKG55IDwgMSB8fCBueSA+IG0pIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgaWYgKGNbbnhdW255XSA9PSAnIycpIGNvbnRpbnVlOwovLyAgICAgICAgICAgICAgICBjb3V0IDw8ICJVUEQgIiA8PCBueCA8PCAiICIgPDwgbnkgPDwgZW5kbDsKICAgICAgICAgICAgICAgIGlmIChkcFtueF1bbnldW2ldID4gdyArIDEpIHsKICAgICAgICAgICAgICAgICAgICBkcFtueF1bbnldW2ldID0gdyArIDE7CiAgICAgICAgICAgICAgICAgICAgUS5wdXNoKHt3ICsgMSwgbngsIG55fSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9Ci8vICAgICAgICBjb3V0IDw8ICJNQVRSSVggIiA8PCBpIDw8IGVuZGw7CiAgICAgICAgaWYgKGkgPCBzLnNpemUoKSkKICAgICAgICAgICAgRk9SKHgsIDEsIG4pCiAgICAgICAgICAgICAgICBGT1IoeSwgMSwgbSkgewovLyAgICAgICAgICAgICAgICAgICAgY291dCA8PCAiQ0hFQ0sgIiA8PCB4IDw8ICIgIiA8PCB5IDw8ICIgIiA8PCBkcFt4XVt5XVtpXSA8PCBlbmRsOwogICAgICAgICAgICAgICAgICAgIE1uKGRwW3hdW3ldW2kgKyAxXSwgZHBbeF1beV1baV0gKyAxKTsKICAgICAgICAgICAgICAgICAgICBpbnQgbnggPSB4LCBueSA9IHk7CiAgICAgICAgICAgICAgICAgICAgaWYgKHNbaV0gPT0gJ1UnKQogICAgICAgICAgICAgICAgICAgICAgICBueC0tOwogICAgICAgICAgICAgICAgICAgIGlmIChzW2ldID09ICdEJykKICAgICAgICAgICAgICAgICAgICAgICAgbngrKzsKICAgICAgICAgICAgICAgICAgICBpZiAoc1tpXSA9PSAnTCcpCiAgICAgICAgICAgICAgICAgICAgICAgIG55LS07CiAgICAgICAgICAgICAgICAgICAgaWYgKHNbaV0gPT0gJ1InKQogICAgICAgICAgICAgICAgICAgICAgICBueSsrOwovLyAgICAgICAgICAgICAgICAgICAgY291dCA8PCBueCA8PCAiICIgPDwgbnkgPDwgZW5kbDsKICAgICAgICAgICAgICAgICAgICBpZiAobnggPCAxIHx8IG54ID4gbikgewogICAgICAgICAgICAgICAgICAgICAgICBNbihkcFt4XVt5XVtpICsgMV0sIGRwW3hdW3ldW2ldKTsKICAgICAgICAgICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmIChueSA8IDEgfHwgbnkgPiBtKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIE1uKGRwW3hdW3ldW2kgKyAxXSwgZHBbeF1beV1baV0pOwogICAgICAgICAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgaWYgKGNbbnhdW255XSA9PSAnIycpCiAgICAgICAgICAgICAgICAgICAgICAgIE1uKGRwW3hdW3ldW2kgKyAxXSwgZHBbeF1beV1baV0pOwogICAgICAgICAgICAgICAgICAgIGVsc2UgTW4oZHBbbnhdW255XVtpICsgMV0sIGRwW3hdW3ldW2ldKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICBBbnMgPSBtaW4oQW5zLCBkcFtmdC5maV1bZnQuc2VdW2ldKTsKICAgIH0KICAgIGNvdXQgPDwgQW5zOwp9CgpUaTIwX250c29uIHsKLy8gICAgZnJlb3BlbihUQVNLIi5JTlAiLCJyIixzdGRpbik7Ci8vICAgIGZyZW9wZW4oVEFTSyIuT1VUIiwidyIsc3Rkb3V0KTsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIGludCBUID0gMTsKICAgIHdoaWxlIChUIC0tICkgewogICAgICAgIFJlYWRfSW5wdXQoKTsKICAgICAgICBTb2x2ZSgpOwogICAgfQp9Cg==