The Professor has lost his home robot yet again. After some thinking Professor understood that he had left the robot in the basement.The basement in Professor's house is represented by a rectangle n × m, split into 1 × 1 squares. Some squares are walls which are impassable; other squares are passable. You can get from any passable square to any other passable square moving through edge-adjacent passable squares. One passable square is the exit from the basement. The robot is placed exactly in one passable square. Also the robot may be placed in the exit square.Professor is scared of going to the dark basement looking for the robot at night. However, he has a basement plan and the robot's remote control. Using the remote, Professor can send signals to the robot to shift one square left, right, up or down. When the robot receives a signal, it moves in the required direction if the robot's neighboring square in the given direction is passable. Otherwise, the robot stays idle.Professor wrote a sequence of k commands on a piece of paper. He thinks that the sequence can lead the robot out of the basement, wherever it's initial position might be. Professor programmed another robot to press the required buttons on the remote according to the notes on the piece of paper. Professor was just about to run the program and go to bed, when he had an epiphany.Executing each command takes some energy and Professor doesn't want to get huge electricity bill at the end of the month. That's why he wants to find in the sequence he has written out the minimal possible prefix that would guarantee to lead the robot out to the exit after the prefix is fulfilled. And that's the problem Professor challenges you with at this late hour.

The first line contains three integers n, m and k (3 ≤ n, m ≤ 150, 1 ≤ k ≤ 105). Next n lines contain m characters each — that is the Professor's basement's description: "#" stands for a wall, "." stands for a passable square and "E" stands for the exit from the basement (this square also is passable). It is possible to get from each passable square to the exit, all squares located by the n × m rectangle's perimeter are the walls. Exactly one square is the exit from the basement. The last line contains k characters, the description of the sequence of commands that Professor has written out on a piece of paper. "L", "R", "U", "D" stand for commands left, right, up and down correspondingly.

Print in the output file the length of the smallest possible prefix that will lead the robot to the exit square. In other words, wherever the robot had been positioned initially, it should be positioned in the exit square after all the commands from the prefix are fulfilled (during doing commands the robot can come and leave the exit square, but only the last position of the robot is interesting for us). If Professor is mistaken and no prefix (including the whole sequence) can bring the robot to the exit, print "-1" (without the quotes). If there is the only passable square and it is the exit, print "0" (without the quotes).

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string.h>
#include <limits.h>
#include <cfloat>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mem(flag) memset(flag,0,sizeof(flag))
#define Mem(flag) memset(flag,-1,sizeof(flag))
#define read(a) a=getInt()
#define En printf("\n")
#define P(a) printf("%d ",a)
#define Pn(a) printf("%d\n",a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define rep(flag, a, b) for (int flag = (a); flag <= (b); ++flag)
#define Rep(flag, a, b) for (int flag = (a); flag >= (b); --flag)

const int MAX1 = 155, MAX2 = 1e5 + 20, MAX3 = 155 / 32 + 2;
char mm[MAX1][MAX1];
unsigned int st[MAX1][MAX3];
unsigned int ma[MAX1][MAX3], ma2[MAX1][MAX3], maf[4][MAX1][MAX3];
unsigned int mu[MAX1][MAX3];
unsigned int tmp1[MAX1][MAX3], tmp2[MAX1][MAX3];
int n, m, k;
int colnum;
char com[MAX2];
int x[4] = {0, 0, 1, -1}, y[4] = {-1, 1, 0, 0};

inline void set1(unsigned int hh[][MAX3], int i, int j) {
    int pos, off;
    j++;
    if (j % 32 == 0) {
        pos = j / 32 - 1, off = 31;
    } else {
        pos = j / 32, off = j % 32 - 1;
    }
    hh[i][pos] |= 1 << off;
}

inline bool check() {
    bool ret = 1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < colnum; j++) {
            if (st[i][j] != mu[i][j]) {
                ret = 0; break;
            }
        }
    }
    return ret;
}

inline void shift(unsigned int st[][MAX3], int f) {
    if (f == 0) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < colnum; j++) {
                if (j == colnum - 1 || (st[i][j + 1] & 1) == 0)  {
                    st[i][j] >>= 1;
                } else {
                    st[i][j] >>= 1;
                    st[i][j] |= 1 << 31;
                }
            }
        }
    }
    if (f == 1) {
        for (int i = 0; i < n; i++) {
            for (int j = colnum - 1; j >= 0; j--) {
                if (j == 0 || (st[i][j - 1] & (1 << 31)) == 0) {
                    st[i][j] <<= 1;
                } else {
                    st[i][j] <<= 1;
                    st[i][j] |= 1;
                }
            }
        }
    }
    if (f == 2) {
        for (int c = 0; c < colnum; c++) {
            for (int r = n - 1; r >= 1; r--) {
                st[r][c] = st[r - 1][c];
            }
        }
    }
    if (f == 3) {
        for (int c = 0; c < colnum; c++) {
            for (int r = 0; r < n - 1; r++) {
                st[r][c] = st[r + 1][c];
            }
        }
    }
}

inline void getT1(int f) {
    for (int i = 0; i < n; i++) {
        for (int j= 0; j < colnum; j++) tmp1[i][j] = st[i][j] & maf[f][i][j];
    }
}
inline void getT2() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < colnum; j++) {
            tmp2[i][j] = st[i][j] & ma[i][j];
            st[i][j] = tmp1[i][j] | tmp2[i][j];
        }
    }
}

int main() {
    scanf("%d%d%d", &n, &m, &k);
    int cnt = 0;;
    for (int i = 0; i < n; i++) {
        scanf("%s", mm[i]);
        for (int j = 0; j < m; j++)
            if (mm[i][j] != '#') cnt++;

    }
    colnum = m % 32 == 0? m / 32 : m / 32 + 1;
    mem(mu);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (mm[i][j] != '#') set1(ma, i, j);
            if (mm[i][j] == 'E') set1(mu, i, j);
            if (mm[i][j] == '#') {
                for (int d = 0; d < 4; d++)
                    set1(maf[d], i, j);
            }
        }
    }
    memcpy(st, ma, sizeof(ma));
    for (int d = 0; d < 4; d++) {

        if (d == 0) shift(maf[d], 1);
        if (d == 1) shift(maf[d], 0);
        if (d == 2) shift(maf[d], 3);
        if (d == 3) shift(maf[d], 2);

    }
    scanf("%s", com);
    if (cnt == 1) Pn(0);
    else {

        bool gok = 0; int idx;
        for (int i = 0; i < k; i++) {
            if (com[i] == 'L') {
                getT1(0);

                shift(st, 0);

            }
            if (com[i] == 'R') {
                getT1(1);
                shift(st, 1);
            }
            if (com[i] == 'D') {
                getT1(2);
                shift(st, 2);
            }
            if (com[i] == 'U') {
                getT1(3);
                shift(st, 3);
            }

            getT2();

            if (check()) { gok = 1, idx = i; break; }

        }
        if (gok) Pn(idx + 1);
        else Pn(-1);
    }
    return 0;
}

// AVX-enabled std::bitset Pragmas (before #includes!)
// - bitset<200000> takes ~ 1ms per 1000 operations on GNU C++14 & GNU C++17
// #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
// #pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
// #pragma GCC target("avx2")  //Enable AVX

// =========== Template ========== //
#include <bits/stdc++.h>
using namespace std;
int fastio() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); return 0; }
int __fastio = fastio();
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p);
template<typename A> ostream& operator<<(ostream &os, const set<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A> ostream& operator<<(ostream &os, const multiset<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e.first << ": " << e.second, sep = ", "; return os << "}"; }
template<typename T>ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; string sep = ""; for (auto e: v) os << sep << e, sep = ", "; return os << "]"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef STEVEN
#define pp(...) cerr << "\t[" << #__VA_ARGS__ << "]:\t", dbg_out(__VA_ARGS__)
#define ppp(x) cerr << x << "\n"
#else
#define pp(...)
#define ppp(...)
#endif
template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } };
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
#define fi first
#define se second
#define sz(x) (int((x).size()))
#define data _data
// =========== Book ============ //

const int inf = 1000 << 20;
// =========== Solution ============= //

typedef bitset<23104> BS;

BS sh(BS a, int b) {
  if (b > 0) return a << b;
  else return a >> -b;
}

int main() {
  BS passable;
  int n, m, k;
  cin >> n >> m >> k;
  int exit = 0;
  int row = m + 2;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; ++j) {
      passable[i * row + j] = s[j - 1] == '.' || s[j - 1] == 'E';
      if (s[j - 1] == 'E') exit = i * (row) + j;
    }
  }
  string moves;
  cin >> moves;

  BS cur = passable;
  int ans = -1;
  for (int idx = 0; idx <= k; ++idx) {
    if (cur.count() == 1 && cur[exit]) {
      ans = idx;
      break;
    }
    if (idx == k) break;
    char ch = moves[idx];
    int offset;
    if (ch == 'U') offset = -row;
    else if (ch == 'D') offset = row;
    else if (ch == 'L') offset = -1;
    else offset = 1;

    BS move = sh(cur, offset);
    BS nxt = (move & passable) | sh(move & ~passable, -offset);
    cur = nxt;
  }
  cout << ans << "\n";
}

// ============== Notes =============== //
// common bugs
// - fastio
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN? different MAXN vs MAXM?)
// - special cases (n=1? graph not connected?)
// - initialize & re-initialize efficiently between test cases
//
// history of bugs
// - 3/28/20 forgot to initialize set with dummy node (n) [cf 1136e]
// - 3/28/20 array bounds (map was size MAXN instead of MAXK) [cf 1210b]
// - 3/29/20 array bounds ([MAXN][MAXN][4] -> [MAXN][MAXN * 2][4]) [cf 1051d]
// - 3/29/20 special cases (answer cannot be empty string) [cf 848a]
// - 3/29/20 special case / bad initialization of (ans, ansi) when backtracking [cf 733f]
// - 3/30/20 special case / "0" has length 0 in binary [cf 748b]
// - 4/2/20 fakesolve -- setup of dp states missed possible transitions / solutions [cf 771D]
// - 4/2/20 fakesolve -- cant count inversions in linear time, misunderstand structure of problem [cf 771D]
// - 4/3/20 (TLE) re-initialization was too slow [cf 1329 D]
// - 4/3/20 (WA) bottom-up segtree query didn't handle [0, n) correctly
// - 4/5/20 array bounds -- two different array sizes needed [cf 18d]
// - 4/5/20 fakesolve -- greedy sol was wrong,  needed dp [cf 18d]
// - 4/6/20 fakesolve -- tried to do LIS backtracking incorrectly w/ greedy approach, needed true backtracking [cf 650d]
// - 4/6/20 array bounds -- indexing with count into vector of size n; needs to be n + 1
// - 4/7/20 typo -- forgot to change i -> j when copying var declarations x -> y (4 copies) [cf 690b1]
// - 4/8/20 implementation error -- forgot to iterate in sorted order of x [cf 1045g]
// - 4/8/20 plane sweep implementation error -- insertion & removal orders are distinct (x_i & x_i + r_i) [cf 1045g]
// - 4/8/20 inf not big enough -- 1000 << 20 vs 1 billion + 1 billion [cf 1045g]
// - 4/8/20 n ffts wayyy too slow (nlog^2n, n = 2^20, time limit = 1s) [cf 1227f2]
// - 4/8/20 int overflow (multiplied j * count[j] without LL, when adding to ll) [cf 804d]
// - 4/8/20 implementation error -- forgot to dfs again (a 3rd time) after finding both ends of diameter [cf 804d]
// - 4/8/20 typo -- cur.fi < n && cur.se < n <-- should be m
// - 4/10/20 implementation error -- need to erase before inserting into pdbs::tree [1093e]
// - 4/10/20 typo -- forgot to change n + 1 to m + 1 when fixing sliding window to be over edges instead of nodes [cf 901c]
// - 4/10/20 typo -- implementation error -- messed up dfs cycle detection; need to mark edges as vis instead of nodes / use a real algo [cf 901c]
// - 4/10/20 turns out i misread the problem and it's actually querying nodes, not edges
// - 4/10/20 typo -- s/m/n again in if (r < m && ...)
// - 4/10/20 int overflow -- map<int, int> instead of map<ll, int> (elements were from vector<ll>)
// - 4/13/20 fakesolve -- "proved" ans = min(1, n) when ans = n [cf 1339a]
// - 4/13/20 implementation error -- failed to correctly iterate over all permutations of (x, y, z) and its subsets (missed y, z, yz, zy)
// - 4/13/20 int overflow -- iterating 10^5 times from l to r, but l and r may be large[cf 768b]
// - 4/13/20 bit length of 0 is 1 [cf 768b]
// - 4/13/20 fakesolve -- rectangles can overlap without both containing each others vertices [1216c]

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<bitset>
#define N 155
#define M 100005
#define LL long long
#define oo (1<<30)
using namespace std;
int n,m,len; char A[N][N],st[M];
bitset<N*N> bar,U,D,L,R,E,F,G,End;
int gd(int x,int y){ return (x-1)*m+y;}
int main()
{
	int i,j;
	scanf("%d %d %d\n",&n,&m,&len);
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++)
			A[i][j]=getchar();
		getchar();
	  }
	for(i=1;i<=len;i++) st[i]=getchar();
	for(i=2;i<n;i++)
		for(j=2;j<m;j++){
			if(A[i+1][j]=='#') D[gd(i,j)]=1;
			if(A[i-1][j]=='#') U[gd(i,j)]=1;
			if(A[i][j+1]=='#') R[gd(i,j)]=1;
			if(A[i][j-1]=='#') L[gd(i,j)]=1;
			if(A[i][j]!='#') bar[gd(i,j)]=1;
			if(A[i][j]=='E') End[gd(i,j)]=1;
		  }
	for(i=1;i<=len+1;i++){
		if(bar==End){ printf("%d",i-1); return 0;}
		if(st[i]=='U'){ E=bar&U; bar=((bar^E)>>m)|E;}
		if(st[i]=='D'){ E=bar&D; bar=((bar^E)<<m)|E;}
		if(st[i]=='L'){ E=bar&L; bar=((bar^E)>>1)|E;}
		if(st[i]=='R'){ E=bar&R; bar=((bar^E)<<1)|E;}
	  }
	printf("-1");
	return 0;
}

#include<iostream>
#include<cstdio>
#include<bitset>
#include<cstring>
using namespace std;
int n,m,k,p[200][200];
bitset<40000>a,c,b,e;
char ch[100009];

int work()
{
	for(int i=0;i<n;i++)
	{
		scanf("%s",ch);
		for(int j=0;j<m;j++)
		{
			if(ch[j]=='#')b[i*m+j]=1;
			else a[i*m+j]=1;
			if(ch[j]=='E')e[i*m+j]=1;
		}
	}
//	cout<<e<<"eeeeeeeeeee";
	cin>>ch;
	c=a;
	for(int i=0;i<k;i++)
	{
		if(c==e)return i;
		if(ch[i]=='L')c=(((c>>1)&a)|(b<<1)&c);
		if(ch[i]=='R')c=(((c<<1)&a)|(b>>1)&c);
		if(ch[i]=='U')c=(((c>>m)&a)|(b<<m)&c);
		if(ch[i]=='D')c=(((c<<m)&a)|(b>>m)&c);
		//cnt++;
	//cout<<x<<endl;
	}
	if(c==e)return k;
	return -1;
}
int main()
{
	cin>>n>>m>>k;
	// O_O();
	cout<<work();
	}

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<iterator>
#include<iostream>
#include<list>
#include<map>
#include<memory>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#include<utility>
#include<bitset>
#define ll long long
using namespace std;

bitset<22500> mp,w,r,e;
int main(){
    int n,m,k,i,j;
    cin>>n>>m>>k;
    string s;
    for(i=0;i<n;i++)
    {   cin>>s;
        for(j=0;j<m;j++)
        {   if(s[j]=='.' || s[j]=='E') {mp.set(i*m+j);r.set(i*m+j);}
            if(s[j]=='#') w.set(i*m+j);
            if(s[j]=='E') e.set(i*m+j);
        }
    }
    char c;
    scanf("%c",&c);
    for(i=0;i<k;i++)
    {   if(r==e) break;
        scanf("%c",&c);
        if(c=='L') r=(r>>1)&mp | (w<<1)&r;
        if(c=='R') r=(r<<1)&mp | (w>>1)&r;
        if(c=='U') r=(r>>m)&mp | (w<<m)&r;
        if(c=='D') r=(r<<m)&mp | (w>>m)&r;
    }
    if(r==e) cout<<i<<endl;
    else cout<<-1;
    //system("pause");
    return 0;
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<bitset>
using namespace std;
bitset<22500>a,b,c,e;
int n,m,k;
char str[101000];
int main(){
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++){
      scanf("%s",str);
      for(int j=0;j<m;j++)
        if(str[j]=='#')b.set(i*m+j);
        else{
          a.set(i*m+j);
          if(str[j]=='E')e.set(i*m+j);
        }
      }
    c=a;
    scanf("%s",str);
    for(int i=0;i<k;i++){
      if(c==e){printf("%d\n",i);return 0;}
      if(str[i]=='L')c=((c>>1)&a)|((b<<1)&c);
      if(str[i]=='R')c=((c<<1)&a)|((b>>1)&c);
      if(str[i]=='U')c=((c>>m)&a)|((b<<m)&c);
      if(str[i]=='D')c=((c<<m)&a)|((b>>m)&c);
      }
    if(c==e)printf("%d\n",k);
    else printf("-1\n");
    return 0;
}

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <bitset>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));i++)
#define pb       	push_back
#define sz(v)    	((li)v.size())
#define bit(n)   	(1ll<<(li)(n))

#define MAX 155
bitset<MAX> mp[MAX];
bitset<MAX> goal[MAX];
bitset<MAX> pos[MAX];
bitset<MAX> next[MAX];
string str[MAX];
string inst;

void check(li cnt){
	rep(i,MAX)if(goal[i]!=pos[i]) return;
	cout<<cnt<<endl;
	exit(0);
}

int main(){
	li n,m,k;
	cin>>n>>m>>k;
	rep(i,n) cin>>str[i];
	rep(i,n) mp[i].reset();
	rep(i,n) goal[i].reset();
	rep(i,n) pos[i].reset();
	rep(i,n)rep(j,m)if(str[i][j]=='#') mp[i][j]=1;
	rep(i,n)rep(j,m)if(str[i][j]=='E') goal[i][j]=pos[i][j]=1;
	rep(i,n)rep(j,m)if(str[i][j]=='.') pos[i][j]=1;
	cin>>inst;
	li cnt=0;
	while(true){
		check(cnt);
		if(cnt==sz(inst)) break;
		rep(i,MAX) next[i].reset();
		if(inst[cnt]=='U'){
			rep(i,n){
				mp[i].flip();
				next[i]|=pos[i+1]&mp[i];
				mp[i].flip();
				if(i) next[i]|=pos[i]&mp[i-1];
			}
		}else if(inst[cnt]=='D'){
			rep(i,n){
				mp[i].flip();
				if(i) next[i]|=pos[i-1]&mp[i];
				mp[i].flip();
				next[i]|=pos[i]&mp[i+1];
			}
		}else if(inst[cnt]=='L'){
			rep(i,n){
				next[i]|=(pos[i]>>1)&mp[i];
				next[i]=next[i]<<1;
				mp[i].flip();
				next[i]|=(pos[i]>>1)&mp[i];
				mp[i].flip();
			}
		}else{
			rep(i,n){
				next[i]|=(pos[i]<<1)&mp[i];
				next[i]=next[i]>>1;
				mp[i].flip();
				next[i]|=(pos[i]<<1)&mp[i];
				mp[i].flip();
			}
		}
		rep(i,MAX) pos[i]=next[i];
		cnt++;
	}
	cout<<-1<<endl;
}

#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) ((S&two(X))>0)
#define containL(S,X) ((S&twoL(X))>0)
typedef pair<int,int> ipair;
int countbit(int n) {return (n==0)?0:(1+countbit(n&(n-1)));}
int lowbit(int n) {return (n^(n-1))&n;}
const double pi=acos(-1.0);
const double eps=1e-11;
template<class T> T sqr(T x) {return x*x;}
template<class T> void checkmin(T &a,T b) {if (b<a) a=b;}
template<class T> void checkmax(T &a,T b) {if (b>a) a=b;}
#define SIZE(X) ((int)((X).size()))
#define LENGTH(X) ((int)((X).length()))
#define MP(A,B) make_pair(A,B)
typedef unsigned int uint;

const int maxk=100000+5;

int n,m,k,dstx,dsty;
uint mask[160][5],state[160][5];
uint blockL[160][5],blockR[160][5],blockU[160][5],blockD[160][5];
char g[160][160];
char cmd[maxk];

bool ck()
{
	for (int i=0;i<n;i++)
		if (i!=dstx)
		{
			for (int j=0;j<5;j++) if (state[i][j]) return false;
		}
		else
		{
			for (int j=0;j<5;j++) if (j!=(dsty>>5) && state[i][j]) return false;
			if (state[i][dsty>>5]!=(1u<<(dsty&31))) return false;
		}
	return true;
}
int main()
{
#ifdef _MSC_VER
	freopen("input.txt","r",stdin);
#endif
	memset(mask,0,sizeof(mask));
	scanf("%d%d%d",&n,&m,&k);
	memset(blockL,0,sizeof(blockL));
	memset(blockR,0,sizeof(blockR));
	memset(blockU,0,sizeof(blockU));
	memset(blockD,0,sizeof(blockD));
	for (int i=0;i<n;i++)
	{
		scanf("%s",g[i]);
		for (int j=0;j<m;j++)
			if (g[i][j]!='#')
			{
				mask[i][j>>5]|=(1u<<(j&31));
				if (g[i][j]=='E') dstx=i,dsty=j;
			}
	}
	for (int x=0;x<n;x++) for (int y=0;y<m;y++) if (g[x][y]!='#')
	{
		if (g[x][y-1]=='#') blockL[x][y>>5]|=(1u<<(y&31));
		if (g[x][y+1]=='#') blockR[x][y>>5]|=(1u<<(y&31));
		if (g[x-1][y]=='#') blockU[x][y>>5]|=(1u<<(y&31));
		if (g[x+1][y]=='#') blockD[x][y>>5]|=(1u<<(y&31));
	}
	scanf("%s",cmd);
	memcpy(state,mask,sizeof(state));
	if (ck())
	{
		printf("0\n");
		return 0;
	}
	for (int step=0;step<k;step++)
	{
		char key=cmd[step];
		if (key=='L')
		{
			uint last=0;
			for (int x=0;x<n;x++) for (int i=4;i>=0;i--)
			{
				uint next=state[x][i]&1;
				state[x][i]=(state[x][i]&blockL[x][i])|(state[x][i]>>1);
				if (last) state[x][i]|=(1u<<31);
				last=next;
			}
		}
		else if (key=='R')
		{
			uint last=0;
			for (int x=0;x<n;x++) for (int i=0;i<5;i++)
			{
				uint next=state[x][i]>>31;
				state[x][i]=(state[x][i]&blockR[x][i])|(state[x][i]<<1)|last;
				last=next;
			}
		}
		else if (key=='U')
		{
			for (int x=0;x<n;x++)
			{
				for (int i=0;i<5;i++) state[x][i]&=blockU[x][i];
				if (x+1<n) for (int i=0;i<5;i++) state[x][i]|=state[x+1][i];
			}
		}
		else
		{
			for (int x=n-1;x>=0;x--)
			{
				for (int i=0;i<5;i++) state[x][i]&=blockD[x][i];
				if (x-1>=0) for (int i=0;i<5;i++) state[x][i]|=state[x-1][i];
			}
		}
		for (int x=0;x<n;x++) for (int i=0;i<5;i++) state[x][i]&=mask[x][i];
/*
		for (int x=0;x<n;x++)
		{
			for (int y=0;y<m;y++) if (state[x][y>>5]&(1u<<(y&31)))
				printf("1");
			else
				printf("0");
			printf("\n");
		}
		printf("\n");
*/
		if (ck())
		{
			printf("%d\n",step+1);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li		long long
#define rep(i,to)	for(li i=0;i<((li)to);i++)
#define pb		push_back
#define sz(v)		((li)v.size())

#define MAX 155
int main(){
	bitset<MAX*MAX> goal,mask,wall,now;
	li n,m,k;
	cin>>n>>m>>k;
	rep(i,n){
		string str;
		cin>>str;
		rep(j,m){
			((str[j]=='#')?wall:mask).set(i*MAX+j);
			if(str[j]=='E') goal.set(i*MAX+j);
		}
	}
	string ins;
	cin>>ins;
	now=mask;
	rep(i,sz(ins)){
		if(now==goal){
			cout<<i<<endl;
			return 0;
		}
		if(ins[i]=='U')	now=((now>>MAX)&mask)|(now&(wall<<MAX));
		else if(ins[i]=='D') now=((now<<MAX)&mask)|(now&(wall>>MAX));
		else if(ins[i]=='R') now=((now<<1)&mask)|(now&(wall>>1));
		else if(ins[i]=='L') now=((now>>1)&mask)|(now&(wall<<1));
		else cout<<"error inst"<<endl;
	}
	if(now==goal) cout<<sz(ins)<<endl;
	else cout<<"-1"<<endl;
}

/*AMETHYSTS*/
//#pragma comment(linker, "/STACK:1000000000")
#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#include <fstream>
#include <deque>
#include <stack>
#include <climits>
#include <string>
#include <queue>
#include <memory.h>
#include <map>
#include <unordered_map>

#define ll long long
#define ld double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define mp make_pair

using namespace std;

const int maxn = 155;
char t[(int)1e5 + 10];
char s[maxn][maxn];
bitset <maxn> b[maxn], now[2][maxn], c[maxn];

int main() {
	int n, m, k;

	cin >> n >> m >> k;

	int sum = 0;

	int xx = 0;
	int yy = 0;

	for (int i = 0; i < n; i++) {
		scanf("%s", s[i]);

		for (int j = 0; j < m; j++) {
			if (s[i][j] != '#') {
				b[i][j] = 1;
				now[0][i][j] = 1;
				sum++;
			} else {
				c[i][j] = 1;
			}

			if (s[i][j] == 'E') {
				xx = i;
				yy = j;
			}
		}
	}

	if (sum == 1) {
		cout << 0 << endl;
		return 0;
	}

	int it = 0;

	scanf("%s", t);

	int len = strlen(t);

	for (int i = 0; i < len; i++) {
		int sum = 0;

		if (t[i] == 'L') {
			for (int j = 0; j < n; j++) {
				now[it ^ 1][j] = ((now[it][j] >> 1) & b[j]) | (now[it][j] & (c[j] << 1));
				sum += now[it ^ 1][j].count();
			}
		} else if (t[i] == 'R') {
			for (int j = 0; j < n; j++) {
				now[it ^ 1][j] = ((now[it][j] << 1) & b[j]) | (now[it][j] & (c[j] >> 1));
				sum += now[it ^ 1][j].count();
			}
		} else if (t[i] == 'D') {
			for (int j = 0; j < n; j++) {
				if (j == 0) {
					now[it ^ 1][j] = now[it][j] & c[j + 1];
				} else if (j == n - 1) {
					now[it ^ 1][j] = now[it][j - 1] & b[j];
				} else {
					now[it ^ 1][j] = (now[it][j] & c[j + 1]) | (now[it][j - 1] & b[j]);
				}
				sum += now[it ^ 1][j].count();
			}
		} else {
			for (int j = 0; j < n; j++) {
				if (j == 0) {
					now[it ^ 1][j] = now[it][j + 1] & b[j];
				} else if (j == n - 1) {
					now[it ^ 1][j] = now[it][j] & c[j - 1];
				} else {
					now[it ^ 1][j] = (now[it][j] & c[j - 1]) | (now[it][j + 1] & b[j]);
				}
				sum += now[it ^ 1][j].count();
			}
		}

		it ^= 1;

		if (sum == 1 && now[it][xx][yy] == 1) {
			cout << i + 1 << endl;
			return 0;
		}
	}

	cout << -1 << endl;

	return 0;
}

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define INF 1000000007
#define EPS 1e-10
/*
	Problem: Robot in Basement
	Source: Codeforces 97D
	Author: fts2001
*/
using namespace std;
int n,m,k,ex,ey;
char ch[155][155];
string cmd;
bitset<22505> wal,bsm,cur;
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>m>>k;
	for(int i=0;i<n;i++){
		cin.ignore();
		for(int j=0;j<m;j++){
			cin>>ch[i][j];
			if(ch[i][j]=='E'){
				ex=i;
				ey=j;
				bsm.set(i*m+j);
				cur.set(i*m+j);
			}
			else if(ch[i][j]=='.'){
				bsm.set(i*m+j);
				cur.set(i*m+j);
			}
			else{
				wal.set(i*m+j);
			}
		}
	}
	cin>>cmd;
	if(cur.count()==1 && cur[ex*m+ey]){
		cout<<"0\n";
		return 0;
	}
	for(int i=0;i<cmd.length();i++){
		if(cmd[i]=='L'){
			cur=(cur>>1)&bsm|(wal<<1)&cur;
		}
		else if(cmd[i]=='R'){
			cur=(cur<<1)&bsm|(wal>>1)&cur;
		}
		else if(cmd[i]=='U'){
			cur=(cur>>m)&bsm|(wal<<m)&cur;
		}
		else{
			cur=(cur<<m)&bsm|(wal>>m)&cur;
		}
		if(cur.count()==1 && cur[ex*m+ey]){
			cout<<i+1<<endl;
			return 0;
		}
	}
	cout<<"-1\n";
	return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
using namespace std;

const int N = 155, K = 100005;
int n, m, ex, ey;
char map[N][N], op[K];
bitset<150> st[150], tmp[150], stableUp[150], stableDown[150], stableLeft[150], stableRight[150];

void goup() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableUp[i];
		st[i]  ^= tmp[i];
	}
	for (int i = 0; i + 1 < n; ++i) {
		st[i] = st[i + 1];
	}
	st[n - 1].reset();
	for (int i = 0; i < n; ++i) {
		st[i] |= tmp[i];
	}
}

void godown() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableDown[i];
		st[i] ^= tmp[i];
	}
	for (int i = n - 1; i > 0; --i) {
		st[i] = st[i - 1];
	}
	st[0].reset();
	for (int i = 0; i < n; ++i) {
		st[i] |= tmp[i];
	}
}

void goleft() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableLeft[i];
		st[i] ^= tmp[i];
	}
	for (int i = 0; i < n; ++i) {
		(st[i] >>= 1) |= tmp[i];
	}
}

void goright() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableRight[i];
		st[i] ^= tmp[i];
	}
	for (int i = 0; i < n; ++i) {
		(st[i] <<= 1) |= tmp[i];
	}
}

void initst() {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (map[i][j] != '#') {
				st[i].set(j);
			}
		}
	}
}

void initUDLR() {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (map[i][j] == '#') {
				continue;
			}
			if (i == 0 || map[i - 1][j] == '#') {
				stableUp[i].set(j);
			}
			if (i == n - 1 || map[i + 1][j] == '#') {
				stableDown[i].set(j);
			}
			if (j == 0 || map[i][j - 1] == '#') {
				stableLeft[i].set(j);
			}
			if (j == m - 1 || map[i][j + 1] == '#') {
				stableRight[i].set(j);
			}
		}
	}
}

bool check() {
	int k = st[ex][ey];
	st[ex].reset(ey);
	for (int i = 0; i < n; ++i) {
		if (st[i].any()) {
			st[ex][ey] = k;
			return false;
		}
	}
	return true;
}

int main() {
	int k;
	scanf("%d%d%d", &n, &m, &k);
	int cntone = 0;
	for (int i = 0; i < n; ++i) {
		scanf("%s", map[i]);
		for (int j = 0; j < m; ++j) {
			cntone += map[i][j] != '#';
			if (map[i][j] == 'E') {
				ex = i;
				ey = j;
			}
		}
	}
	if (cntone == 1) {
		printf("0\n");
		return 0;
	}
	initst();
	initUDLR();
	scanf("%s", op);
	for (int i = 0; i < k; ++i) {
		if (op[i] == 'U') {
			goup();
		}
		if (op[i] == 'D') {
			godown();
		}
		if (op[i] == 'L') {
			goleft();
		}
		if (op[i] == 'R') {
			goright();
		}
		if (check()) {
			printf("%d\n", i + 1);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}

#include<bits/stdc++.h>
#define free spasite
using namespace std;

const int N = 155;

int n, m, k;
int ex, ey;
string s;
char a[N][N];
bitset < N > cur[N], free[N], blocked[N], new_cur[N];

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin >> n >> m >> k;
    int ss = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
            if(a[i][j] == '#'){
                blocked[i][j] = true;
            }
            else{
                cur[i][j] = true;
                free[i][j] = true;
                ss++;
                if(a[i][j] == 'E'){
                    ex = i;
                    ey = j;
                }
            }
        }
    }
    if(ss == 1){
        return cout << 0, 0;
    }
    cin >> s;
    for(int z = 1; z <= k; z++){
        auto it = s[z - 1];
        ss = 0;
        if(it == 'L'){
            for(int i = 1; i <= n; i++){
                new_cur[i] = ((free[i] & (cur[i] >> 1)) | (cur[i] & (blocked[i] << 1)));
                ss += new_cur[i].count();
            }
        }
        else if(it == 'R'){
            for(int i = 1; i <= n; i++){
                new_cur[i] = ((free[i] & (cur[i] << 1)) | (cur[i] & (blocked[i] >> 1)));
                ss += new_cur[i].count();
            }
        }
        else if(it == 'D'){
            for(int i = 1; i <= n; i++){
                if(i == 1){
                    new_cur[i] = (cur[i] & blocked[i + 1]);
                }
                else if(i == n){
                    new_cur[i] = (cur[i - 1] & free[i]);
                }
                else{
                    new_cur[i] = ((cur[i] & blocked[i + 1]) | (cur[i - 1] & free[i]));
                }
                ss += new_cur[i].count();
            }
        }
        else{
            for(int i = 1; i <= n; i++){
                if(i == 1){
                    new_cur[i] = (cur[i + 1] & free[i]);
                }
                else if(i == n){
                    new_cur[i] = (cur[i] & blocked[i - 1]);
                }
                else{
                    new_cur[i] = ((cur[i] & blocked[i - 1]) | (cur[i + 1] & free[i]));
                }
                ss += new_cur[i].count();
            }
        }
        if(ss == 1 && new_cur[ex][ey] == 1){
            return cout << z, 0;
        }
        for(int i = 1; i <= n; i++){
            cur[i] = new_cur[i];
        }
    }
    cout << "-1";
}

#include <cstdio>
#include <cassert>
#include <cstring>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

struct Line
{
	unsigned int a[5];
	void setAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0xffffffff;
	}
	void resetAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0;
	}
	void And(const Line &l)
	{
		for(int i = 0; i < 5; i++) a[i] &= l.a[i];
	}
	void Or(const Line &l)
	{
		for(int i = 0; i < 5; i++) a[i] |= l.a[i];
	}
	void shiftRight()
	{
		for(int i = 4; i >= 0; i--)
		{
			a[i] <<= 1;
			if (i != 0 && a[i - 1] >= (unsigned)0x80000000) a[i]++;
		}
	}
	void shiftLeft()
	{
		for(int i = 0; i < 5; i++)
		{
			a[i] >>= 1;
			if (i != 4 && (a[i + 1] & 1) != 0) a[i] += (unsigned)0x80000000;
		}
	}
	void set(int pos, int value)
	{
		if (value)
		{
			a[pos / 32] |= ((unsigned)1 << (pos & 31));
		}
		else
		{
			a[pos / 32] &= ~((unsigned)1 << (pos & 31));
		}
	}
	int countUpToTwo()
	{
		int res = 0;
		unsigned x;
		for(int i = 0; i < 5 && res < 2; i++)
		{
			x = a[i];
			while(x && res < 2)
			{
				x &= x - 1;
				res++;
			}
		}
		return res;
	}

	bool test(int pos)
	{
		return (a[pos / 32] & ((unsigned)1 << (pos & 31))) != 0;
	}
};

int n, m, k;
char s[150][151];
Line a[150], empty[150], wall[150];
char cmd[100001];
int ei, ej;

bool enough()
{
	int cnt = 0;
	for(int i = 0; i < n && cnt < 2; i++) cnt += a[i].countUpToTwo();
	assert(cnt != 0);
	if (cnt > 1) return false;

	return a[ei].test(ej);
}

int main()
{
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 0; i < n; i++) scanf("%s", s[i]);
	scanf("%s", cmd);

	for(int i = 0; i < n; i++)
	{
		a[i].resetAll();
		wall[i].setAll();
		for(int j = 0; j < m; j++)
		{
			if (s[i][j] != '#') a[i].set(j, 1), wall[i].set(j, 0);
			if (s[i][j] == 'E') ei = i, ej = j;
		}
		empty[i] = a[i];
	}

	int pos = 0;
	Line tmp;
	while(pos < k && !enough())
	{
		//for(int i = 0; i < n; i++, puts(""))
		//for(int j = 0; j < m; j++) putchar('0' + (int)a[i].test(j));
		//puts("-----");

		int ch = cmd[pos++];
		if (ch == 'L')
		{
			for(int i = 0; i < n; i++)
			{
				tmp = wall[i];
				tmp.shiftRight();
				tmp.And(a[i]);
				a[i].shiftLeft();
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
		}
		else
		if (ch == 'R')
		{
			for(int i = 0; i < n; i++)
			{
				tmp = wall[i];
				tmp.shiftLeft();
				tmp.And(a[i]);
				a[i].shiftRight();
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
		}
		else
		if (ch == 'U')
		{
			for(int i = 1; i < n - 1; i++)
			{
				tmp = wall[i - 1];
				tmp.And(a[i]);
				a[i] = a[i + 1];
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
			a[0].resetAll();
			a[n - 1].resetAll();
		}
		else
		{
			// ch == 'D'
			for(int i = n - 2; i > 0; i--)
			{
				tmp = wall[i + 1];
				tmp.And(a[i]);
				a[i] = a[i - 1];
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
			a[0].resetAll();
			a[n - 1].resetAll();
		}
	}

	if (pos == k && !enough()) pos = -1;

	printf("%d\n", pos);

	return 0;
}

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=25010;
inline int read(){
	int f=0,x=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[100050];int n,m,k,flag;
int work(){
	bitset<maxn>ch,bl,cn,e;
	for(int i=0;i<n;++i){
		scanf("%s",s1);
		for(int j=0;j<m;++j){
			if(s1[j]=='#')bl[i*m+j]=1;
			else {
				cn[i*m+j]=1;
				if(s1[j]=='E')e[i*m+j]=1;
			}
		}
	}
	scanf("%s",s2);
	ch=cn;
	for(int i=0;i<k;++i){
		if(ch==e)return i;
		if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
		if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
		if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
		if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
	}
	if(ch==e)return k;
	return -1;
}
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	while(~scanf("%d %d %d",&n,&m,&k)){
		write(work());puts("");
	}
	return 0;
}

// new day,new strength,new thougts
#include<bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define F first
#define S second
#define sz(c) int((c).size())
#define REP(i,n)  for(int i=0;i<int(n);i++)
#define REPD(i,n) for(int i=int(n)-1;i>=0;i--)
#define FOR(i,a,b) for(int i=int(a);i<=int(b);i++)
#define FORD(i,a,b) for(int i=int(a);i>=int(b);i--)
#define all(s) (s).begin(),(s).end()
#define bit(num,i) ((num>>i)&1)

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

const ll maxn=153;

vector< bitset<maxn> >is,wl,chk;
bitset<maxn>sv[maxn],rem[maxn];
int n,m,k;
string tdo;

void input(){
    cin>>n>>m>>k;
    is.resize(n+2);
    wl.resize(n+2);
    chk.resize(n+2);
    string str;
    REP(i,n){
	cin>>str;
	REP(j,m){
	    if(str[j]!='#')
		is[i][j]=wl[i][j]=1;
	    if(str[j]=='E')
		chk[i][j]=1;
	}
    }
    cin>>tdo;
}

int main(){
    input();
    REP(w,sz(tdo)){
	if(chk==is){
	    cout<<w;
	    return 0;
	}
	memset(sv,0,sizeof sv);
	memset(rem,0,sizeof rem);
	if(tdo[w]=='U'){
	    REP(i,n){
		rem[i]=is[i+1]&wl[i];
		is[i]=is[i+1];
	    }
	    REP(i,n){
		sv[i]=rem[i]^is[i];
	    }
	    FORD(i,n-1,1){
		sv[i]=sv[i-1];
	    }
	    sv[0]=0;
	    REP(i,n){
		is[i]=rem[i]|sv[i];
	    }
	}
	if(tdo[w]=='D'){
	    FORD(i,n-1,1){
		rem[i]=is[i-1]&wl[i];
		is[i]=is[i-1];
	    }
	    is[0]=rem[0]=0;
	    REP(i,n){
		sv[i]=rem[i]^is[i];
	    }
	    REP(i,n){
		sv[i]=sv[i+1];
	    }
	    REP(i,n){
		is[i]=rem[i]|sv[i];
	    }
	}
	if(tdo[w]=='R'){
	    REP(i,n){
		rem[i]=(is[i]<<1)&wl[i];
		sv[i]=rem[i]^(is[i]<<1);
		rem[i][0]=sv[i][0]=0;
		is[i]=rem[i]|(sv[i]>>1);
	    }
	}
	if(tdo[w]=='L'){
	    REP(i,n){
		rem[i]=(is[i]>>1)&wl[i];
		sv[i]=rem[i]^(is[i]>>1);
		is[i]=rem[i]|(sv[i]<<1);
	    }
	}
	if(chk==is){
	    cout<<w+1;
	    return 0;
	}
    }
    cout<<-1;
}

//lxrtxdy!
#include<iostream>
#include<cstring>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,k;
string ss;
bitset<22500>a,c,b,ans;
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>ss;
        for(int j=0;j<m;j++)
        {
            if(ss[j]=='#')b.set(i*m+j);
            else
            {
                a.set(i*m+j);
                if(ss[j]=='E')ans.set(i*m+j);
            }
        }
    }
    c=a;
    cin>>ss;
    for(int i=0;i<k;i++)
    {
        if(c==ans){cout<<i;return 0;}
        if(ss[i]=='L')c=((c>>1)&a)|((b<<1)&c);
        if(ss[i]=='R')c=((c<<1)&a)|((b>>1)&c);
        if(ss[i]=='U')c=((c>>m)&a)|((b<<m)&c);
        if(ss[i]=='D')c=((c<<m)&a)|((b>>m)&c);
    }
    if(c==ans)cout<<k;else cout<<-1;;
    return 0;
}
//orzlxr!//hello

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<map>
#include<bitset>
#define debug(x) cerr<<"\tdebug:"<<#x<<"="<<x<<endl
#define debugg(x,y) cerr<<"\tdebug;"<<x<<":"<<#y<<"="<<y<<endl
#define debugzu(x,a,b) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++)cerr<<x[i]<<" ";fprintf(stderr,"\n");
#define debugerzu(x,a,b,c,d) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++,fprintf(stderr,"\n\t"))for(int j=c;j<d;j++)cerr<<x[i][j]<<" ";fprintf(stderr,"\n");
#define R register
#define LL long long
#define I inline
using namespace std;
bitset <23000> a,b,c,e;

int main()
{
// 	freopen("CF97D.in","r",stdin);
// 	freopen("CF97D.out","w",stdout);
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	getchar();
	for(int i=0;i<n;i++,getchar())
		for(int j=0;j<m;j++)
		{
			char s=getchar();
			(s=='#'?b:a).set(i*m+j);
			(s=='E'?e.set(i*m+j):0);
		}
	c=a;
	for(int i=0;i<k;i++)
	{
		char s=getchar();
		if(c==e)
		{
			printf("%d\n",i);
			return 0;
		}
		if(s=='U') c = ( (c>>m)&a) | (c&(b<<m));
		else if(s=='L') c = ((c>>1)&a) | (c&(b<<1));
		else if(s=='D') c = ((c<<m)&a) | (c&(b>>m));
		else if(s=='R') c = ((c<<1)&a) | (c&(b>>1));
	}
	if(c==e)
		printf("%d\n",k);
	else
		printf("%d\n",-1);

	fclose(stdin);
	fclose(stdout);
 	return 0;
}

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li       	long long int
#define rep(i,to)	for(li i=0;i<((li)(to));i++)
#define pb       	push_back
#define sz(v)    	((li)v.size())
#define bit(n)   	(1ll<<(li)(n))

#define MAX 155
bitset<MAX> mp[MAX];
bitset<MAX> nmp[MAX];
bitset<MAX> pos[MAX];
bitset<MAX> goal[MAX];
li n,m,k,res;
string base[MAX],inst;

void check(){
	rep(i,n)if(goal[i]!=pos[i]) return;
	cout<<res<<endl;
	exit(0);
}

int main(){
	cin>>n>>m>>k;
	rep(i,n) cin>>base[i];
	cin>>inst;
	rep(i,n)rep(j,m)if(base[i][j]=='E') goal[i][j]=1;
	rep(i,n)rep(j,m)if(base[i][j]=='.' || base[i][j]=='E') pos[i][j]=1;
	rep(i,n)rep(j,m)if(base[i][j]=='#') mp[i][j]=1;
	rep(i,n) nmp[i]=mp[i];
	rep(i,n) nmp[i].flip();

	do{
		check();
		if(res==sz(inst)) break;
		bitset<MAX> tmp[MAX];
		rep(i,n)if(i){
			if(inst[res]=='U') tmp[i]=(mp[i-1]&pos[i])|(nmp[i]&pos[i+1]);
			if(inst[res]=='D') tmp[i]=(mp[i+1]&pos[i])|(nmp[i]&pos[i-1]);
			if(inst[res]=='R') tmp[i]=(nmp[i]&(pos[i]<<1))|((mp[i]>>1)&pos[i]);
			if(inst[res]=='L') tmp[i]=(nmp[i]&(pos[i]>>1))|((mp[i]<<1)&pos[i]);
		}
		rep(i,n) pos[i]=tmp[i];
	}while(++res);
	cout<<"-1"<<endl;
}

#include <cstdio>

#include <cstring>

#include <algorithm>

#include <bitset>

using namespace std;

char s[100005];

int n, m, k;

int work(){

	bitset<23000> a, b, c, e;

	for(int i = 0; i < n; i++)

	{

		scanf("%s", s);

		for(int j = 0; j < m; j++)

		{

			(s[j]=='#'?b:a).set(i*m+j);

			(s[j]=='E'?e.set(i*m+j):0);

		}

	}

	scanf("%s", s);

	c = a;

	for(int i = 0; i < k; i++)

	{

		if(c==e)return i;

		if(s[i]=='U') c = ((c>>m)&a) | (c&(b<<m));

		if(s[i]=='L') c = ((c>>1)&a) | (c&(b<<1));

		if(s[i]=='D') c = ((c<<m)&a) | (c&(b>>m));

		if(s[i]=='R') c = ((c<<1)&a) | (c&(b>>1));

	}

	if(c==e)return k;

	return -1;

}

int main(){

	int i, j;

	while (~scanf("%d %d %d", &n, &m, &k)){

		printf("%d\n", work());

	}

	return 0;

}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define pb push_back
#define __V vector
#define all(x) x.begin(), x.end()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
}
template<typename X, typename Y>
istream& operator>>(istream& in, pair<X, Y> &a) {
	in >> a.first >> a.second;
	return in;
}
template<typename T>
void getv(T& i) {
	cin >> i;
}
template<typename T, typename ... Ns>
void getv(vector<T>& v, int n, Ns ... ns) {
	v.resize(n);
	for (auto& i : v)
		getv(i, ns...);
}
template<typename T>
void getv1(T& i) {
	cin >> i;
}
template<typename T, typename ... Ns>
void getv1(vector<T>& v, int n, Ns ... ns) {
	v.resize(n + 1);
	for (int i = 1; i <= n; i++)
		getv(v[i], ns...);
}
#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
inline void getch(char &x) {
	while (x = getchar_unlocked(), x < 33) {
		;
	}
}
inline void getstr(string &str) {
	str.clear();
	char cur;
	while (cur = getchar_unlocked(), cur < 33) {
		;
	}
	while (cur > 32) {
		str += cur;
		cur = getchar_unlocked();
	}
}
template<typename T> inline bool sc(T &num) {
	bool neg = 0;
	int c;
	num = 0;
	while (c = getchar_unlocked(), c < 33) {
		if (c == EOF)
			return false;
	}
	if (c == '-') {
		neg = 1;
		c = getchar_unlocked();
	}
	for (; c > 47; c = getchar_unlocked())
		num = num * 10 + c - 48;
	if (neg)
		num *= -1;
	return true;
}
typedef unsigned long long ull;
typedef long long ll;
typedef float ld;
typedef int _I;
typedef pair<ll, ll> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
using AntonTsypko = void;
using arsijo = AntonTsypko;
using god = arsijo;
uniform_real_distribution<double> double_dist(0, 1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int S = 151*151 + 100;
int n, m, k;
bitset<S> no[4], msk[4][2], ans, cur;
char mat[160][160];
void setp(bitset<S>& bs, int i, int j, int c = 1) {
	i = i*m + j;
	bs.set(i, c);
}
void shbo(bitset<S>& bs) {
	bs<<=m;
	bs&=no[0];
}
void shup(bitset<S>& bs) {
	bs>>=m;
	bs&=no[1];
}
void shle(bitset<S>& bs) {
	bs>>=1;
	bs&=no[3];
}
void shri(bitset<S>& bs) {
	bs<<=1;
	bs&=no[2];
}
void db(bitset<S>& bs) {
	for(int i = 0; i < n; cout << "\n", i++)
		for(int j = 0; j < m; j++)
			cout << bs[i*m + j];
	cout << "============\n";
}

int main() {
	doin();
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if(i)
				setp(no[0], i, j);
			if(i+1 != n)
				setp(no[1], i, j);
			if(j)
				setp(no[2], i, j);
			if(j+1 != m)
				setp(no[3], i, j);
		}
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			cin >> mat[i][j];
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if(mat[i][j] == 'E') {
				setp(ans, i, j);
			}
			if(mat[i][j] == '#')
				continue;
			setp(cur, i, j);
			setp(msk[0][(!i||mat[i-1][j] == '#')], i, j);//up
			setp(msk[1][(i+1==n||mat[i+1][j] == '#')], i, j);//bottom
			setp(msk[2][(!j||mat[i][j-1] == '#')], i, j);//right
			setp(msk[3][(j+1==m||mat[i][j+1] == '#')], i, j);//left
		}
	string s;
	if(cur == ans) {
		cout << 0;
		return 0;
	}
	cin >> s;
	int k = 0;
	while(k < s.size()) {
		if(s[k] == 'L') {
			bitset<S> t = (cur&msk[2][0]);
			shle(t);
			cur = (cur&msk[2][1])|t;
		} else if(s[k] == 'R') {
			bitset<S> t = (cur&msk[3][0]);
			shri(t);
			cur = (cur&msk[3][1])|t;

		} else if(s[k] == 'U') {
			bitset<S> t = (cur&msk[0][0]);
			shup(t);
			cur = (cur&msk[0][1])|t;
		} else if(s[k] == 'D') {
			bitset<S> t = (cur&msk[1][0]);
			shbo(t);
			cur = (cur&msk[1][1])|t;
		}
		k++;
		if(cur == ans) {
			cout << k << "\n";
			return 0;
		}
	}
	cout << -1;
}

#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define ll long long
#define fi first
#define se second
using namespace std;

template <typename T>
void read(T &x) {
    x = 0; bool f = 0;
    char c = getchar();
    for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
    for (;isdigit(c);c=getchar()) x=x*10+(c^48);
    if (f) x=-x;
}

template<typename F>
inline void write(F x, char ed = '\n') {
	static short st[30];short tp=0;
	if(x<0) putchar('-'),x=-x;
	do st[++tp]=x%10,x/=10; while(x);
	while(tp) putchar('0'|st[tp--]);
	putchar(ed);
}

template <typename T>
inline void Mx(T &x, T y) { x < y && (x = y); }

template <typename T>
inline void Mn(T &x, T y) { x > y && (x = y); }

#include <bitset>
const int N = 155;
const int S = 100500;
bitset<N * N> ed, st, T, L, R, D, U;
char s[S];

int m, n, len;
int num(int x, int y) {
	return (x - 1) * m + y;
}

int main() {
	read(n), read(m), read(len);
	for (int i = 1;i <= n; i++) {
		scanf ("%s", s + 1);
		for (int j = 1;j <= m; j++) {
			if (s[j] == 'E') ed[num(i, j)] = T[num(i, j)] = 1;
			else if (s[j] == '#') {
				if (i != 1) D[num(i - 1, j)] = 1;
				if (i != n) U[num(i + 1, j)] = 1;
				if (j != 1) R[num(i, j - 1)] = 1;
				if (j != m) L[num(i, j + 1)] = 1;
			}
			else T[num(i, j)] = 1;
		}
	}
	scanf ("%s", s + 1); st = T;
	if (T == ed) return puts("0"), 0;
	for (int i = 1;i <= len; i++) {
		if (s[i] == 'U') T = (T >> m & st) | (T & U);
		else if (s[i] == 'D') T = (T << m & st) | (T & D);
		else if (s[i] == 'L') T = (T >> 1 & st) | (T & L);
		else if (s[i] == 'R') T = (T << 1 & st) | (T & R);
		if (ed == T) return write(i), 0;
	}
	write(-1);
	return 0;
}

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <utility>
#include <bitset>
using namespace std;

typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long LL;

#define REP(i, n) for(int i=0; i<n; ++i)
#define ST first
#define ND second
#define PB push_back
#define VAR(v,n) __typeof__(n) v=(n)
#define FE(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define ALL(c) (c).begin(),(c).end()

#define MAXN 151
#define MAXK 100001
char buf[MAXK];

int main(){
    int n, m, k;
    scanf("%d%d%d", &n, &m, &k);

    bitset<MAXN*MAXN> exit, walls;
    walls.flip();

    REP(i, n){
        scanf("%s", buf);
        REP(j, m){
            if ( buf[j] != '#' ) walls.reset( i*MAXN+j );
            if ( buf[j] == 'E' ) exit.set( i*MAXN+j );
        }
        walls.set( i*MAXN + MAXN-1 );
    }

    scanf("%s", buf);

    bitset<MAXN*MAXN> possible = ~walls;

    int turn = 0;
    for(; turn<k; ++turn){
        if ( possible == exit ) break;
        switch(buf[turn]){
            case 'U':
                possible = ( ( possible >> MAXN ) & ( ~ walls ) ) | ( ( walls << MAXN ) & possible );
                break;
            case 'R':
                possible = ( ( possible << 1 ) & ( ~ walls ) ) | ( (walls >> 1) & possible );
                break;
            case 'D':
                possible = ( ( possible << MAXN ) & ( ~ walls ) ) | ( ( walls >> MAXN ) & possible );
                break;
            case 'L':
                possible = ( ( possible >> 1 ) & ( ~ walls ) ) | ( (walls << 1) & possible );
                break;
        }
    }

    if ( possible != exit ) turn = -1;
    printf("%d\n", turn);
return 0;
}

/*

*/
#include <cstdio>
#include <bitset>
#include <cstring>
const int N=153;

std::bitset<N*N> can,ban,now;

int main()
{
	static char s[100005];
	int n,m,K,pos=0; scanf("%d%d%d",&n,&m,&K);
	for(int i=1,now=0; i<=n; ++i)
	{
		scanf("%s",s+1);
		for(int j=1; j<=m; ++j,++now)
		{
			s[j]=='#'?ban[now]=1:can[now]=1;
			if(s[j]=='E') pos=now;
		}
	}
	if(can.count()==1&&can[pos]==1) return putchar('0'),0;
	now=can; scanf("%s",s+1);
	for(int i=1,l=K; i<=l; ++i)
	{
		switch(s[i])
		{
			case 'U': now=(now>>m&can)|(now&(ban<<m)); break;
			case 'D': now=(now<<m&can)|(now&(ban>>m)); break;
			case 'L': now=(now>>1&can)|(now&(ban<<1)); break;
			case 'R': now=(now<<1&can)|(now&(ban>>1)); break;
		}
		if(now.count()==1&&now[pos]==1) return printf("%d\n",i),0;
	}
	puts("-1");

	return 0;
}

#include<iostream>
#include<algorithm>
#include<bitset>
#include<cstring>
#include<string>
using namespace std;
int n,m,k;
string str;
bitset<100005>ok,notok,work;
bitset<100005>end;
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>str;
        for(int j=0;j<m;j++)
        {
            if(str[j]=='#')
            {
                notok.set(m*i+j);
            }
            else
            {
                ok.set(m*i+j);
            }
            if(str[j]=='E')
            {
                end.set(i*m+j);
            }
        }
    }
    cin>>str;
    work=ok;
    int i;
    for(i=0;i<k;i++)
    {
        if(work==end)
        {
            cout<<i<<endl;
            return 0;
        }
        if(str[i]=='U')work=((work>>m)&ok)|(work&(notok<<m));
        if(str[i]=='L')work=((work>>1)&ok)|(work&(notok<<1));
        if(str[i]=='D')work=((work<<m)&ok)|(work&(notok>>m));
        if(str[i]=='R')work=((work<<1)&ok)|(work&(notok>>1));
    }
    if(work==end)cout<<k<<endl;
    else cout<<-1<<endl;
    return 0;
}

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
bitset<22505> a,b,c,d;
int main ( ) {
	int n,m,k,i,j;
	string x;
	scanf("%d%d%d",&n,&m,&k);
	for (i=0;i<n;i++) {
		string x;
		cin>>x;
		for (j=0;j<m;j++) {
			if (x[j]=='#') b.set(i*m+j); else a.set(i*m+j);
			if (x[j]=='E') c.set(i*m+j);
		}
	}
	cin>>x;
	d=a;
    for (i=0;i<k;i++) {
        if (c==d) {printf("%d\n",i);return 0;}
        if (x[i]=='D') d=((d<<m)&a)|(d&(b>>m));
        if (x[i]=='U') d=((d>>m)&a)|(d&(b<<m));
        if (x[i]=='R') d=((d<<1)&a)|(d&(b>>1));
        if (x[i]=='L') d=((d>>1)&a)|(d&(b<<1));
    }
	if (c==d) printf("%d\n",k); else puts("-1");
    return 0;
}
/*
Sample test(s)

Input

5 5 7
#####
#...#
#...#
#E..#
#####
UULLDDR

Output

6

Input

5 5 7
#####
#.#.#
#...#
#E..#
#####
UULLDDR

Output

-1

Input

5 3 2
###
#.#
#.#
#E#
###
DD

Output

2
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn=23010;
int n,m,k;
char s[200][200],op[100010];
bitset<maxn>emp,wal,to,nww;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
	{
		scanf(" %c",s[i]+j);
		if(s[i][j]=='#')wal[(i-1)*m+j]=1;
		else emp[(i-1)*m+j]=1;
		if(s[i][j]=='E')to[(i-1)*m+j]=1;
	}
	nww=emp;
	if(nww==to){printf("0\n");return 0;}
	scanf("%s",op);
	for(int i=0;i<k;i++)
	{
		if(op[i]=='L')nww=(((nww>>1)&emp)|(nww&(wal<<1)));
		if(op[i]=='R')nww=(((nww<<1)&emp)|(nww&(wal>>1)));
		if(op[i]=='U')nww=(((nww>>m)&emp)|(nww&(wal<<m)));
		if(op[i]=='D')nww=(((nww<<m)&emp)|(nww&(wal>>m)));
		if(nww==to){printf("%d\n",i+1);return 0;}
	}
	printf("-1\n");
	return 0;
}

#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <deque>
#include <stack>
#include <map>
#include <utility>
#include <cstring>
#include <sstream>
#include <bitset>
#include <assert.h>
#include <iomanip>
#include <climits>
#include <cstdio>
#include <ctime>

#define DEBUG_FLAG 1
#if DEBUG_FLAG
#define DBG(z) cout << #z << " : " << (z) << "\n";
#define DBY(x,y) cout << #x << " : " << (x) <<" , "<< #y << " : "<< (y) << "\n";
#define DBZ(x,y,z) cout << #x << " : " << (x) <<" , "<< #y << " : "<< (y) << " , " << #z << " : " << (z) << "\n";
#else
#define DBG(z)
#define DBY(x,y)
#define DBZ(x,y,z)
#endif

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset((x),(y),sizeof((x)))
#define all(c) c.begin(),c.end()
#define emp(a,i) (!((a) & (1<<(i))))
#define sh(a) (1<<(a))

#define PI 3.1415926535

using namespace std;
using namespace __gnu_cxx;

int n,m,sz;
char mat[155][155];
char k[110000];
bitset<155> final[155],a[155],b[155];

int main()
{
    scanf("%d%d%d",&n,&m,&sz);
    for(int i=0;i<n;i++)
        scanf("%s",mat[i]);
    for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    if(mat[i][j]=='E')  mat[i][j]='.',final[i][j]=1;
    for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    if(mat[i][j]=='.')  a[i][j]=1;  else b[i][j]=1;
    scanf("%s",k);

    //a mi se kaj moze da sum, b mi e kaj ima precki

   /* for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cout<<a[i][j]<<" ";
            }
            cout<<"\n";
        }
    cout<<"\n\n\n";*/

    int ans=0;
    for(;ans<sz;ans++){
        bool error=0;
        for(int i=0;i<n;i++)
            if(final[i]!=a[i]){
                error=1;
                break;
            }
        if(!error)  break;
        if(k[ans]=='R'){
            for(int i=0;i<n;i++){
                bitset<155> tmp=b[i]>>1;
                tmp&=a[i];
                a[i]^=tmp;
                a[i]<<=1;
                a[i]|=tmp;
            }
        }
        if(k[ans]=='L'){
            for(int i=0;i<n;i++){
                bitset<155> tmp=b[i]<<1;
                tmp&=a[i];
                a[i]^=tmp;
                a[i]>>=1;
                a[i]|=tmp;
            }
        }
        if(k[ans]=='D'){
            for(int i=n-1;i>0;i--)
                a[i]=a[i-1];
            a[0].reset();
            for(int i=1;i<n;i++){
                bitset<155> tmp=a[i]&b[i];
                a[i]^=tmp;
                a[i-1]|=tmp;
            }
        }
        if(k[ans]=='U'){
            for(int i=0;i<n-1;i++)
                a[i]=a[i+1];
            a[n-1].reset();
            for(int i=n-2;i>=0;i--){
                bitset<155> tmp=a[i]&b[i];
                a[i]^=tmp;
                a[i+1]|=tmp;
            }
        }
     /*   for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cout<<a[i][j]<<" ";
            }
            cout<<"\n";
        }
        cout<<"\n\n";*/
    }
    bool error=0;
    for(int i=0;i<n;i++)
        if(final[i]!=a[i]){
            error=1;
            break;
        }
    if(error)  ans=-1;

    printf("%d\n",ans);

    return 0;
}
/*
1 10
6 ....#...##
LLLLLL

1 10
6 #...#....#
RRRRRR

10 1
6
.
.
.
#
.
.
.
.
#
#
DDDDDD

10 1
6
#
#
.
.
.
#
.
.
.
.
UUUUUU
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include<cstring>
#include<string>
#include<bitset>
using namespace std;
bitset <23000> a,b,c,e;

int main()
{

    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    getchar();
    for(int i=0;i<n;i++,getchar())
        for(int j=0;j<m;j++)
        {
            char s=getchar();
            (s=='#'?b:a).set(i*m+j);
            (s=='E'?e.set(i*m+j):0);
        }
    c=a;
    for(int i=0;i<k;i++)
    {
        char s=getchar();
        if(c==e)
        {
            printf("%d\n",i);
            return 0;
        }
        if(s=='U') c = ( (c>>m)&a) | (c&(b<<m));
        else if(s=='L') c = ((c>>1)&a) | (c&(b<<1));
        else if(s=='D') c = ((c<<m)&a) | (c&(b>>m));
        else if(s=='R') c = ((c<<1)&a) | (c&(b>>1));
    }
    if(c==e)
        printf("%d\n",k);
    else
        printf("%d\n",-1);

    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
typedef long long ll;
#define MAX 305
using namespace std;
int n,m;
int k;
#define INF 2147483647
#define MOD 1000000007
#define EPS 1e-8
#define G 9.8
#define PI acos(-1)
bool debug = false;

bitset<22600> init,wall, E, pos;
char s[100005];
int main()
{
    scanf("%d%d%d", &n, &m, &k);
    int idx = 0;
    for(int i=0;i<n;i++) {
        scanf("%s", s);
        for(int j=0;j<m;j++) {
            if (s[j] == '#') wall[idx]=1;
            else {
                init[idx] = 1; if(s[j]=='E') E[idx]=1;
            }
            idx++;
        }
    }
    pos = init;
    if (pos == E) {puts("0"); return 0;}
    scanf("%s", s); int ans = -1;
    for(int i=0;i<k;i++) {
        if (s[i]=='U') pos = ((wall<<m)&pos) | ((pos>>m) & init);
        if (s[i]=='D') pos = ((wall>>m)&pos) | ((pos<<m) & init);
        if (s[i]=='L') pos = ((wall<<1)&pos) | ((pos>>1) & init);
        if (s[i]=='R') pos = ((wall>>1)&pos) | ((pos<<1) & init);
        if (pos == E) {ans = i+1; break;}
    }
    printf("%d\n", ans);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn=155,maxk=100005;
bitset<maxn*maxn> now,wall,pass,goal;
char opr[maxk],field[maxn][maxn];
int n,m,k;
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%s",field[i]);
        for(int j=0;j<m;j++)
            if(field[i][j]!='#')
            {
                now.set(i*m+j);
                if(field[i][j]=='E')
                    goal.set(i*m+j);
            }
            else
                wall.set(i*m+j);
    }
    pass=now;
    scanf("%s",opr);
    if(now==goal)
    {
        puts("0");
        return 0;
    }
    for(int i=0;i<k;i++)
    {
        if(opr[i]=='U')
            now=((now>>m)&pass)|(now&(wall<<m));
        if(opr[i]=='D')
            now=((now<<m)&pass)|(now&(wall>>m));
        if(opr[i]=='L')
            now=((now>>1)&pass)|(now&(wall<<1));
        if(opr[i]=='R')
            now=((now<<1)&pass)|(now&(wall>>1));
        if(now==goal)
        {
            printf("%d\n",i+1);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

#include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define ll long long
//#define int long long
#define pb push_back
#define x first
#define ld long double
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) a.begin(),a.end()

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;

/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
    return uniform_int_distribution<int>(l, r)(rng);
}
const int N = 152;
bitset<N> karta[N], L[N], R[N], U[N], D[N], E[N];
main()
{
//    ios::sync_with_stdio(0);
//    ios_base::sync_with_stdio(0);
//    cin.tie(0);
//    cout.tie(0);
    int n, m, k, x, y;
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            L[i][j] = R[i][j] = D[i][j] = L[i][j] = E[i][j] = 0;
            E[i][j] = 1;
        }
    }
    vector <string> a(n);
    int kol = n * m;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        for (int j = 0; j < m; j++) {
            if (a[i][j] == 'E') {
                x = i;
                y = j;
            }
            if (a[i][j] != '#') karta[i][j] = 1;
            if (a[i][j] == '#') {
                if (j) R[i][j - 1] = 1;
                if (j + 1 != m) L[i][j + 1] = 1;
                if (i) D[i - 1][j] = 1;
                if (i + 1 != n) U[i + 1][j] = 1;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i][j] == '#') {
                E[i][j] = 0;
                --kol;
            }
        }
    }
    if (kol == 1) {
        cout << 0;
        rr;
    }
    string s;
    cin >> s;
    for (int i = 0; i < k; i++) {
        if (s[i] == 'U') {
            for (int j = 0; j < n; j++) {
                if (j != n - 1) {
                    karta[j] = (karta[j + 1] & E[j]) | (karta[j] & U[j]);
                }
                else {
                    karta[j] &= U[j];
                }
            }
        }
        if (s[i] == 'D') {
            for (int j = n - 1; j >= 0; j--) {
                if (j) {
                    karta[j] = (karta[j - 1] & E[j]) | (karta[j] & D[j]);
                }
                else {
                    karta[j] &= D[j];
                }
            }
        }
        if (s[i] == 'L') {
            for (int j = 0; j < n; j++) {
                karta[j] = (E[j] & (karta[j] >> 1)) | (karta[j] & L[j]);
            }
        }
        if (s[i] == 'R') {
            for (int j = 0; j < n; j++) {
                karta[j] = (E[j] & (karta[j] << 1)) | (karta[j] & R[j]);
            }
        }
//        for (int j = 0; j < n; j++) {
//            for (int k = 0; k < m; k++) {
//                cout << karta[j][k];
//            }
//            cout << '\n';
//        }
//        cout << '\n';
//        char c;
//        cin >> c;
        int cnt = 0;
        for (int j = 0; j < n; j++) cnt += karta[j].count();
        if (cnt == 1) {
//            cout << "COOL " << i + 1 << '\n';
            for (int j = 0; j < n; j++) {
                if (karta[j].count()) {
                    int x1 = j, y1 = karta[j]._Find_first();
                    if (x1 == x && y1 == y) {
                        cout << i + 1;
                        rr;
                    }
                }
            }
        }
    }
    cout << -1;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;

typedef long long LL;

inline int read()
{
	char c=getchar();int num=0,f=1;
	for(;!isdigit(c);c=getchar())
		f=c=='-'?-1:f;
	for(;isdigit(c);c=getchar())
		num=num*10+c-'0';
	return num*f;
}

const int N=25005;

int n,m,l;
bitset<N> a,b,c,d;
char s[100005];

int main()
{
	n=read(),m=read(),l=read();
	for(int i=0;i<n;++i)
	{
		scanf("%s",s);
		for(int j=0;j<m;++j)
		{
			if(s[j]=='#') b.set(i*m+j);
			else a.set(i*m+j);
			if(s[j]=='E') d.set(i*m+j);
		}
	}
	scanf("%s",s+1);c=a;
	if(c==d)
	{
		puts("0");
		return 0;
	}
	for(int i=1;i<=l;++i)
	{
		if(s[i]=='U') c=(c>>m&a)|(c&(b<<m));
		else if(s[i]=='D') c=(c<<m&a)|(c&(b>>m));
		else if(s[i]=='L') c=(c>>1&a)|(c&(b<<1));
		else c=(c<<1&a)|(c&(b>>1));
		if(c==d)
		{
			printf("%d",i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}//hello

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <sstream>
#include <iostream>
#include <set>
#include <map>
#include <cassert>
#include <bitset>

#define REP(AA,BB) for(int AA=0; AA<(BB); ++AA)
#define FOR(AA,BB,CC) for(int AA=(BB); AA<(CC); ++AA)
#define FC(AA,BB) for(__typeof((AA).begin()) BB=(AA).begin(); BB!=(AA).end(); ++BB)
#define SZ(AA) ((int)((AA).size()))
#define ALL(AA) (AA).begin(), (AA).end()
#define PB push_back
#define MP make_pair

using namespace std;

typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long long LL;
typedef long double LD;

const int MAXN = 150, MAXM = 100000;
typedef bitset<MAXN> bset;

bset W[MAXN + 10];
bset cur[MAXN + 10], final[MAXN + 10];
char buf[MAXN + 10], seq[MAXM + 10];

int main(void) {
    int n, m, K; scanf("%d%d%d", &n, &m, &K);
    REP (i, n) {
        scanf("%s", buf);
        REP (j, m) {
            if (buf[j] == '#') {
                W[i][j] = 1;
            } else {
                cur[i][j] = 1;
                if (buf[j] == 'E') {
                    final[i][j] = 1;
                }
            }
        }
    }
    scanf("%s", seq);
    REP (f, K + 1) {
        bool ok = true;
        REP (i, n) {
            if (cur[i] != final[i]) {
                ok = false;
            }
        }
        if (ok) {
            printf("%d\n", f);
            return 0;
        }
        if (f == K) {
            break;
        }
        if (seq[f] == 'R') {
            REP (i, n) {
                bset w = cur[i] & (W[i] >> 1);
                cur[i] = ((cur[i] ^ w) << 1) | w;
            }
        } else if (seq[f] == 'L') {
            REP (i, n) {
                bset w = cur[i] & (W[i] << 1);
                cur[i] = ((cur[i] ^ w) >> 1) | w;
            }
        } else if (seq[f] == 'U') {
            FOR (i, 1, n) {
                bset w = cur[i] & (W[i - 1]);
                cur[i - 1] |= (cur[i] ^ w);
                cur[i] = w;
            }
        } else if (seq[f] == 'D') {
            for (int i = n - 2; i >= 0; --i) {
                bset w = cur[i] & (W[i + 1]);
                cur[i + 1] |= (cur[i] ^ w);
                cur[i] = w;
            }
        }
    }
    printf("%d\n", -1);
    return 0;
}

#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
using namespace std;

struct IO
{
	#define MAXSIZE (1 << 20)
	#define isdigit(x) (x >= '0' && x <= '9')
  	char buf[MAXSIZE],*p1,*p2;
  	char pbuf[MAXSIZE],*pp;
  	IO():p1(buf),p2(buf),pp(pbuf){}
  	inline char gc() {
    	return getchar();
    	if (p1==p2) p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
    	return p1==p2?' ':*p1++;
  	}
	inline bool blank(char ch) {return ch==' '||ch =='\n'||ch == '\r'||ch == '\t';}
  	template <class T>
  	inline void read(T &x)
	{
	    register double tmp=1;register bool sign=0; x=0;
	    register char ch=gc();
	    for (;!isdigit(ch);ch=gc()) if(ch=='-') sign=1;
	    for (;isdigit(ch);ch=gc()) x=x*10+(ch-'0');
	    if (ch=='.') for (ch=gc();isdigit(ch);ch=gc()) tmp/=10.0,x+=tmp*(ch-'0');
	    if (sign) x=-x;
  	}
  	inline void read(char *s)
	{
    	register char ch=gc();
		for (;blank(ch);ch=gc());
		for (;!blank(ch);ch=gc()) *s++=ch;
    	*s=0;
  	}
  	inline void read(char &c) {for(c=gc();blank(c);c=gc());}
  	template<class t> inline void write(t x){
		if(x<0) putchar('-'),write(-x);
		else{if(x>9) write(x/10);putchar('0'+x%10);}
	}
} io;

const int mod=1e9+7;
const int mo=998244353;
const int N=155;
const int M=1e5+5;

int n,m,l,PE;
char a[N][N],opt[M];
bitset<N*N> can,bn,now;

int main()
{
	scanf("%d%d%d",&n,&m,&l);
	For(i,1,n) scanf("%s",a[i]+1);
	scanf("%s",opt+1);
	For(i,1,n) For(j,1,m)
	{
		if(a[i][j]=='#') bn.set((i-1)*m+j);
		else can.set((i-1)*m+j);
		if(a[i][j]=='E') PE=(i-1)*m+j;
	}
	now=can;
	if(now.count()==1&&now[PE]==1) return printf("%d\n",0),0;
	For(i,1,l)
	{
		if(opt[i]=='U') now=((now>>m)&can)|(now&(bn<<m));
		if(opt[i]=='D') now=((now<<m)&can)|(now&(bn>>m));
		if(opt[i]=='L') now=((now>>1)&can)|(now&(bn<<1));
		if(opt[i]=='R') now=((now<<1)&can)|(now&(bn>>1));
		if(now.count()==1&&now[PE]==1) return printf("%d\n",i),0;
	}
	puts("-1");
	return 0;
}

#include <queue>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define u32 unsigned int
using namespace std;
const int MAXN = 1e5 + 10, SS = 150 * 150 + 150;
int N, M, Len, id;
bitset<SS> now, B, can;
char s[151], str[MAXN];
int main() {
	scanf("%d%d%d", &N, &M, &Len);
	for(int i = 1; i <= N; i++) {
		scanf("%s", s + 1);
		for(int j = 1; j <= M; j++) {
			if(s[j] == '#') B[i * M + j] = 1;
			else can[i * M + j] = now[i * M + j] = 1;
			if(s[j] == 'E') id = i * M + j;
		}
	}
	scanf("%s", str + 1);
	if(now.count() == 1 && now[id] == 1) return puts("0"), 0;
	for(int i = 1; i <= Len; i++) {
		if(str[i] == 'U') now = ((now >> M) & can) | (now & (B << M));
		else if(str[i] == 'D') now = ((now << M) & can) | (now & (B >> M));
		else if(str[i] == 'L') now = ((now >> 1) & can) | (now & (B << 1));
		else if(str[i] == 'R') now = ((now << 1) & can) | (now & (B >> 1));
		if(now.count() == 1 && now[id] == 1) return printf("%d\n", i), 0;
	}
	puts("-1");
	return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
char s[100005];
int n, m, k;
int work(){
    bitset<23000> a, b, c, e;
    for(int i = 0; i < n; i++)
    {
        scanf("%s", s);
        for(int j = 0; j < m; j++)
        {
            (s[j]=='#'?b:a).set(i*m+j);
            (s[j]=='E'?e.set(i*m+j):0);
        }
    }
    scanf("%s", s);
    c = a;
    for(int i = 0; i < k; i++)
    {
        if(c==e)return i;
        if(s[i]=='U') c = ((c>>m)&a)|(c&(b<<m));
        if(s[i]=='L') c = ((c>>1)&a)|(c&(b<<1));
        if(s[i]=='D') c = ((c<<m)&a)|(c&(b>>m));
        if(s[i]=='R') c = ((c<<1)&a)|(c&(b>>1));
    }
    if(c==e)return k;
    return -1;
}
int main(){
    int i, j;
    while (~scanf("%d %d %d", &n, &m, &k)){
        printf("%d\n", work());
    }
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;

typedef long long LL;

inline int read()
{
	char c=getchar();int num=0,f=1;
	for(;!isdigit(c);c=getchar())
		f=c=='-'?-1:f;
	for(;isdigit(c);c=getchar())
		num=num*10+c-'0';
	return num*f;
}

const int N=25005;

int n,m,l;
bitset<N> a,b,c,d;
char s[100005];

int main()
{
	n=read(),m=read(),l=read();
	for(int i=0;i<n;++i)
	{
		scanf("%s",s);
		for(int j=0;j<m;++j)
		{
			if(s[j]=='#') b.set(i*m+j);
			else a.set(i*m+j);
			if(s[j]=='E') d.set(i*m+j);
		}
	}
	scanf("%s",s+1);c=a;
	if(c==d)
	{
		puts("0");
		return 0;
	}
	for(int i=1;i<=l;++i)
	{
		if(s[i]=='U') c=(c>>m&a)|(c&(b<<m));
		else if(s[i]=='D') c=(c<<m&a)|(c&(b>>m));
		else if(s[i]=='L') c=(c>>1&a)|(c&(b<<1));
		else c=(c<<1&a)|(c&(b>>1));
		if(c==d)
		{
			printf("%d",i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cassert>
#include <cstdlib>
#include <ctime>

using namespace std;
typedef long long int64;
#define E(c) cerr<<#c
#define Eo(x) cerr<<#x<<" = "<<(x)<<endl
typedef unsigned int dword;

const int SIZE = 160;
const int SZ = (SIZE + 31) / 32;

void Set(dword *arr, int i) {
	arr[i >> 5] |= 1 << (i & 31);
}
dword Get(dword *arr, int i) {
	return (arr[i >> 5] >> (i & 31)) & 1;
}

int n, m;
char pict[SIZE][SIZE];

struct Field {
	dword a[SIZE][SZ];
};

int k;
char comms[1<<20];

int cc;
Field cf[2];

int exitx, exity;
Field pass;

int main() {
#ifdef HOME
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 0; i<n; i++)
		scanf("%s", pict[i]);

	int freecells = 0;
	memset(&pass, 0, sizeof(pass));
	for (int i = 0; i<n; i++)
		for (int j = 0; j<m; j++) {
			if (pict[i][j] != '#') {
				Set(pass.a[i + 3], j + 3);
				freecells++;
			}
			if (pict[i][j] == 'E') {
				exitx = i + 3;
				exity = j + 3;
			}
		}
	if (freecells == 1) {
		printf("0\n");
		return 0;
	}

	cc = 0;
	memcpy(&cf[cc], &pass, sizeof(pass));

	scanf("%s", comms);
	for (int q = 0; q<=k; q++) {

/*        for (int i = 0; i<SIZE; i++) {
        	for (int j = 0; j<SIZE; j++) {
        		char c = Get(pass.a[i], j) ? '.' : '#';
        		c += Get(cf[cc].a[i], j) ? '*' - '.' : 0;
        		printf("%c", c);
        	}
        	printf("\n");
        }*/
        if (q == k) break;

		char c = comms[q];
		bool final = true;
		for (int i = 0; i<SIZE; i++) {
			const Field &curr = cf[cc];
			Field &next = cf[cc^1];

			for (int j = 0; j<SZ; j++) {
				dword pos = curr.a[i][j];
				dword mask = pass.a[i][j];
				dword movpos = 0;
				dword revmask = 0;

				switch (c) {
					case 'R':
						movpos = (pos << 1);
						revmask = (mask >> 1);
						if (j > 0) movpos ^= curr.a[i][j-1] >> 31;
						if (j < SZ-1) revmask ^= (pass.a[i][j+1] & dword(1)) << 31;
						break;
					case 'L':
						movpos = (pos >> 1);
						revmask = (mask << 1);
						if (j < SZ-1) movpos ^= (curr.a[i][j+1] & dword(1)) << 31;
						if (j > 0) revmask ^= pass.a[i][j-1] >> 31;
						break;
					case 'D':
						if (i > 0) movpos = curr.a[i-1][j];
						if (i < SIZE-1) revmask = pass.a[i+1][j];
						break;
					case 'U':
						if (i < SIZE-1) movpos = curr.a[i+1][j];
						if (i > 0) revmask = pass.a[i-1][j];
						break;
				}

				dword res = (movpos & mask) | (pos & (~revmask));
				next.a[i][j] = res;

				if (final && res) {
					if (!(i == exitx && j == (exity >> 5) && res == (dword(1) << (exity & 31))))
						final = false;
				}
			}
        }
        cc ^= 1;

        if (final) {
			printf("%d\n", q+1);
			return 0;
        }
	}

	printf("-1\n");
	return 0;
}

//lxrtxdy!
#include<iostream>
#include<cstring>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,k;
string ss;
bitset<22500>a,c,b,ans;
int main()
{
    cin>>n>>m>>k;
    for(int i=0;i<n;i++)
    {
        cin>>ss;
        for(int j=0;j<m;j++)
        {
            if(ss[j]=='#')b.set(i*m+j);
            else
            {
                a.set(i*m+j);
                if(ss[j]=='E')ans.set(i*m+j);
            }
        }
    }
    c=a;
    cin>>ss;
    for(int i=0;i<k;i++)
    {
        if(c==ans){cout<<i;return 0;}
        if(ss[i]=='L')c=((c>>1)&a)|((b<<1)&c);
        if(ss[i]=='R')c=((c<<1)&a)|((b>>1)&c);
        if(ss[i]=='U')c=((c>>m)&a)|((b<<m)&c);
        if(ss[i]=='D')c=((c<<m)&a)|((b>>m)&c);
    }
    if(c==ans)cout<<k;else cout<<-1;;
    return 0;
}
//orzlxr!

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<bitset>

#define ls (t<<1)
#define rs ((t<<1)+1)
#define mid ((l+r)>>1)
#define fi first
#define se second
#define mk make_pair

#define N 155
#define M 200005
#define seed 23333
#define Mo 998244353

using namespace std;
int i,j,m,n,p,k,tot,End,id[N][N];
bitset<N*N>A,L,R,U,D,B,C;
char c[N][N],s[M];
int main()
{
	   scanf("%d%d%d",&n,&m,&k);
	   for (i=0;i<n;++i) for (j=0;j<m;++j) id[i][j]=tot++;
	   for (i=0;i<n;++i)
	   {
	   	     scanf("%s",c[i]);
	   	     for (j=0;j<m;++j)  if (c[i][j]=='E') End=id[i][j];
	   }
	   for (i=0;i<n;++i)
	     for (j=0;j<m;++j)
		   if (c[i][j]!='#')
		   {
		     	 A[id[i][j]]=1;
		     	 if (c[i-1][j]=='#') U[id[i][j]]=1;
		     	 if (c[i+1][j]=='#') D[id[i][j]]=1;
		     	 if (c[i][j-1]=='#') L[id[i][j]]=1;
		     	 if (c[i][j+1]=='#') R[id[i][j]]=1;
		   }
	   scanf("%s",s);
	   for (i=0;i<n*m;++i) B[i]=1;
	   for (i=0;i<=k;++i)
	   {
	   	   C=A&B;
	   	   if (C.count()==1&&A[End]) break;
	   	   if (s[i]=='L') A=(A&L)|((A^(A&L))>>1);
	   	   if (s[i]=='R') A=(A&R)|((A^(A&R))<<1);
	   	   if (s[i]=='U') A=(A&U)|((A^(A&U))>>m);
	   	   if (s[i]=='D') A=(A&D)|((A^(A&D))<<m);
	   }
	   if (i>k) printf("-1\n"); else printf("%d\n",i);
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
const int maxn=23010;
int n,m,k;
char s[200][200],op[100010];
bitset<maxn>emp,wal,to,nww;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
	{
		scanf(" %c",s[i]+j);
		if(s[i][j]=='#')wal[(i-1)*m+j]=1;
		else emp[(i-1)*m+j]=1;
		if(s[i][j]=='E')to[(i-1)*m+j]=1;
	}
	nww=emp;
	if(nww==to){printf("0\n");return 0;}
	scanf("%s",op);
	for(int i=0;i<k;i++)
	{
		if(op[i]=='L')nww=(((nww>>1)&emp)|(nww&(wal<<1)));
		if(op[i]=='R')nww=(((nww<<1)&emp)|(nww&(wal>>1)));
		if(op[i]=='U')nww=(((nww>>m)&emp)|(nww&(wal<<m)));
		if(op[i]=='D')nww=(((nww<<m)&emp)|(nww&(wal>>m)));
		if(nww==to){printf("%d\n",i+1);return 0;}
	}
	printf("-1\n");
	return 0;
}

#include<iostream>
#include<cstdio>
#include<bitset>
using namespace std;

const int MAXN = 155;

int n, m, p, id, ed; char s[100010];
bitset<MAXN * MAXN> ban, can, now;

int main()
{
	scanf("%d %d %d", &n, &m, &p);
	for(int i = 0; i < n; ++i)
	{
		scanf("%s", s);
		for(int j = 0; j < m; ++j)
		{
			id = i * m + j;
			if(s[j] == '#') ban[id] = 1;
			else can[id] = 1;
			if(s[j] == 'E') ed = id;
		}
	}
	scanf("%s", s); now = can;
	if(now.count() == 1 && now[ed] == 1) return puts("0"), 0;
	for(int i = 0; i < p; ++i)
	{
		if(s[i] == 'R') now = ((now << 1) & can) | (now & (ban >> 1));
		else if(s[i] == 'L') now = ((now >> 1) & can) | (now & (ban << 1));
		else if(s[i] == 'D') now = ((now << m) & can) | (now & (ban >> m));
		else now = ((now >> m) & can) | (now & (ban << m));
		if(now.count() == 1 && now[ed] == 1) return printf("%d\n", i + 1), 0;
	}
	puts("-1");
	return 0;
}

#include <iostream>
#include <bitset>
using namespace std;
int n,m,k;
string st;
int main()
{
	int i,j;
	bitset<23005> a,b,c,e;
	cin>>n>>m>>k;
	for (i=0;i<n;i=i+1)
	{
		cin>>st;
		for (j=0;j<m;j=j+1)
		{
			if (st[j]=='#') b.set(i*m+j);else a.set(i*m+j);
			if (st[j]=='E') e.set(i*m+j);
		}
	}
	cin>>st;
	c=a;
	for (i=0;i<k;i=i+1)
	{
		if (c==e) {cout<<i<<endl;return 0;}
        if (st[i]=='U') c=((c>>m)&a)|(c&(b<<m));
        if (st[i]=='D') c=((c<<m)&a)|(c&(b>>m));
        if (st[i]=='L') c=((c>>1)&a)|(c&(b<<1));
        if (st[i]=='R') c=((c<<1)&a)|(c&(b>>1));
	}
	if (c==e) cout<<i<<endl;else cout<<-1<<endl;
	return 0;
}

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
bitset<22505> a,b,c,d;
int main ( ) {
	int n,m,k,i,j;
	string x;
	scanf("%d%d%d",&n,&m,&k);
	for (i=0;i<n;i++) {
		string x;
		cin>>x;
		for (j=0;j<m;j++) {
			if (x[j]=='#') b.set(i*m+j); else a.set(i*m+j);
			if (x[j]=='E') c.set(i*m+j);
		}
	}
	cin>>x;
	d=a;
    for (i=0;i<k;i++) {
        if (c==d) {printf("%d\n",i);return 0;}
        if (x[i]=='D') d=((d<<m)&a)|(d&(b>>m));
        if (x[i]=='U') d=((d>>m)&a)|(d&(b<<m));
        if (x[i]=='R') d=((d<<1)&a)|(d&(b>>1));
        if (x[i]=='L') d=((d>>1)&a)|(d&(b<<1));
    }
	if (c==d) printf("%d\n",k); else puts("-1");
    return 0;
}
/*
Sample test(s)

Input

5 5 7
#####
#...#
#...#
#E..#
#####
UULLDDR

Output

6

Input

5 5 7
#####
#.#.#
#...#
#E..#
#####
UULLDDR

Output

-1

Input

5 3 2
###
#.#
#.#
#E#
###
DD

Output

2
*/

#include<iostream>
#include<cstdio>
#include<bitset>
#include<cstring>
using namespace std;
int n,m,k,p[200][200];
bitset<40000>a,c,b,e;
char ch[100009];

int work()
{
	for(int i=0;i<n;i++)
	{
		scanf("%s",ch);
		for(int j=0;j<m;j++)
		{
			if(ch[j]=='#')b[i*m+j]=1;
			else a[i*m+j]=1;
			if(ch[j]=='E')e[i*m+j]=1;
		}
	}
//	cout<<e<<"eeeeeeeeeee";
	cin>>ch;
	c=a;
	for(int i=0;i<k;i++)
	{
		if(c==e)return i;
		if(ch[i]=='L')c=(((c>>1)&a)|(b<<1)&c);
		if(ch[i]=='R')c=(((c<<1)&a)|(b>>1)&c);
		if(ch[i]=='U')c=(((c>>m)&a)|(b<<m)&c);
		if(ch[i]=='D')c=(((c<<m)&a)|(b>>m)&c);
		//cnt++;
	//cout<<x<<endl;
	}
	if(c==e)return k;
	return -1;
}
int main()
{
	cin>>n>>m>>k;
	// O_O();
	cout<<work();
	}

#include<bits/stdc++.h>
#define u32 unsigned int
using namespace std;
const int MAXN = 1e5 + 10, SS = 150 * 150 + 150;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, M, Len, id;
bitset<SS> now, B, can;
char s[151], str[MAXN];
int main() {
	N = read(); M = read(); Len = read();
	for(int i = 1; i <= N; i++) {
		scanf("%s", s + 1);
		for(int j = 1; j <= M; j++) {
			if(s[j] == '#') B[i * M + j] = 1;
			else can[i * M + j] = now[i * M + j] = 1;
			if(s[j] == 'E') id = i * M + j;
		}
	}
	scanf("%s", str + 1);
	if(now.count() == 1 && now[id] == 1) return puts("0"), 0;
	for(int i = 1; i <= Len; i++) {
		if(str[i] == 'U') {
			now = ((now >> M) & can) | (now & (B << M));
		} else if(str[i] == 'D') {
			now = ((now << M) & can) | (now & (B >> M));
		} else if(str[i] == 'L') {
			now = ((now >> 1) & can) | (now & (B << 1));
		} else if(str[i] == 'R') {
			now = ((now << 1) & can) | (now & (B >> 1));
		}
		if(now.count() == 1 && now[id] == 1) {
			printf("%d\n", i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}

#include <bitset>
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>

using namespace std;

typedef bitset<150> line;

char nextchar() {
  char ch;
  do {
    scanf("%c", &ch);
  } while (ch <= ' ');
  return ch;
}

int main() {
  int height, width, numQueries;
  scanf("%d", &height);
  scanf("%d", &width);
  scanf("%d", &numQueries);
  vector<line> mask(height);
  vector<line> wall(height);
  int er = -1, ec = -1;
  for (int r = 0; r < height; ++r) {
    for (int c = 0; c < width; ++c) {
      char ch = nextchar();
      if (ch == 'E') {
        er = r;
	ec = c;
      }
      if (ch == '#') {
        wall[r].set(c);
      } else {
        mask[r].set(c);
      }
    }
  }
  vector<line> needMask(height);
  needMask[er].set(ec);
  int qId;
  for (qId = 0; qId <= numQueries; ++qId) {
    if (needMask == mask) break;
    if (qId < numQueries) {
      char ch = nextchar();
      switch (ch) {
        case 'L':
	  for (int r = 0; r < height; ++r) {
	    line tmp = mask[r];
	    tmp >>= 1;
	    line cross = tmp & wall[r];
	    tmp ^= cross;
	    cross <<= 1;
	    tmp |= cross;
	    mask[r] = tmp;
	  }
	  break;
	case 'R':
	  for (int r = 0; r < height; ++r) {
	    line tmp = mask[r];
	    tmp <<= 1;
	    line cross = tmp & wall[r];
	    tmp ^= cross;
	    cross >>= 1;
	    tmp |= cross;
	    mask[r] = tmp;
	  }
	  break;
	case 'U':
	  for (int r = 1; r < height; ++r) {
	    mask[r - 1] = mask[r];
	  }
	  for (int r = height - 2; r >= 0; --r) {
	    line tmp = mask[r];
	    line cross = tmp & wall[r];
	    tmp ^= cross;
	    mask[r + 1] |= cross;
	    mask[r] = tmp;
	  }
	  break;
	case 'D':
	  for (int r = height - 2; r >= 0; --r) {
	    mask[r + 1] = mask[r];
	  }
	  for (int r = 1; r < height; ++r) {
	    line tmp = mask[r];
	    line cross = tmp & wall[r];
	    tmp ^= cross;
	    mask[r - 1] |= cross;
	    mask[r] = tmp;
	  }
	  break;
      }
    }
  }
  if (qId > numQueries) qId = -1;
  printf("%d\n", qId);
  return 0;
}

#include <set>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <ctype.h>
#include <iostream>
#include <stack>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <cmath>
#include <bitset>
using namespace std;

char mmap[151][151];
int keydir[255];
int dx[4]={0,+1,0,-1};
int dy[4]={-1,0,+1,0};

char command[100001];
int main()
{
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0;i<n;i++)
		scanf("%s",mmap[i]);
	bitset<150*150> T,dir[4][2],E;
	keydir['U']=0;
	keydir['R']=1;
	keydir['D']=2;
	keydir['L']=3;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			if(mmap[i][j]=='E')
				E[i*m+j]=1;
			if(mmap[i][j]!='#')
			{
				T[i*m+j]=1;
				for(int k=0;k<4;k++)
				{
					if(mmap[i+dy[k]][j+dx[k]]!='#')
						dir[k][0][i*m+j]=1;
					else
						dir[k][1][i*m+j]=1;
				}
			}
		}
//	cout<<T.to_string()<<endl;
//	cout<<"________"<<endl;
//	cout<<dir[0][0]<<endl;
//	cout<<dir[0][1]<<endl;
	{

		int i;
		scanf("%s",command);
		for( i=0;command[i]!='\0';i++)
		{
			if(T==E)
			{
				printf("%d\n",i);
				return 0;
			}
			switch(command[i])
			{
			case 'U':
				T=((T&dir[0][1])|((T&dir[0][0])>>m));
				break;
			case 'R':
				T=((T&dir[1][1])|((T&dir[1][0])<<1));
				break;
			case 'D':
				T=((T&dir[2][1])|((T&dir[2][0])<<m));
				break;
			case 'L':
				T=((T&dir[3][1])|((T&dir[3][0])>>1));
				break;
			}
//			for(int i=0;i<n;i++,putchar('\n'))
//				for(int j=0;j<m;j++)
//					cout<<T[i*m+j];
//			cout<<endl;
		}
		if(T==E)
			printf("%d\n",i);
		else
			printf("-1\n");
	}
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include<map>
#include<bitset>
#define R register
#define LL long long
#define I inline
using namespace std;
bitset <23000> a,b,c,e;

int main()
{
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	getchar();
	for(int i=0;i<n;i++,getchar())
		for(int j=0;j<m;j++)
		{
			char s=getchar();
			(s=='#'?b:a).set(i*m+j);
			(s=='E'?e.set(i*m+j):0);
		}
	c=a;
	for(int i=0;i<k;i++)
	{
		char s=getchar();
		if(c==e)
		{
			printf("%d\n",i);
			return 0;
		}
		if(s=='U') c = ( (c>>m)&a) | (c&(b<<m));
		if(s=='L') c = ((c>>1)&a) | (c&(b<<1));
		if(s=='D') c = ((c<<m)&a) | (c&(b>>m));
		if(s=='R') c = ((c<<1)&a) | (c&(b>>1));
	}
	if(c==e)
		printf("%d\n",k);
	else
		printf("%d\n",-1);
 	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include<cstring>
#include<string>
#include<bitset>
using namespace std;
bitset <23000> a,b,c,e;

int main()
{

    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    getchar();
    for(int i=0;i<n;i++,getchar())
        for(int j=0;j<m;j++)
        {
            char s=getchar();
            (s=='#'?b:a).set(i*m+j);
            (s=='E'?e.set(i*m+j):0);
        }
    c=a;
    for(int i=0;i<k;i++)
    {
        char s=getchar();
        if(c==e)
        {
            printf("%d\n",i);
            return 0;
        }
        if(s=='U') c = ( (c>>m)&a) | (c&(b<<m));
        else if(s=='L') c = ((c>>1)&a) | (c&(b<<1));
        else if(s=='D') c = ((c<<m)&a) | (c&(b>>m));
        else if(s=='R') c = ((c<<1)&a) | (c&(b>>1));
    }
    if(c==e)
        printf("%d\n",k);
    else
        printf("%d\n",-1);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
string st;
int n,m,l,ans;
void solve(){
	static bitset<25000> b1,b2,a,e;
	for(int i=0;i<n;i++){
		cin>>st;
		for(int j=0;j<m;j++){
			if(st[j]=='#') b2.set(i*m+j);
			else b1.set(i*m+j);
			if(st[j]=='E') e.set(i*m+j);
		}
	}
	a=b1;
	cin>>st;
	for(int i=0;i<l;i++){
		if(a==e){ans=i;return;}
		if(st[i]=='U')a=((a>>m)&b1)|(a&(b2<<m));
		if(st[i]=='D')a=((a<<m)&b1)|(a&(b2>>m));
		if(st[i]=='L')a=((a>>1)&b1)|(a&(b2<<1));
		if(st[i]=='R')a=((a<<1)&b1)|(a&(b2>>1));
	}
	if(a==e)ans=l;
	else ans=-1;
}
int main(){
	ios::sync_with_stdio(0);
	cin>>n>>m>>l;
	solve();
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

const int EXP = 22505;
const int DIM = 100015;

char str[DIM];
bitset<EXP> wal, rom, ext, cur;

int main(void)
{
    int n, m, k;
    cin >> n >> m >> k;

    for (int i = 0; i < n; ++i) {
        cin >> str;

        for (int j = 0; j < m; ++j) {
            if (str[j] == '#')
                wal[i * m + j] = true;
            if (str[j] != '#')
                rom[i * m + j] = cur[i * m + j] = true;
            if (str[j] == 'E')
                ext[i * m + j] = true;
        }
    }

    int ans = -1;
    if (ext == cur)
        ans = 0;

    cin >> str;
    for (int i = 0; i <= k and ans == -1; ++i) {
        switch (str[i]) {
            case 'U':
                cur = ((cur >> m) & rom) | (cur & (wal << m)); break;
            case 'D':
                cur = ((cur << m) & rom) | (cur & (wal >> m)); break;
            case 'L':
                cur = ((cur >> 1) & rom) | (cur & (wal << 1)); break;
            case 'R':
                cur = ((cur << 1) & rom) | (cur & (wal >> 1)); break;
        }

        if (cur == ext)
            ans = i + 1;
    }

    cout << ans << endl;
    return 0;
}

// AVX-enabled std::bitset Pragmas (before #includes!)
// - bitset<200000> takes ~ 1ms per 1000 operations on GNU C++14 & GNU C++17
// #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
// #pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
// #pragma GCC target("avx2")  //Enable AVX

// =========== Template ========== //
#include <bits/stdc++.h>
using namespace std;
int fastio() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); return 0; }
int __fastio = fastio();
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p);
template<typename A> ostream& operator<<(ostream &os, const set<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A> ostream& operator<<(ostream &os, const multiset<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e.first << ": " << e.second, sep = ", "; return os << "}"; }
template<typename T>ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; string sep = ""; for (auto e: v) os << sep << e, sep = ", "; return os << "]"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef STEVEN
#define pp(...) cerr << "\t[" << #__VA_ARGS__ << "]:\t", dbg_out(__VA_ARGS__)
#define ppp(x) cerr << x << "\n"
#else
#define pp(...)
#define ppp(...)
#endif
template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } };
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
#define fi first
#define se second
#define sz(x) (int((x).size()))
#define data _data
// =========== Book ============ //

const int inf = 1000 << 20;
// =========== Solution ============= //

typedef bitset<23104> BS;

BS sh(BS a, int b) {
  if (b > 0) return a << b;
  else return a >> -b;
}

int main() {
  BS passable;
  int n, m, k;
  cin >> n >> m >> k;
  int exit = 0;
  int row = m + 2;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; ++j) {
      passable[i * row + j] = s[j - 1] == '.' || s[j - 1] == 'E';
      if (s[j - 1] == 'E') exit = i * (row) + j;
    }
  }
  string moves;
  cin >> moves;

  BS cur = passable;
  int ans = -1;
  for (int idx = 0; idx <= k; ++idx) {
    if (cur.count() == 1 && cur[exit]) {
      ans = idx;
      break;
    }
    if (idx == k) break;
    char ch = moves[idx];
    int offset;
    if (ch == 'U') offset = -row;
    else if (ch == 'D') offset = row;
    else if (ch == 'L') offset = -1;
    else offset = 1;

    BS move = sh(cur, offset);
    BS nxt = (move & passable) | sh(move & ~passable, -offset);
    cur = nxt;
  }
  cur = passable;
  for (int idx = 0; idx <= k; ++idx) {
    if (cur.count() == 1 && cur[exit]) {
      ans = idx;
      break;
    }
    if (idx == k) break;
    char ch = moves[idx];
    int offset;
    if (ch == 'U') offset = -row;
    else if (ch == 'D') offset = row;
    else if (ch == 'L') offset = -1;
    else offset = 1;

    BS move = sh(cur, offset);
    BS nxt = (move & passable) | sh(move & ~passable, -offset);
    cur = nxt;
  }
  cout << ans << "\n";
}

// ============== Notes =============== //
// common bugs
// - fastio
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN? different MAXN vs MAXM?)
// - special cases (n=1? graph not connected?)
// - initialize & re-initialize efficiently between test cases
//
// history of bugs
// - 3/28/20 forgot to initialize set with dummy node (n) [cf 1136e]
// - 3/28/20 array bounds (map was size MAXN instead of MAXK) [cf 1210b]
// - 3/29/20 array bounds ([MAXN][MAXN][4] -> [MAXN][MAXN * 2][4]) [cf 1051d]
// - 3/29/20 special cases (answer cannot be empty string) [cf 848a]
// - 3/29/20 special case / bad initialization of (ans, ansi) when backtracking [cf 733f]
// - 3/30/20 special case / "0" has length 0 in binary [cf 748b]
// - 4/2/20 fakesolve -- setup of dp states missed possible transitions / solutions [cf 771D]
// - 4/2/20 fakesolve -- cant count inversions in linear time, misunderstand structure of problem [cf 771D]
// - 4/3/20 (TLE) re-initialization was too slow [cf 1329 D]
// - 4/3/20 (WA) bottom-up segtree query didn't handle [0, n) correctly
// - 4/5/20 array bounds -- two different array sizes needed [cf 18d]
// - 4/5/20 fakesolve -- greedy sol was wrong,  needed dp [cf 18d]
// - 4/6/20 fakesolve -- tried to do LIS backtracking incorrectly w/ greedy approach, needed true backtracking [cf 650d]
// - 4/6/20 array bounds -- indexing with count into vector of size n; needs to be n + 1
// - 4/7/20 typo -- forgot to change i -> j when copying var declarations x -> y (4 copies) [cf 690b1]
// - 4/8/20 implementation error -- forgot to iterate in sorted order of x [cf 1045g]
// - 4/8/20 plane sweep implementation error -- insertion & removal orders are distinct (x_i & x_i + r_i) [cf 1045g]
// - 4/8/20 inf not big enough -- 1000 << 20 vs 1 billion + 1 billion [cf 1045g]
// - 4/8/20 n ffts wayyy too slow (nlog^2n, n = 2^20, time limit = 1s) [cf 1227f2]
// - 4/8/20 int overflow (multiplied j * count[j] without LL, when adding to ll) [cf 804d]
// - 4/8/20 implementation error -- forgot to dfs again (a 3rd time) after finding both ends of diameter [cf 804d]
// - 4/8/20 typo -- cur.fi < n && cur.se < n <-- should be m
// - 4/10/20 implementation error -- need to erase before inserting into pdbs::tree [1093e]
// - 4/10/20 typo -- forgot to change n + 1 to m + 1 when fixing sliding window to be over edges instead of nodes [cf 901c]
// - 4/10/20 typo -- implementation error -- messed up dfs cycle detection; need to mark edges as vis instead of nodes / use a real algo [cf 901c]
// - 4/10/20 turns out i misread the problem and it's actually querying nodes, not edges
// - 4/10/20 typo -- s/m/n again in if (r < m && ...)
// - 4/10/20 int overflow -- map<int, int> instead of map<ll, int> (elements were from vector<ll>)
// - 4/13/20 fakesolve -- "proved" ans = min(1, n) when ans = n [cf 1339a]
// - 4/13/20 implementation error -- failed to correctly iterate over all permutations of (x, y, z) and its subsets (missed y, z, yz, zy)
// - 4/13/20 int overflow -- iterating 10^5 times from l to r, but l and r may be large[cf 768b]
// - 4/13/20 bit length of 0 is 1 [cf 768b]
// - 4/13/20 fakesolve -- rectangles can overlap without both containing each others vertices [1216c]
// - 4/14/20 implementation error - forgot to break; when ans is found

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;

typedef long long LL;

inline int read()
{
	char c=getchar();int num=0,f=1;
	for(;!isdigit(c);c=getchar())
		f=c=='-'?-1:f;
	for(;isdigit(c);c=getchar())
		num=num*10+c-'0';
	return num*f;
}

const int N=25005;

int n,m,l;
bitset<N> a,b,c,d;
char s[100005];

int main()
{
	n=read(),m=read(),l=read();
	for(int i=0;i<n;++i)
	{
		scanf("%s",s);
		for(int j=0;j<m;++j)
		{
			if(s[j]=='#') b.set(i*m+j);
			else a.set(i*m+j);
			if(s[j]=='E') d.set(i*m+j);
		}
	}
	scanf("%s",s+1);c=a;
	if(c==d)
	{
		puts("0");
		return 0;
	}
	for(int i=1;i<=l;++i)
	{
		if(s[i]=='U') c=(c>>m&a)|(c&(b<<m));
		else if(s[i]=='D') c=(c<<m&a)|(c&(b>>m));
		else if(s[i]=='L') c=(c>>1&a)|(c&(b<<1));
		else c=(c<<1&a)|(c&(b>>1));
		if(c==d)
		{
			printf("%d",i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}//hello

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;

typedef long long LL;

inline int read()
{
	char c=getchar();int num=0,f=1;
	for(;!isdigit(c);c=getchar())
		f=c=='-'?-1:f;
	for(;isdigit(c);c=getchar())
		num=num*10+c-'0';
	return num*f;
}

const int N=25005;

int n,m,l;
bitset<N> a,b,c,d;
char s[100005];

int main()
{
	n=read(),m=read(),l=read();
	for(int i=0;i<n;++i)
	{
		scanf("%s",s);
		for(int j=0;j<m;++j)
		{
			if(s[j]=='#') b.set(i*m+j);
			else a.set(i*m+j);
			if(s[j]=='E') d.set(i*m+j);
		}
	}
	scanf("%s",s+1);c=a;
	if(c==d)
	{
		puts("0");
		return 0;
	}
	for(int i=1;i<=l;++i)
	{
		if(s[i]=='U') c=(c>>m&a)|(c&(b<<m));
		else if(s[i]=='D') c=(c<<m&a)|(c&(b>>m));
		else if(s[i]=='L') c=(c>>1&a)|(c&(b<<1));
		else c=(c<<1&a)|(c&(b>>1));
		if(c==d)
		{
			printf("%d",i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
bitset<25000> e,c,a,b;
int n,m,k;
char p;
int main()
{

	scanf("%d%d%d",&n,&m,&k);
	getchar();

	for(int i = 0 ; i < n; i++,getchar()){
		for(int j = 0 ; j < m; j++){
			p = getchar();
			(p=='#'?b:a).set(i*m+j);
			(p=='E'?e.set(i*m+j):0);
		}
	}

	c = a;

	for(int i = 0 ; i < k; i++){
		p = getchar();

		if(c==e){
			printf("%d",i);
			return 0;
		}

		if(p=='U')
		  c = ((c>>m)&a | c&(b<<m));
		else if(p=='D')
		  c = ((c<<m)&a | c&(b>>m));
		else if(p=='L')
		  c = ((c>>1)&a | c&(b<<1));
		else if(p=='R')
		  c = ((c<<1)&a | c&(b>>1));
	}

	if(c==e)
	   printf("%d",k);
	else
	   cout<<"-1";
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<bitset>

#define ls (t<<1)
#define rs ((t<<1)+1)
#define mid ((l+r)>>1)
#define fi first
#define se second
#define mk make_pair

#define N 155
#define M 200005
#define seed 23333
#define Mo 998244353

using namespace std;
int i,j,m,n,p,k,tot,End,id[N][N];
bitset<N*N>A,L,R,U,D,B,C;
char c[N][N],s[M];
int main()
{
	   scanf("%d%d%d",&n,&m,&k);
	   for (i=0;i<n;++i) for (j=0;j<m;++j) id[i][j]=tot++;
	   for (i=0;i<n;++i)
	   {
	   	     scanf("%s",c[i]);
	   	     for (j=0;j<m;++j)  if (c[i][j]=='E') End=id[i][j];
	   }
	   for (i=0;i<n;++i)
	     for (j=0;j<m;++j)
		   if (c[i][j]!='#')
		   {
		     	 A[id[i][j]]=1;
		     	 if (c[i-1][j]=='#') U[id[i][j]]=1;
		     	 if (c[i+1][j]=='#') D[id[i][j]]=1;
		     	 if (c[i][j-1]=='#') L[id[i][j]]=1;
		     	 if (c[i][j+1]=='#') R[id[i][j]]=1;
		   }
	   scanf("%s",s);
	   for (i=0;i<n*m;++i) B[i]=1;
	   for (i=0;i<=k;++i)
	   {
	   	   C=A&B;
	   	   if (C.count()==1&&A[End]) break;
	   	   if (s[i]=='L') A=(A&L)|((A^(A&L))>>1);
	   	   if (s[i]=='R') A=(A&R)|((A^(A&R))<<1);
	   	   if (s[i]=='U') A=(A&U)|((A^(A&U))>>m);
	   	   if (s[i]=='D') A=(A&D)|((A^(A&D))<<m);
	   }
	   if (i>k) printf("-1\n"); else printf("%d\n",i);
}

#include<bits/stdc++.h>
using namespace std;
const int maxn = 160;
const int N = 1e5 + 10;

char s[maxn][maxn];
char op[N];
int n, m, k;
bitset<160*160> mp, a, b, c;

int main() {
    while(~scanf("%d %d %d", &n, &m, &k)) {
        for(int i = 0; i < n; i++) scanf("%s", s[i]);
        scanf("%s", op);
        mp.reset(); a.reset(); b.reset(); c.reset();
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(s[i][j] != '#') {
                    a.set(i * m + j);
                    if(s[i][j] == 'E') mp.set(i * m + j);
                } else {
                    b.set(i * m + j);
                }
            }
        }
        c = a;
        //cout << mp << endl;
        for(int i = 0; i < k; i++) {
            if(mp == c) { printf("%d\n", i); return 0; }
            if(op[i] == 'L') {
                c = ((c >> 1) & a) | (c & (b << 1));
            } else if(op[i] == 'R') {
                c = ((c << 1) & a) | (c & (b >> 1));
            } else if(op[i] == 'U') {
                c = ((c >> m) & a) | (c & (b << m));
            } else {
                c = ((c << m) & a) | (c & (b >> m));
            }
        }
        printf("%d\n", c == mp ? k: -1);
    }
    return 0;
}

//while(true)rp++;
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cerr<<#x<<"="<<x<<endl;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
typedef long long ll;
const ll INF=1<<28;
const ll LINF=1ll<<61;
int n,m,k,ex,ey;
char g[155][155];
bitset<22555>mm,a,b;
string s;
int pos(int x,int y){return (x-1)*m+y-1;}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
    {
        cin>>g[i][j];
        if(g[i][j]=='#')b.set(pos(i,j));
        else
        {
            a.set(pos(i,j)),mm.set(pos(i,j));
            if(g[i][j]=='E')ex=i,ey=j;
        }
    }
    if(a.count()==1){cout<<0<<endl;return 0;}
    cin>>s;
    /*
            for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=m;k++)
            {
                cout<<a[pos(j,k)]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
        */
    for(int i=0;i<k;i++)
    {
        if(s[i]=='D')
        {
            a=(a<<m)&mm|((b>>m)&a);
        }
        else if(s[i]=='U')
        {
            a=(a>>m)&mm|((b<<m)&a);
        }
        else if(s[i]=='R')
        {
            a=(a<<1)&mm|((b>>1)&a);
        }
        else
        {
            a=(a>>1)&mm|((b<<1)&a);
        }
        /*
        for(int j=1;j<=n;j++)
        {
            for(int k=1;k<=m;k++)
            {
                cout<<a[pos(j,k)]<<" ";
            }
            cout<<endl;
        }
        cout<<endl;
        */
        if(a.count()==1&&a[pos(ex,ey)]==1)
        {
            cout<<i+1<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=23010;
const int inf=-0x3f3f3f3f;
inline int read(){
    int f=0,x=0;char ch=getchar();
    while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[100050];int n,m,k,flag;
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
    while(~scanf("%d%d%d",&n,&m,&k)){
        ch.reset();bl.reset();cn.reset();e.reset();flag=0;
        for(int i=1;i<=n;++i){
            scanf("%s",s1+1);
            for(int j=1;j<=m;++j){
                if(s1[j]=='#')bl[(i-1)*m+j]=1;
                else if(s1[j]!='#')cn[(i-1)*m+j]=1;
                if(s1[j]=='E')e[(i-1)*m+j]=1;
            }
        }
        scanf("%s",s2+1);
        ch=cn;
        for(int i=1;i<=k;++i){
            if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
            if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
            if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
            if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
            if(ch==e){
             write(i);puts("");flag=1;break;
            }
        }
        if(!flag){
            write(-1);puts("");
        }

    }
    return 0;
}

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=250010;
const int inf=-0x3f3f3f3f;
inline int read(){
	int f=0,x=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[10050];int n,m,k,flag;
int work(){
	ch.reset();bl.reset();cn.reset();e.reset();flag=0;
	for(int i=0;i<n;++i){
		scanf("%s",s1);
		for(int j=0;j<m;++j){
			if(s1[j]=='#')bl[i*m+j]=1;
			else {
				cn[i*m+j]=1;
				if(s1[j]=='E')e[i*m+j]=1;
			}
		}
	}
	scanf("%s",s2);
	ch=cn;
	for(int i=0;i<k;++i){
		if(ch==e)return i;
		if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
		if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
		if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
		if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
	}
	if(ch==e)return k;
	else return -1;
}
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	while(~scanf("%d%d%d",&n,&m,&k)){
		write(work());puts("");
	}
	return 0;
}

/*AMETHYSTS*/
#pragma comment(linker, "/STACK:1000000000")
#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#include <deque>
#include <stack>
#include <climits>
#include <string>
#include <queue>
#include <memory.h>
#include <map>
#include <unordered_map>

#define ll long long
#define ld double
#define pii pair <ll, ll>
#define mp make_pair

using namespace std;

const double PI = 3.14159265358979323846;
const int maxn = (int)151;
const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e9;
const double eps = 1e-9;

int n, m, cnt, xe, ye, it, k;
string s;
char a[maxn][maxn];
bitset<150> bs[maxn], bs1[maxn];
bitset<150> cur[maxn][2];

void print(int num) {
	cout << num << endl;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << cur[i][it][j];
		}
		cout << endl;
	}
}

int main() {

	cin >> n >> m >> k;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
			if (a[i][j] != '#') {
				cnt++;
				cur[i][it][j] = 1;
				if (a[i][j] == 'E') {
					xe = i;
					ye = j;
				}
			}
			else {
				bs[i][j] = 1;
			}
		}
		bs1[i] = ~bs[i];
	}
	cin >> s;
	it ^= 1;
	for (int i = 0; i < s.length(); i++) {
		for (int i = 0; i < n; i++) {
			cur[i][it] &= 0;
		}
		if (s[i] == 'U') {
			for (int i = 1; i < n; i++) {
				cur[i - 1][it] |= (cur[i][it ^ 1] & bs1[i - 1]);
				cur[i][it] = (cur[i][it ^ 1] & bs[i - 1]);
			}
		}
		else if (s[i] == 'R') {
			for (int i = 0; i < n; i++) {
				cur[i][it] = ((bs[i] >> 1) & cur[i][it ^ 1]);
				cur[i][it] |= ((cur[i][it ^ 1] << 1) & bs1[i]);
			}
		}
		else if (s[i] == 'L') {
			for (int i = 0; i < n; i++) {
				cur[i][it] = ((bs[i] << 1) & cur[i][it ^ 1]);
				cur[i][it] |= ((cur[i][it ^ 1] >> 1) & bs1[i]);
			}
		}
		else if (s[i] == 'D') {
			for (int i = n - 2; i >= 0; i--) {
				cur[i + 1][it] |= (cur[i][it ^ 1] & bs1[i + 1]);
				cur[i][it] = (cur[i][it ^ 1] & bs[i + 1]);
			}
		}
		int sum = 0;
		for (int i = 0; i < n; i++) {
			sum += cur[i][it].count();
		}
		if (sum == 1 && cur[xe][it][ye]) {
			cout << i + 1;
			return 0;
		}
		//print(i);
		it ^= 1;
	}
	cout << -1;
    return 0;
}

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef double ld;
typedef pair<ll, ll> pll;

const int nmax = 150;

bitset<nmax> b[nmax];
bitset<nmax> wall[nmax];
bitset<nmax> tmp;
int n, m;

int x_exit, y_exit;

bool finish()
{
	/*for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << b[i][j];
		}
		cout << "\n";
	}
	cout << "\n";*/
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		sum += b[i].count();
		if (sum > 1) {
			return false;
		}
	}
	return (sum == 1 && b[x_exit][y_exit] == 1);
}

int main()
{

	//ifstream cin("strictly-off.in");
	//ofstream cout("strictly-off.out");
	//freopen("input.txt", "r", stdin);
	//freopen("threed.out", "w", stdout);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int k;
	cin >> n >> m >> k;

	vector<vector<char> > t(n, vector<char>(m));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> t[i][j];
			if (t[i][j] == '#') {
				wall[i][j] = 1;
			} else {
				b[i][j] = 1;
			}
			if (t[i][j] == 'E') {
				x_exit = i, y_exit = j;
			}
		}
	}

	string s;
	cin >> s;

	if (finish()) {
		cout << "0\n";
		return 0;
	}

	for (int p = 0; p < k; ++p) {
		if (s[p] == 'L') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] >> 1);
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i] |= (tmp << 1);
			}
		} else if (s[p] == 'R') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] << 1);
				tmp = (b[i] & tmp);
				b[i] ^= tmp;
				b[i] |= (tmp >> 1);
			}
		} else if (s[p] == 'D') {
			for (int i = n - 1; i; --i) {
				b[i] = b[i - 1];
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i - 1] |= tmp;
			}
		} else {
			for (int i = 0; i < n - 1; ++i) {
				b[i] = b[i + 1];
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i + 1] |= tmp;
			}
		}
		if (finish()) {
			cout << p + 1 << "\n";
			return 0;
		}
	}

	cout << "-1\n";

}

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef double ld;
typedef pair<ll, ll> pll;

const int nmax = 5;

bitset<nmax> b[nmax];
bitset<nmax> wall[nmax];
bitset<nmax> tmp;
int n, m;

int x_exit, y_exit;

bool finish()
{
	/*for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << b[i][j];
		}
		cout << "\n";
	}
	cout << "\n";*/
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		sum += b[i].count();
		if (sum > 1) {
			return false;
		}
	}
	return (sum == 1 && b[x_exit][y_exit] == 1);
}

int main()
{

	//ifstream cin("strictly-off.in");
	//ofstream cout("strictly-off.out");
	//freopen("input.txt", "r", stdin);
	//freopen("threed.out", "w", stdout);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int k;
	cin >> n >> m >> k;

	vector<vector<char> > t(n, vector<char>(m));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> t[i][j];
			if (t[i][j] == '#') {
				wall[i][j] = 1;
			} else {
				b[i][j] = 1;
			}
			if (t[i][j] == 'E') {
				x_exit = i, y_exit = j;
			}
		}
	}

	string s;
	cin >> s;

	if (finish()) {
		cout << "0\n";
		return 0;
	}

	for (int p = 0; p < k; ++p) {
		if (s[p] == 'L') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] >> 1);
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i] |= (tmp << 1);
			}
		} else if (s[p] == 'R') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] << 1);
				tmp = (b[i] & tmp);
				b[i] ^= tmp;
				b[i] |= (tmp >> 1);
			}
		} else if (s[p] == 'D') {
			for (int i = n - 1; i; --i) {
				b[i] = b[i - 1];
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i - 1] |= tmp;
			}
		} else {
			for (int i = 0; i < n - 1; ++i) {
				b[i] = b[i + 1];
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i + 1] |= tmp;
			}
		}
		if (finish()) {
			cout << p + 1 << "\n";
			return 0;
		}
	}

	cout << "-1\n";

}

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef double ld;
typedef pair<ll, ll> pll;

const int nmax = 150;

bitset<nmax> b[nmax];
bitset<nmax> f[nmax];
bitset<nmax> wall[nmax];
bitset<nmax> tmp;
int n, m;

int x_exit, y_exit;

bool finish()
{
	/*for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << b[i][j];
		}
		cout << "\n";
	}
	cout << "\n";*/
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		sum += b[i].count();
		if (sum > 1) {
			return false;
		}
	}
	return (sum == 1 && b[x_exit][y_exit] == 1);
}

int main()
{

	//ifstream cin("strictly-off.in");
	//ofstream cout("strictly-off.out");
	//freopen("input.txt", "r", stdin);
	//freopen("threed.out", "w", stdout);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int k;
	cin >> n >> m >> k;

	vector<vector<char> > t(n, vector<char>(m));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> t[i][j];
			if (t[i][j] == '#') {
				wall[i][j] = 1;
			} else {
				b[i][j] = 1;
			}
			if (t[i][j] == 'E') {
				x_exit = i, y_exit = j;
			}
		}
	}

	string s;
	cin >> s;

	if (finish()) {
		cout << "0\n";
		return 0;
	}

	for (int p = 0; p < k; ++p) {
		if (s[p] == 'L') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] >> 1);
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i] |= (tmp << 1);
			}
		} else if (s[p] == 'R') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] << 1);
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i] |= (tmp >> 1);
			}
		} else if (s[p] == 'D') {
			for (int i = 0; i < n; ++i) {
				f[i].reset();
				if (i) {
					f[i] |= b[i - 1];
				}
				tmp = (f[i] & wall[i]);
				f[i] ^= tmp;
				if (i) {
					f[i - 1] |= tmp;
				}
			}
			for (int i = 0; i < n; ++i) {
				b[i] = f[i];
			}
		} else {
			for (int i = 0; i < n; ++i) {
				f[i].reset();
				if (i + 1 < n) {
					f[i] |= b[i + 1];
				}
				tmp = (f[i] & wall[i]);
				f[i] ^= tmp;
				if (i + 1 < n) {
					f[i + 1] |= tmp;
				}
			}
			for (int i = 0; i < n; ++i) {
				b[i] = f[i];
			}
		}
		if (finish()) {
			cout << p + 1 << "\n";
			return 0;
		}
	}

	cout << "-1\n";

}

#include <bits/stdc++.h>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef double ld;
typedef pair<ll, ll> pll;

const int nmax = 150;

bitset<nmax> b[nmax];
bitset<nmax> wall[nmax];
bitset<nmax> tmp;
int n, m;

int x_exit, y_exit;

bool finish()
{
	/*for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cout << b[i][j];
		}
		cout << "\n";
	}
	cout << "\n";*/
	int sum = 0;
	for (int i = 0; i < n; ++i) {
		sum += b[i].count();
		if (sum > 1) {
			return false;
		}
	}
	return (sum == 1 && b[x_exit][y_exit] == 1);
}

int main()
{

	//ifstream cin("strictly-off.in");
	//ofstream cout("strictly-off.out");
	//freopen("input.txt", "r", stdin);
	//freopen("threed.out", "w", stdout);
	//ifstream cin("input.txt");
	//ofstream cout("output.txt");

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int k;
	cin >> n >> m >> k;

	vector<vector<char> > t(n, vector<char>(m));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> t[i][j];
			if (t[i][j] == '#') {
				wall[i][j] = 1;
			} else {
				b[i][j] = 1;
			}
			if (t[i][j] == 'E') {
				x_exit = i, y_exit = j;
			}
		}
	}

	string s;
	cin >> s;

	if (finish()) {
		cout << "0\n";
		return 0;
	}

	for (int p = 0; p < k; ++p) {
		if (s[p] == 'L') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] >> 1);
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i] |= (tmp << 1);
			}
		} else if (s[p] == 'R') {
			for (int i = 0; i < n; ++i) {
				b[i] = (b[i] << 1);
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i] |= (tmp >> 1);
			}
		} else if (s[p] == 'D') {
			for (int i = n - 1; i; --i) {
				b[i] = b[i - 1];
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i - 1] |= tmp;
			}
		} else {
			for (int i = 0; i < n - 1; ++i) {
				b[i] = b[i + 1];
				tmp = (b[i] & wall[i]);
				b[i] ^= tmp;
				b[i + 1] |= tmp;
			}
		}
		if (finish()) {
			cout << p + 1 << "\n";
			return 0;
		}
	}

	cout << "-1\n";

}

#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define ll long long
#define fi first
#define se second
using namespace std;

template <typename T>
void read(T &x) {
    x = 0; bool f = 0;
    char c = getchar();
    for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
    for (;isdigit(c);c=getchar()) x=x*10+(c^48);
    if (f) x=-x;
}

template<typename F>
inline void write(F x, char ed = '\n') {
	static short st[30];short tp=0;
	if(x<0) putchar('-'),x=-x;
	do st[++tp]=x%10,x/=10; while(x);
	while(tp) putchar('0'|st[tp--]);
	putchar(ed);
}

template <typename T>
inline void Mx(T &x, T y) { x < y && (x = y); }

template <typename T>
inline void Mn(T &x, T y) { x > y && (x = y); }

#include <bitset>
const int N = 155;
const int S = 100500;
bitset<N * N> T, L, R, D, U;
char s[S];

int m, n, ed, len;
int num(int x, int y) {
	return (x - 1) * m + y;
}

int main() {
	read(n), read(m), read(len);
	for (int i = 1;i <= n; i++) {
		scanf ("%s", s + 1);
		for (int j = 1;j <= m; j++) {
			if (s[j] == 'E') ed = num(i, j);
			else if (s[j] == '#') {
				if (i != 1) D[num(i - 1, j)] = 1;
				if (i != n) U[num(i + 1, j)] = 1;
				if (j != 1) R[num(i, j - 1)] = 1;
				if (j != m) L[num(i, j + 1)] = 1;
			}
			else T[num(i, j)] = 1;
		}
	}
	scanf ("%s", s + 1);
	if (T.none()) return puts("0"), 0;
	for (int i = 1;i <= len; i++) {
		if (s[i] == 'U') T = ((T ^ (T & U)) >> m) | (T & U);
		else if (s[i] == 'D') T = ((T ^ (T & D)) << m) | (T & D);
		else if (s[i] == 'L') T = ((T ^ (T & L)) >> 1) | (T & L);
		else if (s[i] == 'R') T = ((T ^ (T & R)) << 1) | (T & R);
		T[ed] = 0;
		if (T.none()) return write(i), 0;
	}
	write(-1);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
char s[300],ss[maxn];
bitset<maxn>now,fail,can;
int n,m,l,pos;
int main()
{
	scanf("%d%d%d",&n,&m,&l);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		for(int j=1;j<=n;j++)
		{
			if(s[j]=='#')fail[i*m+j]=1;
			else can[i*m+j]=now[i*m+j]=1;
			if(s[j]=='E')pos=i*m+j;
		}
	}
	scanf("%s",ss+1);
	if(now.count()==1&&now[pos]==1)return puts("0"),0;
	for(int i=1;i<=l;i++)
	{
		if(ss[i]=='U')now=(((now>>m)&can)|(now&(fail<<m)));
		else if(ss[i]=='D')now=(((now<<m)&can)|(now&(fail>>m)));
		else if(ss[i]=='L')now=(((now>>1)&can)|(now&(fail<<1)));
		else if(ss[i]=='R')now=(((now<<1)&can)|(now&(fail>>1)));
		if(now.count()==1&&now[pos]==1)
		{
			printf("%d\n",i);
			return 0;
		}
	}
	return puts("-1"),0;
}

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
char s[300],ss[maxn];
bitset<maxn>now,fail,can;
int n,m,l,pos;
int main()
{
	scanf("%d%d%d",&n,&m,&l);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		for(int j=1;j<=n;j++)
		{
			if(s[j]=='#')fail[i*m+j]=1;
			else can[i*m+j]=now[i*m+j]=1;
			if(s[j]=='E')pos=i*m+j;
		}
	}
	cin>>ss+1;
	if(now.count()==1&&now[pos]==1)return puts("0"),0;
	for(int i=1;i<=l;i++)
	{
		if(ss[i]=='U')now=((now>>m)&can|now&(fail<<m));
		if(ss[i]=='D')now=((now<<m)&can|now&(fail>>m));
		if(ss[i]=='L')now=((now>>1)&can|now&(fail<<1));
		if(ss[i]=='R')now=((now<<1)&can|now&(fail>>1));
		if(now.count()==1&&now[pos]==1)return printf("%d\n",i),0;
	}
	return puts("-1"),0;
}

#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
using namespace std;

struct IO
{
	#define MAXSIZE (1 << 20)
	#define isdigit(x) (x >= '0' && x <= '9')
  	char buf[MAXSIZE],*p1,*p2;
  	char pbuf[MAXSIZE],*pp;
  	IO():p1(buf),p2(buf),pp(pbuf){}
  	inline char gc() {
    	return getchar();
    	if (p1==p2) p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
    	return p1==p2?' ':*p1++;
  	}
	inline bool blank(char ch) {return ch==' '||ch =='\n'||ch == '\r'||ch == '\t';}
  	template <class T>
  	inline void read(T &x)
	{
	    register double tmp=1;register bool sign=0; x=0;
	    register char ch=gc();
	    for (;!isdigit(ch);ch=gc()) if(ch=='-') sign=1;
	    for (;isdigit(ch);ch=gc()) x=x*10+(ch-'0');
	    if (ch=='.') for (ch=gc();isdigit(ch);ch=gc()) tmp/=10.0,x+=tmp*(ch-'0');
	    if (sign) x=-x;
  	}
  	inline void read(char *s)
	{
    	register char ch=gc();
		for (;blank(ch);ch=gc());
		for (;!blank(ch);ch=gc()) *s++=ch;
    	*s=0;
  	}
  	inline void read(char &c) {for(c=gc();blank(c);c=gc());}
  	template<class t> inline void write(t x){
		if(x<0) putchar('-'),write(-x);
		else{if(x>9) write(x/10);putchar('0'+x%10);}
	}
} io;

const int mod=1e9+7;
const int mo=998244353;
const int N=155;
const int M=1e5+5;

int n,m,l,PE;
char a[N][N],opt[M];
bitset<N*N> can,bn,now;

int main()
{
	scanf("%d%d%d",&n,&m,&l);
	For(i,1,n) scanf("%s",a[i]+1);
	scanf("%s",opt+1);
	For(i,1,n) For(j,1,m)
	{
		if(a[i][j]=='#') bn.set((i-1)*m+j);
		else can.set((i-1)*m+j);
		if(a[i][j]=='E') PE=(i-1)*m+j;
	}
	now=can;
	For(i,1,l)
	{
		if(opt[i]=='U') now=((now>>m)&can)|(now&(bn<<m));
		if(opt[i]=='D') now=((now<<m)&can)|(now&(bn>>m));
		if(opt[i]=='L') now=((now>>1)&can)|(now&(bn<<1));
		if(opt[i]=='R') now=((now<<1)&can)|(now&(bn>>1));
		if(now.count()==1&&now[PE]==1) return printf("%d\n",i),0;
	}
	puts("-1");
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define rint register int
#define INF ((1 << 30) - 1)
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define Pair pair < int, int >
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i, k, j) for(rint i = (k); i >= (j); i--)
#define For(i, k, j) for(rint i = (k); i <= (j); i++)
#define Foe(i, u) for(rint i = lst[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file(s) freopen(s".in", "r", stdin); freopen(s".out", "w", stdout)
#define int long long
const int P = 1000000007; //
using namespace std;
inline void ckmax(int &a, int b) {a = max(a, b);}
inline void ckmin(int &a, int b) {a = min(a, b);}
inline void mulmod(int &a, int b) {a = 1ll * a * b % P;}
inline void addmod(int &a, int b) {int t = a + b; a = (t >= P ? t - P : t); }
inline int ksm(int a, int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline int inv(int a) {return ksm(a, P-2);}

namespace FastIO {
    const int SIZE=1<<16; char buf[SIZE], obuf[SIZE], str[64]; int bi=SIZE, bn=SIZE, opt;
    int read(char *s) {
        while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
        int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
    }
    bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n)return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
    void write(int x) {
        if(!x) obuf[opt++] = '0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
        if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}
    }
    void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}}
    void Fflush() { if (opt) fwrite(obuf, 1, opt, stdout); opt=0;}
};
const int MAXN = 155, MAXK = 1e5 + 5;
int n, m, k;
inline int Hash(int i, int j) {
	return (i - 1) * m + j;
}
char mp[MAXN][MAXN], s[MAXK];
bitset < MAXN*MAXN > e, now, o;
signed main()
{
	#ifndef ONLINE_JUDGE
		file("pro");
	#endif
	FI(n); FI(m); FI(k);
	For(i, 1, n) FI(mp[i] + 1);
	FI(s + 1);
	For(i, 1, n) For(j, 1, m) {
		if(mp[i][j] == '.') {
			now[Hash(i, j)] = 1;
		} else if(mp[i][j] == '#') {
//			o[Hash(i, j)] = 1;
			;
		} else {
			now[Hash(i, j)] = e[Hash(i, j)] = 1;
		}
	}
	o = now;
//	cerr << b << endl;
	For(i, 1, k) {
		if(s[i] == 'U') now = (now >> m) & o;
		if(s[i] == 'D') now = (now << m) & o;
		if(s[i] == 'L') now = (now >> 1) & o;
		if(s[i] == 'R') now = (now << 1) & o;
//		cerr << now << endl;
		if(now == e) return printf("%d\n", i), 0;
	}
	printf("-1\n");
    return FastIO::Fflush(), 0;
}

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define rint register int
#define INF ((1 << 30) - 1)
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define Pair pair < int, int >
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i, k, j) for(rint i = (k); i >= (j); i--)
#define For(i, k, j) for(rint i = (k); i <= (j); i++)
#define Foe(i, u) for(rint i = lst[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define file(s) freopen(s".in", "r", stdin); freopen(s".out", "w", stdout)
#define int long long
const int P = 1000000007; //
using namespace std;
inline void ckmax(int &a, int b) {a = max(a, b);}
inline void ckmin(int &a, int b) {a = min(a, b);}
inline void mulmod(int &a, int b) {a = 1ll * a * b % P;}
inline void addmod(int &a, int b) {int t = a + b; a = (t >= P ? t - P : t); }
inline int ksm(int a, int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline int inv(int a) {return ksm(a, P-2);}

namespace FastIO {
    const int SIZE=1<<16; char buf[SIZE], obuf[SIZE], str[64]; int bi=SIZE, bn=SIZE, opt;
    int read(char *s) {
        while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
        int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
    }
    bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n)return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
    void write(int x) {
        if(!x) obuf[opt++] = '0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
        if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}
    }
    void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}}
    void Fflush() { if (opt) fwrite(obuf, 1, opt, stdout); opt=0;}
};
const int MAXN = 155, MAXK = 1e5 + 5;
int n, m, k;
inline int Hash(int i, int j) {
	return (i - 1) * m + j;
}
char mp[MAXN][MAXN], s[MAXK];
bitset < MAXN*MAXN > e, now, o, b;
signed main()
{
	#ifndef ONLINE_JUDGE
		file("pro");
	#endif
	FI(n); FI(m); FI(k);
	For(i, 1, n) FI(mp[i] + 1);
	FI(s + 1);
	For(i, 1, n) For(j, 1, m) {
		if(mp[i][j] == '.') {
			now[Hash(i, j)] = 1;
		} else if(mp[i][j] == '#') {
			b[Hash(i, j)] = 1;
		} else {
			now[Hash(i, j)] = e[Hash(i, j)] = 1;
		}
	}
	o = now;
//	cerr << b << endl;
	For(i, 1, k) {
		if(s[i] == 'U') now = ((now >> m) & o) | ((b << m) & now);
		if(s[i] == 'D') now = ((now << m) & o) | ((b >> m) & now);
		if(s[i] == 'L') now = ((now >> 1) & o) | ((b << 1) & now);
		if(s[i] == 'R') now = ((now << 1) & o) | ((b >> 1) & now);
//		cerr << now << endl;
		if(now == e) return printf("%d\n", i), 0;
	}
	printf("-1\n");
    return FastIO::Fflush(), 0;
}

#include<bits/stdc++.h>
using namespace std;

bitset<200000> a,b,c,e;
int n,m,k;
char w[100000+6];

int main()
{
	//freopen("aha.in","r",stdin);
	//freopen("aha.out","w",stdout);

    scanf("%d%d%d",&n,&m,&k);

    for(int i = 0; i < n; ++i)
       for(int j = 0; j < m; j++){
       	   char p;
           p = getchar();
           if(p=='#')
              b.set(i*m+j);
           if(p=='.' || p=='E')
              a.set(i*m+j);
           if(p=='E')
              e.set(i*m+j);
       }

    c = a;

    for(int i = 1;i <= k ; ++i)
       w[i] = getchar();

    if(c==e){
        printf("0");
        return 0;
    }

    for(int i = 1; i <= k; ++i){

        if(w[i]=='U')
		   c = ( (c>>m)&a) | (c&(b<<m));////要么下一一位继续存在，要么撞墙。如果继续存在就跟原图比较，去掉我们减少的部分
        else if(w[i]=='L')
		   c = ((c>>1)&a) | (c&(b<<1));
        else if(w[i]=='D')
		   c = ((c<<m)&a) | (c&(b>>m));
        else if(w[i]=='R')
		   c = ((c<<1)&a) | (c&(b>>1));

        if(c == e){
            printf("%d",i);
            return 0;
        }

    }

    cout<<"-1";

    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
char s[100005];
int n, m, k;
int work(){
    bitset<23000> a, b, c, e;
    for(int i = 0; i < n; i++)
    {
        scanf("%s", s);
        for(int j = 0; j < m; j++)
        {
            (s[j]=='#'?b:a).set(i*m+j);
            (s[j]=='E'?e.set(i*m+j):0);
        }
    }
    scanf("%s", s);
    c = a;
    for(int i = 0; i < k; i++)
    {
        if(c==e)return i;
        if(s[i]=='U') c = ((c>>m)&a);
        if(s[i]=='L') c = ((c>>1)&a);
        if(s[i]=='D') c = ((c<<m)&a);
        if(s[i]=='R') c = ((c<<1)&a);
    }
    if(c==e)return k;
    return -1;
}
int main(){
    int i, j;
    while (~scanf("%d %d %d", &n, &m, &k)){
        printf("%d\n", work());
    }
    return 0;
}

#include<complex>
#include<cstdio>
#include<bitset>
using namespace std;
const int N=153,M=1e5+7;
int n,m,K,ed;
char s[M];
bitset<N*N>wall,now,can;
int main()
{
	scanf("%d%d%d",&n,&m,&K);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		for(int j=1;j<=m;j++)
		{
			if(s[j]=='#')wall[i*m+j]=1;
			else can[i*m+j]=now[i*m+j]=1;
			if(s[j]=='E')ed=i*m+j;
		}
	}
	scanf("%s",s+1);
	for(int i=1;i<=K;i++)
	{
		if(s[i]=='U')now=(now>>m&can)|(now&(wall<<m));
		else if(s[i]=='D')now=(now<<m&can)|(now&(wall>>m));
		else if(s[i]=='L')now=(now>>1&can)|(now&(wall<<1));
		else now=(now<<1&can)|(now&(wall>>1));
		if(now.count()==1 && now[ed])
		{
			printf("%d\n",i);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;

typedef long long LL;

inline int read()
{
	char c=getchar();int num=0,f=1;
	for(;!isdigit(c);c=getchar())
		f=c=='-'?-1:f;
	for(;isdigit(c);c=getchar())
		num=num*10+c-'0';
	return num*f;
}

const int N=25005;

int n,m,l;
bitset<N> a,b,c,d;
char s[100005];

int main()
{
	n=read(),m=read(),l=read();
	for(int i=0;i<n;++i)
	{
		scanf("%s",s);
		for(int j=0;j<m;++j)
		{
			if(s[j]=='#') b.set(i*m+j);
			else a.set(i*m+j);
			if(s[j]=='E') d.set(i*m+j);
		}
	}
	scanf("%s",s+1);c=a;
	for(int i=1;i<=l;++i)
	{
		if(s[i]=='U') c=(c>>m&a)|(c&(b<<m));
		else if(s[i]=='D') c=(c<<m&a)|(c&(b>>m));
		else if(s[i]=='L') c=(c>>1&a)|(c&(b<<1));
		else c=(c<<1&a)|(c&(b>>1));
		if(c==d)
		{
			printf("%d",i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}

//In the name of ALLAH
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define MP make_pair
const bool DEBUG = false;
#define cerr if(DEBUG)cerr
using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const long long maxn = 152;
const long long INF = 1e18 + 1;
const long long inf = 1e9 + 5;
const long long M = 1e9 + 7;
const int lg = 21;

int n, m, k, t, x;
string p, s[maxn];
bitset<25> ans, wall, pass;

int main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		cin >> s[i];
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			if(s[i][j] == '#')
				wall[i * m + j] = 1;
			else
				pass[i * m + j] = ans[i * m + j] = 1;
			if(s[i][j] == 'E')
				x = i * m + j;
		}
	}
	cin >> p;
	if(ans.count() == 1)
		return cout << 0 << endl, 0;
	for(int i = 0; i < k; i++){
		if(p[i] == 'R')
			ans = ((ans << 1) & pass) | (((ans << 1) & wall) >> 1 & pass);
		if(p[i] == 'L')
			ans = ((ans >> 1) & pass) | (((ans >> 1) & wall) << 1 & pass);
		if(p[i] == 'D')
			ans = ((ans << m) & pass) | (((ans << m) & wall) >> m & pass);
		if(p[i] == 'U')
			ans = ((ans >> m) & pass) | (((ans >> m) & wall) << m & pass);
		if(ans.count() == 1 and ans[x] == 1)
			return cout << i + 1 << "\n", 0;
	}
	cout << -1 << endl;
	return 0;
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define pb push_back
#define __V vector
#define all(x) x.begin(), x.end()
#define oit ostream_iterator
#define mod 1000000007ll
using namespace std;
void doin() {
	cin.tie();
	cout.tie();
	ios::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif
}
template<typename X, typename Y>
istream& operator>>(istream& in, pair<X, Y> &a) {
	in >> a.first >> a.second;
	return in;
}
template<typename T>
void getv(T& i) {
	cin >> i;
}
template<typename T, typename ... Ns>
void getv(vector<T>& v, int n, Ns ... ns) {
	v.resize(n);
	for (auto& i : v)
		getv(i, ns...);
}
template<typename T>
void getv1(T& i) {
	cin >> i;
}
template<typename T, typename ... Ns>
void getv1(vector<T>& v, int n, Ns ... ns) {
	v.resize(n + 1);
	for (int i = 1; i <= n; i++)
		getv(v[i], ns...);
}
#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
inline void getch(char &x) {
	while (x = getchar_unlocked(), x < 33) {
		;
	}
}
inline void getstr(string &str) {
	str.clear();
	char cur;
	while (cur = getchar_unlocked(), cur < 33) {
		;
	}
	while (cur > 32) {
		str += cur;
		cur = getchar_unlocked();
	}
}
template<typename T> inline bool sc(T &num) {
	bool neg = 0;
	int c;
	num = 0;
	while (c = getchar_unlocked(), c < 33) {
		if (c == EOF)
			return false;
	}
	if (c == '-') {
		neg = 1;
		c = getchar_unlocked();
	}
	for (; c > 47; c = getchar_unlocked())
		num = num * 10 + c - 48;
	if (neg)
		num *= -1;
	return true;
}
typedef unsigned long long ull;
typedef long long ll;
typedef float ld;
typedef int _I;
typedef pair<ll, ll> pi;
typedef pair<ld, ld> pd;
typedef map<_I, _I> mii;
typedef __V <_I> vi;
typedef __V <char> vc;
typedef __V <ld> vd;
typedef __V <vd> vvd;
typedef __V <pi> vpi;
typedef __V <__V<_I>> vvi;
typedef __V <__V<char>> vvc;
typedef __V <__V<pi>> vvpi;
using AntonTsypko = void;
using arsijo = AntonTsypko;
using god = arsijo;
uniform_real_distribution<double> double_dist(0, 1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int S = 151*151 + 100;
int n, m, k;
bitset<S> no[4], msk[4][2], ans, cur;
char mat[160][160];
void setp(bitset<S>& bs, int i, int j, int c = 1) {
	i = i*m + j;
	bs.set(i, c);
}
void shbo(bitset<S>& bs) {
	bs<<=m;
	bs&=no[0];
}
void shup(bitset<S>& bs) {
	bs>>=m;
	bs&=no[1];
}
void shle(bitset<S>& bs) {
	bs>>=1;
	bs&=no[3];
}
void shri(bitset<S>& bs) {
	bs<<=1;
	bs&=no[2];
}
void db(bitset<S>& bs) {
	for(int i = 0; i < n; cout << "\n", i++)
		for(int j = 0; j < m; j++)
			cout << bs[i*m + j];
	cout << "============\n";
}

int main() {
	doin();
	cin >> n >> m >> k;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if(i)
				setp(no[0], i, j);
			if(i+1 != n)
				setp(no[1], i, j);
			if(j)
				setp(no[2], i, j);
			if(j+1 != m)
				setp(no[3], i, j);
		}
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			cin >> mat[i][j];
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			if(mat[i][j] == 'E') {
				setp(ans, i, j);
			}
			if(mat[i][j] == '#')
				continue;
			setp(cur, i, j);
			setp(msk[0][(!i||mat[i-1][j] == '#')], i, j);//up
			setp(msk[1][(i+1==n||mat[i+1][j] == '#')], i, j);//bottom
			setp(msk[2][(!j||mat[i][j-1] == '#')], i, j);//right
			setp(msk[3][(j+1==m||mat[i][j+1] == '#')], i, j);//left
		}
	string s;
	cin >> s;
	int k = 0;
	while(k < s.size()) {
		if(s[k] == 'L') {
			bitset<S> t = (cur&msk[2][0]);
			shle(t);
			cur = (cur&msk[2][1])|t;
		} else if(s[k] == 'R') {
			bitset<S> t = (cur&msk[3][0]);
			shri(t);
			cur = (cur&msk[3][1])|t;

		} else if(s[k] == 'U') {
			bitset<S> t = (cur&msk[0][0]);
			shup(t);
			cur = (cur&msk[0][1])|t;
		} else if(s[k] == 'D') {
			bitset<S> t = (cur&msk[1][0]);
			shbo(t);
			cur = (cur&msk[1][1])|t;
		}
		k++;
		if(cur == ans) {
			cout << k << "\n";
			return 0;
		}
	}
	cout << -1;
}

// AVX-enabled std::bitset Pragmas (before #includes!)
// - bitset<200000> takes ~ 1ms per 1000 operations on GNU C++14 & GNU C++17
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2")  //Enable AVX

// =========== Template ========== //
#include <bits/stdc++.h>
using namespace std;
int fastio() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); return 0; }
int __fastio = fastio();
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p);
template<typename A> ostream& operator<<(ostream &os, const set<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A> ostream& operator<<(ostream &os, const multiset<A> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e, sep = ", "; return os << "}"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &m) { os << "{"; string sep = ""; for (auto e: m) os << sep << e.first << ": " << e.second, sep = ", "; return os << "}"; }
template<typename T>ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; string sep = ""; for (auto e: v) os << sep << e, sep = ", "; return os << "]"; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef STEVEN
#define pp(...) cerr << "\t[" << #__VA_ARGS__ << "]:\t", dbg_out(__VA_ARGS__)
#define ppp(x) cerr << x << "\n"
#else
#define pp(...)
#define ppp(...)
#endif
template<class Fun> class y_combinator_result { Fun fun_; public: template<class T> explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {} template<class ...Args> decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward<Args>(args)...); } };
template<class Fun> decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
#define fi first
#define se second
#define sz(x) (int((x).size()))
#define data _data
// =========== Book ============ //

const int inf = 1000 << 20;
// =========== Solution ============= //

typedef bitset<23104> BS;

BS sh(BS a, int b) {
  if (b > 0) return a << b;
  else return a >> -b;
}

int main() {
  BS passable;
  int n, m, k;
  cin >> n >> m >> k;
  int exit = 0;
  int row = m + 2;
  for (int i = 1; i <= n; ++i) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; ++j) {
      passable[i * row + j] = s[j - 1] == '.' || s[j - 1] == 'E';
      if (s[j - 1] == 'E') exit = i * (row) + j;
    }
  }
  string moves;
  cin >> moves;

  BS cur = passable;
  int ans = -1;
  for (int idx = 0; idx <= k; ++idx) {
    if (cur.count() == 1 && cur[exit]) {
      ans = idx;
    }
    if (idx == k) break;
    char ch = moves[idx];
    int offset;
    if (ch == 'U') offset = -row;
    else if (ch == 'D') offset = row;
    else if (ch == 'L') offset = -1;
    else offset = 1;

    BS move = sh(cur, offset);
    BS nxt = (move & passable) | sh(move & ~passable, -offset);
    cur = nxt;
  }
  cout << ans << "\n";
}

// ============== Notes =============== //
// common bugs
// - fastio
// - int overflow (especially intermediate expressionns)
// - array bounds (indices bigger than MAXN? different MAXN vs MAXM?)
// - special cases (n=1? graph not connected?)
// - initialize & re-initialize efficiently between test cases
//
// history of bugs
// - 3/28/20 forgot to initialize set with dummy node (n) [cf 1136e]
// - 3/28/20 array bounds (map was size MAXN instead of MAXK) [cf 1210b]
// - 3/29/20 array bounds ([MAXN][MAXN][4] -> [MAXN][MAXN * 2][4]) [cf 1051d]
// - 3/29/20 special cases (answer cannot be empty string) [cf 848a]
// - 3/29/20 special case / bad initialization of (ans, ansi) when backtracking [cf 733f]
// - 3/30/20 special case / "0" has length 0 in binary [cf 748b]
// - 4/2/20 fakesolve -- setup of dp states missed possible transitions / solutions [cf 771D]
// - 4/2/20 fakesolve -- cant count inversions in linear time, misunderstand structure of problem [cf 771D]
// - 4/3/20 (TLE) re-initialization was too slow [cf 1329 D]
// - 4/3/20 (WA) bottom-up segtree query didn't handle [0, n) correctly
// - 4/5/20 array bounds -- two different array sizes needed [cf 18d]
// - 4/5/20 fakesolve -- greedy sol was wrong,  needed dp [cf 18d]
// - 4/6/20 fakesolve -- tried to do LIS backtracking incorrectly w/ greedy approach, needed true backtracking [cf 650d]
// - 4/6/20 array bounds -- indexing with count into vector of size n; needs to be n + 1
// - 4/7/20 typo -- forgot to change i -> j when copying var declarations x -> y (4 copies) [cf 690b1]
// - 4/8/20 implementation error -- forgot to iterate in sorted order of x [cf 1045g]
// - 4/8/20 plane sweep implementation error -- insertion & removal orders are distinct (x_i & x_i + r_i) [cf 1045g]
// - 4/8/20 inf not big enough -- 1000 << 20 vs 1 billion + 1 billion [cf 1045g]
// - 4/8/20 n ffts wayyy too slow (nlog^2n, n = 2^20, time limit = 1s) [cf 1227f2]
// - 4/8/20 int overflow (multiplied j * count[j] without LL, when adding to ll) [cf 804d]
// - 4/8/20 implementation error -- forgot to dfs again (a 3rd time) after finding both ends of diameter [cf 804d]
// - 4/8/20 typo -- cur.fi < n && cur.se < n <-- should be m
// - 4/10/20 implementation error -- need to erase before inserting into pdbs::tree [1093e]
// - 4/10/20 typo -- forgot to change n + 1 to m + 1 when fixing sliding window to be over edges instead of nodes [cf 901c]
// - 4/10/20 typo -- implementation error -- messed up dfs cycle detection; need to mark edges as vis instead of nodes / use a real algo [cf 901c]
// - 4/10/20 turns out i misread the problem and it's actually querying nodes, not edges
// - 4/10/20 typo -- s/m/n again in if (r < m && ...)
// - 4/10/20 int overflow -- map<int, int> instead of map<ll, int> (elements were from vector<ll>)
// - 4/13/20 fakesolve -- "proved" ans = min(1, n) when ans = n [cf 1339a]
// - 4/13/20 implementation error -- failed to correctly iterate over all permutations of (x, y, z) and its subsets (missed y, z, yz, zy)
// - 4/13/20 int overflow -- iterating 10^5 times from l to r, but l and r may be large[cf 768b]
// - 4/13/20 bit length of 0 is 1 [cf 768b]
// - 4/13/20 fakesolve -- rectangles can overlap without both containing each others vertices [1216c]

#include <cassert>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <sstream>
#include <utility>

#include <algorithm>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <memory.h>
//#include "bigint.h"

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define init(st) memset(st, 0, sizeof(st))
#define ll long long

// String operations
template<class T>
void splitstr(const string &s, vector<T> &out) {
    istringstream in(s);
    out.clear();
    copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}

string read_line() {
    string ret_val;
    getline(std::cin, ret_val);
    return ret_val;
}

template<class T>
void init_array(T *arr, int size, T value) {
    for (int i = 0; i < size; i++)
        arr[i] = value;
}

// Arithmetic operations
template<class T>
inline T gcd(T a, T b) {
    return (!a) ? b : gcd(b % a, a);
}

template<class T>
inline T mod(T a, T p) {
    a %= p;
    return (a < 0) ? a + p : a;
}

template<class T>
inline int num_bits(T n) {
    return (!n) ? 0 : 1 + numbits(n & (n - 1));
}

template<class T>
inline T inverse(T a, T m) {
    a = mod<T>(a,m);
    return (a==1) ? 1 : mod((1-m*inverse(m%a,a))/a,m);
}

template<class T>
inline bool is_prime(T a) {
    T lim = (T)sqrt((double)a);
    for (T i = 2; i <= T(lim+1E-9); i++)
        if (a % i == 0)
           return false;
    return true;
}

template<class T>
inline T power(T a, T p, T mod) {
    if (!p) return 1;
    T temp = power(a, p>>1, mod);
    temp = (temp * temp) % mod;
    if (p&1)
        temp = (temp * a) % mod;
    return temp;
}

void get_primes(int start, int end, vector<int> &vi) {
        int *p = new int[end + 1];
        init_array<int>(p, end+1, 0); p[1] = 1;
        for (int i = 2; i <= end; i++) {
                if (!p[i]) {
                        if (i >= start)
                                vi.push_back(i);
                        for (int j = 2*i; j <= end; j += i)
                                p[j] = 1;
                }
        }
    delete [] p;
}

// Graph operations
bool dfs(int current, int final, int total, int* visited, int **edges, bool flow) {
        if (current == final)
                return true;
        if (visited[current])
                return false;
        visited[current] = true;
        for (int i = 0; i < total; i++)
                if (edges[current][i] && dfs(i, final, total, visited, edges, flow)) {
                        if (flow) {
                                edges[current][i]--;
                                edges[i][current]++;
                        }
                        return true;
                }
        return false;
}

int flow(int in, int out, int total, int **edges) {
        int result = 0;
        int *visited = new int[total];
        while (init_array<int>(visited, total, 0), dfs(in, out, total, visited, edges, true))
                result++;
    delete [] visited;
        return result;
}

// Disjoint datasets
void create_set(int x, int *P, int *rank) {
        P[x] = x;
        rank[x] = 0;
}

int find_set(int x, int *P) {
        if (x != P[x])
                P[x] = find_set(P[x], P);
        return P[x];
}

bool merge_sets(int x, int y, int *P, int *rank) {
        int Px = find_set(x, P);
        int Py = find_set(y, P);
        if(Px == Py)
                return false;
        if (rank[Px] > rank[Py]) P[Py] = Px;
        else P[Px] = Py;
        if (rank[Px] == rank[Py])
                rank[Py]++;
        return true;
}

// Binary tree operations
template <typename T>
int read_cum_freq(int index, T *tree) {
        int sum = 0;
        while (index) {
                sum += tree[index];
                index -= (index & -index);
        }
        return sum;
}

template <typename T>
void upd_freq(int index, int mxIndex, int value, T *tree) {
        while (index <= mxIndex) {
                tree[index] += value;
                index += (index & -index);
        }
}

template <typename T>
int read_freq(int index, T *tree) {
        return read_cum_freq(index, tree) - read_cum_freq(index-1, tree);
}

// String algos.
void build_failure_function(const string &str, int *temp_arr) {
    temp_arr[0] = temp_arr[1] = 0;
    for (int i = 2; i <= sz(str); i++) {
        temp_arr[i] = -1;
        for (int ind = temp_arr[i-1]; temp_arr[i] == -1; ind = temp_arr[ind]) {
            if (str[ind] == str[i-1]) {
                temp_arr[i] = ind + 1;
            } else if (ind == 0) {
                temp_arr[i] = 0;
            }
        }
    }
}

void KMP(const string &text, const string &pattern, int *res) {
    int *temp_arr = new int[sz(pattern) + 1];
    build_failure_function(pattern, temp_arr);
    int i = 0;
    int ind = 0;
    while (i < sz(text)) {
        if (text[i] == pattern[ind]) {
            res[i] = ++ind;
            if (res[i] == sz(pattern)) {
                ind = temp_arr[ind];
            }
            i++;
        } else if (ind == 0) {
            res[i++] = 0;
        } else {
            ind = temp_arr[ind];
        }
    }
    delete [] temp_arr;
}

// Setup
void setup(int value, string name) {
    string name_in = name + ".in";
    string name_out = name + ".out";
    freopen(name_in.c_str(), "r", stdin);
    if (value) {
        freopen(name_out.c_str(), "w", stdout);
    }
}

int arr[100001];
int com[100001];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int main() {
#ifdef LOCAL_HOST
    setup(0, "temp");
#endif
    int n, m; std::cin >> n >> m;
    int k; std::cin >> k;
    vector<string> vs(n);
    for (int i = 0; i < n; i++)
        std::cin >> vs[i];
    int x, y, num = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (vs[i][j] == 'E') {
                y = i;
                x = j;
            }
    string command; std::cin >> command;
    string pattern = "LRUD";
    for (int i = 0; i < command.size(); i++) {
        com[i] = pattern.find(command[i]);
    }

    init(arr);
    for (int i = 1; i < n-1; i++)
        for (int j = 1; j < m-1; j++)
            if (vs[i][j] != '#' && (vs[i-1][j] == '#' || vs[i+1][j] == '#') && (vs[i][j-1] == '#' || vs[i][j+1] == '#')) {
                    num++;
                    int cx = j;
                    int cy = i;
                    if (cx == x && cy == y)
                        ++arr[0];
                    for (int k = 0; k < command.size(); k++) {
                        int nx = cx + dx[com[k]];
                        int ny = cy + dy[com[k]];
                        if (vs[ny][nx] != '#') {
                            cx = nx;
                            cy = ny;
                        }
                        if (cx == x && cy == y)
                            ++arr[k+1];
                    }
            }
    for (int i = 0; i <= command.size(); i++)
        if (arr[i] == num) {
            std::cout << i << std::endl;
            return 0;
        }
    std::cout << -1 << std::endl;
    return 0;
}

#include <cassert>
#include <complex>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <sstream>
#include <utility>

#include <algorithm>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <memory.h>
//#include "bigint.h"

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define init(st) memset(st, 0, sizeof(st))
#define ll long long

// String operations
template<class T>
void splitstr(const string &s, vector<T> &out) {
    istringstream in(s);
    out.clear();
    copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}

string read_line() {
    string ret_val;
    getline(std::cin, ret_val);
    return ret_val;
}

template<class T>
void init_array(T *arr, int size, T value) {
    for (int i = 0; i < size; i++)
        arr[i] = value;
}

// Arithmetic operations
template<class T>
inline T gcd(T a, T b) {
    return (!a) ? b : gcd(b % a, a);
}

template<class T>
inline T mod(T a, T p) {
    a %= p;
    return (a < 0) ? a + p : a;
}

template<class T>
inline int num_bits(T n) {
    return (!n) ? 0 : 1 + numbits(n & (n - 1));
}

template<class T>
inline T inverse(T a, T m) {
    a = mod<T>(a,m);
    return (a==1) ? 1 : mod((1-m*inverse(m%a,a))/a,m);
}

template<class T>
inline bool is_prime(T a) {
    T lim = (T)sqrt((double)a);
    for (T i = 2; i <= T(lim+1E-9); i++)
        if (a % i == 0)
           return false;
    return true;
}

template<class T>
inline T power(T a, T p, T mod) {
    if (!p) return 1;
    T temp = power(a, p>>1, mod);
    temp = (temp * temp) % mod;
    if (p&1)
        temp = (temp * a) % mod;
    return temp;
}

void get_primes(int start, int end, vector<int> &vi) {
        int *p = new int[end + 1];
        init_array<int>(p, end+1, 0); p[1] = 1;
        for (int i = 2; i <= end; i++) {
                if (!p[i]) {
                        if (i >= start)
                                vi.push_back(i);
                        for (int j = 2*i; j <= end; j += i)
                                p[j] = 1;
                }
        }
    delete [] p;
}

// Graph operations
bool dfs(int current, int final, int total, int* visited, int **edges, bool flow) {
        if (current == final)
                return true;
        if (visited[current])
                return false;
        visited[current] = true;
        for (int i = 0; i < total; i++)
                if (edges[current][i] && dfs(i, final, total, visited, edges, flow)) {
                        if (flow) {
                                edges[current][i]--;
                                edges[i][current]++;
                        }
                        return true;
                }
        return false;
}

int flow(int in, int out, int total, int **edges) {
        int result = 0;
        int *visited = new int[total];
        while (init_array<int>(visited, total, 0), dfs(in, out, total, visited, edges, true))
                result++;
    delete [] visited;
        return result;
}

// Disjoint datasets
void create_set(int x, int *P, int *rank) {
        P[x] = x;
        rank[x] = 0;
}

int find_set(int x, int *P) {
        if (x != P[x])
                P[x] = find_set(P[x], P);
        return P[x];
}

bool merge_sets(int x, int y, int *P, int *rank) {
        int Px = find_set(x, P);
        int Py = find_set(y, P);
        if(Px == Py)
                return false;
        if (rank[Px] > rank[Py]) P[Py] = Px;
        else P[Px] = Py;
        if (rank[Px] == rank[Py])
                rank[Py]++;
        return true;
}

// Binary tree operations
template <typename T>
int read_cum_freq(int index, T *tree) {
        int sum = 0;
        while (index) {
                sum += tree[index];
                index -= (index & -index);
        }
        return sum;
}

template <typename T>
void upd_freq(int index, int mxIndex, int value, T *tree) {
        while (index <= mxIndex) {
                tree[index] += value;
                index += (index & -index);
        }
}

template <typename T>
int read_freq(int index, T *tree) {
        return read_cum_freq(index, tree) - read_cum_freq(index-1, tree);
}

// String algos.
void build_failure_function(const string &str, int *temp_arr) {
    temp_arr[0] = temp_arr[1] = 0;
    for (int i = 2; i <= sz(str); i++) {
        temp_arr[i] = -1;
        for (int ind = temp_arr[i-1]; temp_arr[i] == -1; ind = temp_arr[ind]) {
            if (str[ind] == str[i-1]) {
                temp_arr[i] = ind + 1;
            } else if (ind == 0) {
                temp_arr[i] = 0;
            }
        }
    }
}

void KMP(const string &text, const string &pattern, int *res) {
    int *temp_arr = new int[sz(pattern) + 1];
    build_failure_function(pattern, temp_arr);
    int i = 0;
    int ind = 0;
    while (i < sz(text)) {
        if (text[i] == pattern[ind]) {
            res[i] = ++ind;
            if (res[i] == sz(pattern)) {
                ind = temp_arr[ind];
            }
            i++;
        } else if (ind == 0) {
            res[i++] = 0;
        } else {
            ind = temp_arr[ind];
        }
    }
    delete [] temp_arr;
}

// Setup
void setup(int value, string name) {
    string name_in = name + ".in";
    string name_out = name + ".out";
    freopen(name_in.c_str(), "r", stdin);
    if (value) {
        freopen(name_out.c_str(), "w", stdout);
    }
}

int arr[100001];
int com[100001];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int main() {
#ifdef LOCAL_HOST
    setup(0, "temp");
#endif
    int n, m; std::cin >> n >> m;
    int k; std::cin >> k;
    vector<string> vs(n);
    for (int i = 0; i < n; i++)
        std::cin >> vs[i];
    int x, y, num = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (vs[i][j] == 'E') {
                y = i;
                x = j;
            }
    string command; std::cin >> command;
    string pattern = "LRUD";
    for (int i = 0; i < command.size(); i++) {
        com[i] = pattern.find(command[i]);
    }

    init(arr);
    for (int i = 1; i < n-1; i++)
        for (int j = 1; j < m-1; j++)
            if (vs[i][j] != '#' &&
                ((vs[i-1][j] == '#' && (vs[i][j-1] == '#' || vs[i][j+1] == '#')) ||
                 (vs[i+1][j] == '#' && (vs[i][j-1] == '#' || vs[i][j+1] == '#')))) {
                    num++;
                    int cx = j;
                    int cy = i;
                    if (cx == x && cy == y)
                        ++arr[0];
                    for (int k = 0; k < command.size(); k++) {
                        int nx = cx + dx[com[k]];
                        int ny = cy + dy[com[k]];
                        if (vs[ny][nx] != '#') {
                            cx = nx;
                            cy = ny;
                        }
                        if (cx == x && cy == y)
                            ++arr[k+1];
                    }
            }
    for (int i = 0; i <= command.size(); i++)
        if (arr[i] == num) {
            std::cout << i << std::endl;
            return 0;
        }
    std::cout << -1 << std::endl;
    return 0;
}

#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    int n, m, k; std::cin >> n >> m >> k;
    vector<string> vs(n);
    for (int i = 0; i < n; i++)
        std::cin >> vs[i];
    string commands; std::cin >> commands;
    bitset<150 * 150> mask, final, cur;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            if (vs[i][j] != '#') {
                mask.set(150 * i + j);
                if (vs[i][j] == 'E') {
                    final.set(150 * i + j);
                }
            }
        }
    cur = mask;
    for (int i = 0; i < commands.size(); i++, cur &= mask) {
        if (cur == final) {
            std::cout << i << std::endl;
            return 0;
        }
        switch(commands[i]) {
            case 'L': cur = (cur >> 1);
                break;
            case 'R': cur = (cur << 1);
                break;
            case 'U': cur = (cur >> 150);
                break;
            case 'D': cur = (cur << 150);
                break;
        }
    }
    if (cur == final)
        std::cout << commands.size() << std::endl;
    else
        std::cout << -1 << std::endl;
    return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

typedef long long int64;

const int INF = (int) 1E9;
const int64 INF64 = (int64) 1E18;
const double EPS = 1E-9;
const double PI = acos((double)0) * 2;

#define forn(i,n)  for (int i=0; i<int(n); ++i)
#define fore(i,l,n)  for (int i=int(l); i<int(n); ++i)
#define ford(i,n)  for (int i=int(n)-1; i>=0; --i)
#define all(a)  a.begin(), a.end()
#define fs  first
#define sc  second
#define mp  make_pair
#define pb  push_back

const int MAXN = 170;
const int CNT = 150*5;

int n, m, len;
char a[MAXN][MAXN];

unsigned field[CNT], mask[CNT], need[CNT];

bool read() {
	if (! (cin >> n >> m >> len))
		return false;
	forn(i,n)
		forn(j,m)
			scanf (" %c", &a[i][j]);
	return true;
}

inline void set_bit (unsigned a[], int idx, int bit) {
	a[idx] |= 1u << bit;
}

void process_op (char op) {
	if (op == 'U') {
		fore(i,5,CNT)
			field[i-5] = field[i];
	}
	if (op == 'D') {
		ford(i,CNT-5)
			field[i+5] = field[i];
	}
	if (op == 'L') {
		forn(i,CNT) {
			unsigned bit = field[i] & 1;
			field[i] >>= 1;
			if (i)
				field[i-1] |= bit << 31;
		}
	}
	if (op == 'R') {
		ford(i,CNT) {
			unsigned bit = (field[i] >> 31) & 1u;
			field[i] <<= 1;
			if (i+1 < CNT)
				field[i+1] |= bit;
		}
	}

	forn(i,CNT)
		field[i] &= mask[i];
}

bool check() {
	return memcmp (field, need, sizeof need) == 0;
}

void dump (unsigned a[]) {
	forn(i,n) {
		forn(j,m)
			if (a[i*5 + j/32] & (1u << (j % 32)))
				cerr << '1';
			else
				cerr << '0';
		cerr << endl;
	}
}

void solve() {
	memset (mask, 0, sizeof mask);
	memset (need, 0, sizeof need);
	forn(i,n)
		forn(j,m) {
			if (a[i][j] != '#')
				set_bit (mask, i*5 + j/32, j%32);
			if (a[i][j] =='E')
				set_bit (need, i*5 + j/32, j%32);
		}
	memcpy (field, mask, sizeof mask);

	/*
	cerr << "mask\n";
	dump (mask);
	cerr << "field\n";
	dump (field);
	cerr << "need\n";
	dump (need);
	*/

	if (check()) {
		puts ("0");
		return;
	}
	forn(i,len) {
		char op;
		scanf (" %c", &op);
		process_op (op);
		/*
		cerr << "after\n";
		dump (field);
		*/
		if (check()) {
			printf ("%d\n", i+1);

			fore(j,i+1,len)
				scanf (" %c", &op);

			return;
		}
	}
	puts("-1");
}

int main() {
#ifndef ONLINE_JUDGE
	freopen ("input.txt", "rt", stdin);
	freopen ("output.txt", "wt", stdout);
#endif

	while (read())
		solve();

}

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

struct Line
{
	unsigned int a[5];
	void setAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0xffffffff;
	}
	void resetAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0;
	}
	void mult(const Line &l)
	{
		for(int i = 0; i < 5; i++) a[i] &= l.a[i];
	}
	void shiftRight()
	{
		for(int i = 0; i < 5; i++)
		{
			a[i] <<= 1;
			if (i != 4 && a[i + 1] >= 0x80000000) a[i]++;
		}
	}
	void shiftLeft()
	{
		for(int i = 4; i >= 0; i--)
		{
			a[i] >>= 1;
			if (i != 0 && (a[i - 1] & 1) != 0) a[i] += 0x80000000;
		}
	}
	void set(int pos, int value)
	{
		if (value)
		{
			a[pos / 32] |= ((unsigned)1 << (pos & 31));
		}
		else
		{
			a[pos / 32] &= ~((unsigned)1 << (pos & 31));
		}
	}
	int countUpToTwo()
	{
		int res = 0;
		unsigned x;
		for(int i = 0; i < 5 && res < 2; i++)
		{
			x = a[i];
			while(x && res < 2)
			{
				x &= x - 1;
				res++;
			}
		}
		return res;
	}

	bool test(int pos)
	{
		return (a[pos / 32] & ((unsigned)1 << (pos & 31))) != 0;
	}
};

int n, m, k;
char s[150][151];
Line a[150], empty[150];
char cmd[100001];
int ei, ej;

bool enough()
{
	int cnt = 0;
	for(int i = 0; i < n && cnt < 2; i++) cnt += a[i].countUpToTwo();
	if (cnt > 1) return false;

	return a[ei].test(ej);
}

int main()
{
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 0; i < n; i++) scanf("%s", s[i]);
	scanf("%s", cmd);

	for(int i = 0; i < n; i++)
	{
		a[i].resetAll();
		for(int j = 0; j < m; j++)
		{
			if (s[i][j] != '#') a[i].set(j, 1);
			if (s[i][j] == 'E') ei = i, ej = j;
		}
		empty[i] = a[i];
	}

	int pos = 0;
	while(pos < k && !enough())
	{
		//for(int i = 0; i < n; i++, puts(""))
		//for(int j = 0; j < m; j++) putchar('0' + (int)a[i].test(j));
		//puts("-----");

		int ch = cmd[pos++];
		if (ch == 'L')
		{
			for(int i = 0; i < n; i++)
			{
				a[i].shiftLeft();
				a[i].mult(empty[i]);
			}
		}
		else
		if (ch == 'R')
		{
			for(int i = 0; i < n; i++)
			{
				a[i].shiftRight();
				a[i].mult(empty[i]);
			}
		}
		else
		if (ch == 'U')
		{
			for(int i = 0; i < n - 1; i++)
			{
				a[i] = a[i + 1];
				a[i].mult(empty[i]);
			}
			a[n - 1].resetAll();
		}
		else
		{
			// ch == 'D'
			for(int i = n - 1; i > 0; i--)
			{
				a[i] = a[i - 1];
				a[i].mult(empty[i]);
			}
			a[0].resetAll();
		}
	}

	if (pos == k && !enough()) pos = -1;

	printf("%d\n", pos);

	return 0;
}

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

struct Line
{
	unsigned int a[5];
	void setAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0xffffffff;
	}
	void resetAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0;
	}
	void And(const Line &l)
	{
		for(int i = 0; i < 5; i++) a[i] &= l.a[i];
	}
	void Or(const Line &l)
	{
		for(int i = 0; i < 5; i++) a[i] |= l.a[i];
	}
	void shiftRight()
	{
		for(int i = 0; i < 5; i++)
		{
			a[i] <<= 1;
			if (i != 4 && a[i + 1] >= 0x80000000) a[i]++;
		}
	}
	void shiftLeft()
	{
		for(int i = 4; i >= 0; i--)
		{
			a[i] >>= 1;
			if (i != 0 && (a[i - 1] & 1) != 0) a[i] += 0x80000000;
		}
	}
	void set(int pos, int value)
	{
		if (value)
		{
			a[pos / 32] |= ((unsigned)1 << (pos & 31));
		}
		else
		{
			a[pos / 32] &= ~((unsigned)1 << (pos & 31));
		}
	}
	int countUpToTwo()
	{
		int res = 0;
		unsigned x;
		for(int i = 0; i < 5 && res < 2; i++)
		{
			x = a[i];
			while(x && res < 2)
			{
				x &= x - 1;
				res++;
			}
		}
		return res;
	}

	bool test(int pos)
	{
		return (a[pos / 32] & ((unsigned)1 << (pos & 31))) != 0;
	}
};

int n, m, k;
char s[150][151];
Line a[150], empty[150], wall[150];
char cmd[100001];
int ei, ej;

bool enough()
{
	int cnt = 0;
	for(int i = 0; i < n && cnt < 2; i++) cnt += a[i].countUpToTwo();
	if (cnt > 1) return false;

	return a[ei].test(ej);
}

int main()
{
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 0; i < n; i++) scanf("%s", s[i]);
	scanf("%s", cmd);

	for(int i = 0; i < n; i++)
	{
		a[i].resetAll();
		wall[i].setAll();
		for(int j = 0; j < m; j++)
		{
			if (s[i][j] != '#') a[i].set(j, 1), wall[i].set(j, 0);
			if (s[i][j] == 'E') ei = i, ej = j;
		}
		empty[i] = a[i];
	}

	int pos = 0;
	Line tmp;
	while(pos < k && !enough())
	{
		//for(int i = 0; i < n; i++, puts(""))
		//for(int j = 0; j < m; j++) putchar('0' + (int)a[i].test(j));
		//puts("-----");

		int ch = cmd[pos++];
		if (ch == 'L')
		{
			for(int i = 0; i < n; i++)
			{
				tmp = wall[i];
				tmp.shiftRight();
				tmp.And(a[i]);
				a[i].shiftLeft();
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
		}
		else
		if (ch == 'R')
		{
			for(int i = 0; i < n; i++)
			{
				tmp = wall[i];
				tmp.shiftLeft();
				tmp.And(a[i]);
				a[i].shiftRight();
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
		}
		else
		if (ch == 'U')
		{
			for(int i = 0; i < n - 1; i++)
			{
				if (i) { tmp = wall[i - 1]; tmp.And(a[i]); }
				a[i] = a[i + 1];
				if (i) a[i].Or(tmp);
				a[i].And(empty[i]);
			}
			a[n - 1].resetAll();
		}
		else
		{
			// ch == 'D'
			for(int i = n - 1; i > 0; i--)
			{
				if (i != n - 1) { tmp = wall[i + 1]; tmp.And(a[i]); }
				a[i] = a[i - 1];
				if (i != n - 1) a[i].Or(tmp);
				a[i].And(empty[i]);
			}
			a[0].resetAll();
		}
	}

	if (pos == k && !enough()) pos = -1;

	printf("%d\n", pos);

	return 0;
}

#include <cstdio>
#include <cassert>
#include <cstring>
#include <string>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

struct Line
{
	unsigned int a[5];
	void setAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0xffffffff;
	}
	void resetAll()
	{
		for(int i = 0; i < 5; i++) a[i] = 0;
	}
	void And(const Line &l)
	{
		for(int i = 0; i < 5; i++) a[i] &= l.a[i];
	}
	void Or(const Line &l)
	{
		for(int i = 0; i < 5; i++) a[i] |= l.a[i];
	}
	void shiftRight()
	{
		for(int i = 0; i < 5; i++)
		{
			a[i] <<= 1;
			if (i != 4 && a[i + 1] >= (unsigned)0x80000000) a[i]++;
		}
	}
	void shiftLeft()
	{
		for(int i = 4; i >= 0; i--)
		{
			a[i] >>= 1;
			if (i != 0 && (a[i - 1] & 1) != 0) a[i] += (unsigned)0x80000000;
		}
	}
	void set(int pos, int value)
	{
		if (value)
		{
			a[pos / 32] |= ((unsigned)1 << (pos & 31));
		}
		else
		{
			a[pos / 32] &= ~((unsigned)1 << (pos & 31));
		}
	}
	int countUpToTwo()
	{
		int res = 0;
		unsigned x;
		for(int i = 0; i < 5 && res < 2; i++)
		{
			x = a[i];
			while(x && res < 2)
			{
				x &= x - 1;
				res++;
			}
		}
		return res;
	}

	bool test(int pos)
	{
		return (a[pos / 32] & ((unsigned)1 << (pos & 31))) != 0;
	}
};

int n, m, k;
char s[150][151];
Line a[150], empty[150], wall[150];
char cmd[100001];
int ei, ej;

bool enough()
{
	int cnt = 0;
	for(int i = 0; i < n && cnt < 2; i++) cnt += a[i].countUpToTwo();
	assert(cnt != 0);
	if (cnt > 1) return false;

	return a[ei].test(ej);
}

int main()
{
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 0; i < n; i++) scanf("%s", s[i]);
	scanf("%s", cmd);

	for(int i = 0; i < n; i++)
	{
		a[i].resetAll();
		wall[i].setAll();
		for(int j = 0; j < m; j++)
		{
			if (s[i][j] != '#') a[i].set(j, 1), wall[i].set(j, 0);
			if (s[i][j] == 'E') ei = i, ej = j;
		}
		empty[i] = a[i];
	}

	int pos = 0;
	Line tmp;
	while(pos < k && !enough())
	{
		//for(int i = 0; i < n; i++, puts(""))
		//for(int j = 0; j < m; j++) putchar('0' + (int)a[i].test(j));
		//puts("-----");

		int ch = cmd[pos++];
		if (ch == 'L')
		{
			for(int i = 0; i < n; i++)
			{
				tmp = wall[i];
				tmp.shiftRight();
				tmp.And(a[i]);
				a[i].shiftLeft();
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
		}
		else
		if (ch == 'R')
		{
			for(int i = 0; i < n; i++)
			{
				tmp = wall[i];
				tmp.shiftLeft();
				tmp.And(a[i]);
				a[i].shiftRight();
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
		}
		else
		if (ch == 'U')
		{
			for(int i = 1; i < n - 1; i++)
			{
				tmp = wall[i - 1];
				tmp.And(a[i]);
				a[i] = a[i + 1];
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
			a[0].resetAll();
			a[n - 1].resetAll();
		}
		else
		{
			// ch == 'D'
			for(int i = n - 2; i > 0; i--)
			{
				tmp = wall[i + 1];
				tmp.And(a[i]);
				a[i] = a[i - 1];
				a[i].Or(tmp);
				a[i].And(empty[i]);
			}
			a[0].resetAll();
			a[n - 1].resetAll();
		}
	}

	if (pos == k && !enough()) pos = -1;

	printf("%d\n", pos);

	return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
char s[100005];
int n, m, k;
int work(){
	bitset<23000> a, b, c, e;
	for(int i = 0; i < n; i++)
	{
		scanf("%s", s);
		for(int j = 0; j < m; j++)
		{
			(s[j]=='#'?b:a).set(i*m+j);
			(s[j]=='E'?e.set(i*m+j):0);
		}
	}
	scanf("%s", s);
	c = a;
	for(int i = 0; i < k; i++)
	{
		if(c==e)return i;
		if(s[i]=='U') c = ((c>>m)&a) | (c&(b<<m));
		if(s[i]=='L') c = ((c>>1)&a) | (c&(b<<1));
		if(s[i]=='D') c = ((c<<m)&a) | (c&(b>>m));
		if(s[i]=='R') c = ((c<<1)&a) | (c&(b>>1));
	}
	if(c==a)return k;
	return -1;
}
int main(){
	int i, j;
	while (~scanf("%d %d %d", &n, &m, &k)){
		printf("%d\n", work());
	}
	return 0;
}

/*
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 50005;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define Lson(x) tree[x].l
#define Rson(x) tree[x].r
#define Oldsum(x) tree[x].oldsum
#define Nowsum(x) tree[x].nowsum
inline int Mid(int x, int y){return (x+y)>>1;}
struct node{
int l, r;
int oldsum, nowsum;
}tree[N<<2];
void build(int l, int r, int id){
Lson(id) = l; Rson(id) = r;
Oldsum(id) = Nowsum(id) = 0;
if(l == r)return ;
int mid = Mid(l, r);
build(1, mid, L(id));
build(mid+1, r, R(id));
}

int n, m, a[N];
struct Q{
int l, r, ans, num;
}q[N];
bool cmp(Q x, Q y){return x.r < y.r;}
bool cmp(Q x, Q y){return x.num < y.num;}
void input(){
for(int i = 1; i <= n; i++)scanf("%d",&a[i]);
scanf("%d",&m);
for(int i = 1; i<= m; i++)scanf("%d %d",&q[i].l, &q[i].r), q[i].num = i;
sort(q+1, q+m+1);
}
int main() {
while (~scanf("%d",&n)) {
input();
build(1, n, 1);

}
return 0;
}
/**/

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
using namespace std;
char s[100005];
int n, m, k;
int work(){
	bitset<23000> a, b, c, e;
	for(int i = 0; i < n; i++)
	{
		scanf("%s", s);
		for(int j = 0; j < m; j++)
		{
			(s[j]=='#'?b:a).set(i*m+j);
			(s[j]=='E'?e.set(i*m+j):0);
		}
	}
	scanf("%s", s);
	c = a;
	for(int i = 0; i < k; i++)
	{
		if(c==e)return i;
		if(s[i]=='U') c = ((c>>m)&a) | (c&(b<<m));
		if(s[i]=='L') c = ((c>>1)&a) | (c&(b<<1));
		if(s[i]=='D') c = ((c<<m)&a) | (c&(b>>m));
		if(s[i]=='R') c = ((c<<1)&a) | (c&(b>>1));
	}
	return -1;
}
int main(){
	int i, j;
	while (~scanf("%d %d %d", &n, &m, &k)){
		printf("%d\n", work());
	}
	return 0;
}

/*
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int N = 50005;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define Lson(x) tree[x].l
#define Rson(x) tree[x].r
#define Oldsum(x) tree[x].oldsum
#define Nowsum(x) tree[x].nowsum
inline int Mid(int x, int y){return (x+y)>>1;}
struct node{
int l, r;
int oldsum, nowsum;
}tree[N<<2];
void build(int l, int r, int id){
Lson(id) = l; Rson(id) = r;
Oldsum(id) = Nowsum(id) = 0;
if(l == r)return ;
int mid = Mid(l, r);
build(1, mid, L(id));
build(mid+1, r, R(id));
}

int n, m, a[N];
struct Q{
int l, r, ans, num;
}q[N];
bool cmp(Q x, Q y){return x.r < y.r;}
bool cmp(Q x, Q y){return x.num < y.num;}
void input(){
for(int i = 1; i <= n; i++)scanf("%d",&a[i]);
scanf("%d",&m);
for(int i = 1; i<= m; i++)scanf("%d %d",&q[i].l, &q[i].r), q[i].num = i;
sort(q+1, q+m+1);
}
int main() {
while (~scanf("%d",&n)) {
input();
build(1, n, 1);

}
return 0;
}
/**/

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <bitset>
#include <string>
#include <cstring>
#define sz(a) int((a).size())
#define pb push_back
#define rep(i,j,k) for (int i=j; i<=k; i++)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define online
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef long long LL;

int main(){
    #ifndef online
            freopen("D.in","r",stdin);
            freopen("D.out","w",stdout);
    #endif

    bitset<22510> now,obs,ori,des;
    int n,m,k;
    cin>>n>>m>>k;

    rep(i,0,n-1){
        string s;
        cin>>s;
        rep(j,0,m-1){
            if (s[j]=='.'||s[j]=='E') ori.set(i*m+j);
            if (s[j]=='E') des.set(i*m+j);
        }
    }
    obs=ori; obs.flip(); now=ori;

    string s; cin>>s;

    int ans=-1;
    rep(i,0,k-1){
        if (des==now) {ans=i; break;}
        if (s[i]=='U')
            now=( ((now>>m)&ori) | (now&(obs<<m)) );
        if (s[i]=='D')
            now=( ((now<<m)&ori) | (now&(obs>>m)) );
        if (s[i]=='L')
            now=( ((now>>1)&ori) | (now&(obs<<1)) );
        if (s[i]=='R')
            now=( ((now<<1)&ori) | (now&(obs>>1)) );
    }

    cout<<(des==now?ans:-1)<<endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn=155,maxk=100005;
bitset<maxn*maxn> now;
bitset<maxn*maxn> wall;
bitset<maxn*maxn> pass;
bitset<maxn*maxn> goal;
char opr[maxk],field[maxn][maxn];
int n,m,k;
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%s",field[i]);
        for(int j=0;j<m;j++)
            if(field[i][j]!='#')
            {
                now.set(i*n+j);
                pass.set(i*n+j);
                if(field[i][j]=='E')
                    goal.set(i*n+j);
            }
            else
                wall.set(i*n+j);
    }
    pass=now;
    scanf("%s",opr);
    if(now==goal)
    {
        puts("0");
        return 0;
    }
    for(int i=0;i<k;i++)
    {
        if(opr[i]=='D')
            now=((now>>m)&pass)|(now&(wall<<m));
        if(opr[i]=='U')
            now=((now<<m)&pass)|(now&(wall>>m));
        if(opr[i]=='R')
            now=((now>>1)&pass)|(now&(wall<<1));
        if(opr[i]=='L')
            now=((now<<1)&pass)|(now&(wall>>1));
        if(now==goal)
        {
            printf("%d\n",i+1);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn=155;
struct line{
    unsigned int a[5];
    void setall()
    {
        for(int i=0;i<5;i++)
            a[i]=0xffffffff;
    }
    void resetall()
    {
        for(int i=0;i<5;i++)
            a[i]=0;
    }
    void And(const line &l)
    {
        for(int i=0;i<5;i++)
            a[i]&=l.a[i];
    }
    void Or(const line &l)
    {
        for(int i=0;i<5;i++)
            a[i]|=l.a[i];
    }
    void ShiftRight()
    {
        for(int i=4;i>=0;i--)
        {
            a[i]<<=1;
            if(i!=0&&a[i-1]&0x7fffffff)
                a[i]++;
        }
    }
    void ShiftLeft()
    {
        for(int i=0;i<5;i++)
        {
            a[i]>>=1;
            if(i!=4&&(a[i+1]&1))
                a[i]|=(unsigned)0x80000000;
        }
    }
    void set(int pos,int val)
    {
        if(val)
            a[pos>>5]|=(1u<<(pos&31));
        else
            a[pos>>5]&=~(1u<<(pos&31));
    }
    int countuptotwo()
    {
        int res=0;
        unsigned int x;
        for(int i=0;i<5&&res<2;i++)
        {
            x=a[i];
            while(x&&res<2)
            {
                x&=x-1;
                res++;
            }
        }
        return res;
    }
    bool test(int pos)
    {
        return a[pos>>5]&((unsigned)1<<(pos&31));
    }
};
int n,m,k;
char s[maxn][maxn];
line a[maxn],empty[maxn],wall[maxn];
char cmd[100005];
int ei,ej;
bool enough()
{
    int cnt=0;
    for(int i=0;i<n&&cnt<1;++i)
        cnt+=a[i].countuptotwo();
    assert(cnt!=0);
    if(cnt>1)
        return false;
    return a[ei].test(ej);
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++)
        scanf("%s",s[i]);
    scanf("%s",cmd);
    for(int i=0;i<n;i++)
    {
        a[i].resetall();
        wall[i].setall();
        for(int j=0;j<m;j++)
        {
            if(s[i][j]!='#')
            {
                a[i].set(j,1);
                wall[i].set(j,0);
            }
            if(s[i][j]=='E')
            {
                ei=i;
                ej=j;
            }
        }
        empty[i]=a[i];
    }
    int pos=0;
    line tmp;
    while(pos<k&&!enough())
    {
        int ch=cmd[pos++];
        if(ch=='L')
        {
            for(int i=0;i<n;i++)
            {
                tmp=wall[i];
                tmp.ShiftRight();
                tmp.And(a[i]);
                a[i].ShiftLeft();
                a[i].Or(tmp);
                a[i].And(empty[i]);
            }
        }
        if(ch=='R')
        {
            for(int i=0;i<n;i++)
            {
                tmp=wall[i];
                tmp.ShiftLeft();
                tmp.And(a[i]);
                a[i].ShiftRight();
                a[i].Or(tmp);
                a[i].And(empty[i]);
            }
        }
        if(ch=='U')
        {
            for(int i=1;i<n-1;i++)
            {
                tmp=wall[i-1];
                tmp.And(a[i]);
                a[i]=a[i+1];
                a[i].Or(tmp);
                a[i].And(empty[i]);
            }
            a[0].resetall();
            a[n-1].resetall();
        }
        if(ch=='D')
        {
            for(int i=n-2;i;i--)
            {
                tmp=wall[i+1];
                tmp.And(a[i]);
                a[i]=a[i-1];
                a[i].Or(tmp);
                a[i].And(empty[i]);
            }
            a[0].resetall();
            a[n-1].resetall();
        }
    }
    if(pos==k&&!enough())
        pos=-1;
    printf("%d\n",pos);
}

#include <functional>
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <bitset>
#include <complex>
#include <numeric>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <set>

using namespace std;

#define all(a)		(a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> pii;

char a[155][155], s[100500];
int n, m, len;
vector<pii> P;
bitset<150 * 150> msk, st;

int main(){
	#ifdef LocalHost
	freopen("x.in", "r", stdin);
	freopen("x.out", "w", stdout);
	#endif
	cin >> n >> m >> len;
	int e = 0;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		for (int j = 0; j < m; ++j) if (a[i][j] != '#') {
			msk.set(i * m + j);
			if (a[i][j] == 'E') {
				e = i * m + j;
			}
		}
	}
	st = msk;
	cin >> s;
	for (int i = 0; ; ++i) {
		if (st[e] && st.count() == 1) {
			printf("%d\n", i);
			return 0;
		}
		if (i == len) break;
		switch (s[i]) {
			case 'D':
				st = (st << m) & msk;
				break;
			case 'U':
				st = (st >> m) & msk;
				break;
			case 'L':
				st = (st >> 1) & msk;
				break;
			case 'R':
				st = (st << 1) & msk;
				break;
		}
	}
	puts("-1");
	return 0;
}

#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define INF 1000000007
#define EPS 1e-10
/*
	Problem: Robot in Basement
	Source: Codeforces 97D
	Author: fts2001
*/
using namespace std;
int n,m,k,ex,ey;
char ch[155][155];
string cmd;
bitset<22505> wal,bsm,cur;
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>m>>k;
	for(int i=0;i<n;i++){
		cin.ignore();
		for(int j=0;j<m;j++){
			cin>>ch[i][j];
			if(ch[i][j]=='E'){
				ex=i;
				ey=j;
				bsm.set(i*m+j);
				cur.set(i*m+j);
			}
			else if(ch[i][j]=='.'){
				bsm.set(i*m+j);
				cur.set(i*m+j);
			}
			else{
				wal.set(i*m+j);
			}
		}
	}
	cin>>cmd;
	for(int i=0;i<cmd.length();i++){
		if(cmd[i]=='L'){
			cur=(cur>>1)&bsm|(wal<<1)&cur;
		}
		else if(cmd[i]=='R'){
			cur=(cur<<1)&bsm|(wal>>1)&cur;
		}
		else if(cmd[i]=='U'){
			cur=(cur>>m)&bsm|(wal<<m)&cur;
		}
		else{
			cur=(cur<<m)&bsm|(wal>>m)&cur;
		}
		if(cur.count()==1 && cur[ex*m+ey]){
			cout<<i+1<<endl;
			return 0;
		}
	}
	cout<<"-1\n";
	return 0;
}

#include <cstdio>
#include <bitset>
using namespace std;
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    bitset<150*150> curr,mask,wall,dest;
    for(int i=0;i<n;i++)
    {
        char str[1024];
        scanf("%s",str);
        for(int j=0;j<m;j++)
        {
            if(str[j]!='#') mask.set(j+i*150); else wall.set(j+i*150);
            if(str[j]=='E') dest.set(j+i*150);
        }
    }
    curr=mask;
    static char cmd[110000];
    scanf("%s",cmd);
    int ans=0;
    while(true)
    {
        if(curr==dest)
        {
            printf("%d\n",ans);
            break;
        }
        if(cmd[ans]==0)
        {
            puts("-1");
            break;
        }
        switch(cmd[ans])
        {
            case 'L':
            curr=(curr>>1)|((wall<<1)&curr);
            break;
            case 'R':
            curr=(curr<<1)|((wall<<1)&curr);
            break;
            case 'U':
            curr=(curr>>150)|((wall<<150)&curr);
            break;
            case 'D':
            curr=(curr<<150)|((wall>>150)&curr);
            break;
        }
        curr&=mask;
        ans++;
    }
    return 0;
}

#include <cstdio>
#include <bitset>
using namespace std;
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    bitset<150*150> curr,mask,dest;
    for(int i=0;i<n;i++)
    {
        char str[1024];
        scanf("%s",str);
        for(int j=0;j<m;j++)
        {
            if(str[j]!='#') mask.set(j+i*150);
            if(str[j]=='E') dest.set(j+i*150);
        }
    }
    curr=mask;
    static char cmd[110000];
    scanf("%s",cmd);
    int ans=0;
    while(true)
    {
        if(curr==dest)
        {
            printf("%d\n",ans);
            break;
        }
        if(cmd[ans]==0)
        {
            puts("-1");
            break;
        }
        switch(cmd[ans])
        {
            case 'L':
            curr=(curr>>1)&mask;
            break;
            case 'R':
            curr=(curr<<1)&mask;
            break;
            case 'U':
            curr=(curr>>150)&mask;
            break;
            case 'D':
            curr=(curr<<150)&mask;
            break;
        }
        ans++;
    }
    return 0;
}

#include <cctype>
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <list>
#include <deque>
#include <stack>
#include <map>
#include <utility>
#include <cstring>
#include <sstream>
#include <bitset>
#include <assert.h>
#include <iomanip>
#include <climits>
#include <cstdio>
#include <ctime>

#define DEBUG_FLAG 1
#if DEBUG_FLAG
#define DBG(z) cout << #z << " : " << (z) << "\n";
#define DBY(x,y) cout << #x << " : " << (x) <<" , "<< #y << " : "<< (y) << "\n";
#define DBZ(x,y,z) cout << #x << " : " << (x) <<" , "<< #y << " : "<< (y) << " , " << #z << " : " << (z) << "\n";
#else
#define DBG(z)
#define DBY(x,y)
#define DBZ(x,y,z)
#endif

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset((x),(y),sizeof((x)))
#define all(c) c.begin(),c.end()
#define emp(a,i) (!((a) & (1<<(i))))
#define sh(a) (1<<(a))

#define PI 3.1415926535

using namespace std;
using namespace __gnu_cxx;

int n,m,sz;
char mat[155][155];
char k[110000];
bitset<155> final[155],a[155],b[155];

int main()
{
    scanf("%d%d%d",&n,&m,&sz);
    for(int i=0;i<n;i++)
        scanf("%s",mat[i]);
    for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    if(mat[i][j]=='E')  mat[i][j]='.',final[i][j]=1;
    for(int i=0;i<n;i++)    for(int j=0;j<m;j++)    if(mat[i][j]=='.')  a[i][j]=1;  else b[i][j]=1;
    scanf("%s",k);

    //a mi se kaj moze da sum, b mi e kaj ima precki

   /* for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cout<<a[i][j]<<" ";
            }
            cout<<"\n";
        }
    cout<<"\n\n\n";*/

    int ans=0;
    for(;ans<sz;ans++){
        bool error=0;
        for(int i=0;i<n;i++)
            if(final[i]!=a[i]){
                error=1;
                break;
            }
        if(!error)  break;
        if(k[ans]=='R'){
            for(int i=0;i<n;i++){
                bitset<155> tmp=b[i]>>1;
                tmp&=a[i];
                a[i]^=tmp;
                a[i]<<=1;
                a[i]|=tmp;
            }
        }
        if(k[ans]=='L'){
            for(int i=0;i<n;i++){
                bitset<155> tmp=b[i]<<1;
                tmp&=a[i];
                a[i]^=tmp;
                a[i]>>=1;
                a[i]|=tmp;
            }
        }
        if(k[ans]=='D'){
            for(int i=n-1;i>0;i--)
                a[i]=a[i-1];
            a[0].reset();
            for(int i=1;i<n;i++){
                bitset<155> tmp=a[i]&b[i];
                a[i]^=tmp;
                a[i-1]|=tmp;
            }
        }
        if(k[ans]=='U'){
            for(int i=0;i<n-1;i++)
                a[i]=a[i+1];
            a[n-1].reset();
            for(int i=n-2;i>=0;i--){
                bitset<155> tmp=a[i]&b[i];
                a[i]^=tmp;
                a[i+1]|=tmp;
            }
        }
     /*   for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cout<<a[i][j]<<" ";
            }
            cout<<"\n";
        }
        cout<<"\n\n";*/
    }
    if(ans>sz) ans=-1;

    printf("%d\n",ans);

    return 0;
}
/*
1 10
6 ....#...##
LLLLLL

1 10
6 #...#....#
RRRRRR

10 1
6
.
.
.
#
.
.
.
.
#
#
DDDDDD

10 1
6
#
#
.
.
.
#
.
.
.
.
UUUUUU
*/

#include<bits/stdc++.h>
#define u32 unsigned int
using namespace std;
const int MAXN = 1e5 + 10, SS = 150 * 150 + 150;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}
int N, M, Len, id;
bitset<SS> now, B, can;
char s[151], str[MAXN];
int main() {
	N = read(); M = read(); Len = read();
	for(int i = 1; i <= N; i++) {
		scanf("%s", s + 1);
		for(int j = 1; j <= M; j++) {
			if(s[j] == '#') B[i * M + j] = 1;
			else can[i * M + j] = now[i * M + j] = 1;
			if(s[j] == 'E') id = i * M + j;
		}
	}
	scanf("%s", str + 1);
	if(now.count() == 1 && now[id] == 1) return puts("0"), 0;
	for(int i = 1; i <= Len; i++) {
		if(str[i] == 'U') {
			now = ((now >> M) & can) | (now & (B << M));
		} else if(str[i] == 'D') {
			now = ((now << M) & can) | (now & (B >> M));
		} else if(str[i] == 'L') {
			now = ((now >> 1) & can) | (now & (B << 1));
		} else if(str[i] == 'R') {
			now = ((now >> M) & can) | (now & (B << M));
		}
	//	cout << now << endl;
	//	cout << now.count() << endl;
		if(now.count() == 1 && now[id] == 1) {
			printf("%d\n", i);
			return 0;
		}
	}
	puts("-1");
	return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int SIZEK=100010,SIZEN=160;
//typedef bitset<150*150> BS;
typedef bitset<25> BS;
int N,M,K;
int hash(int i,int j){return i*M+j;}
void print(BS &s){
	for(int i=0;i<N;i++){
		for(int j=0;j<M;j++){
			cout<<s[hash(i,j)];
		}
		cout<<endl;
	}
	cout<<endl;
}
char board[SIZEN][SIZEN]={0};
char cmd[SIZEK]={0};
BS now,goal,mask;
BS block[4];
//0123:��������
//����Ϊ��
int delta[4]={0};
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
int dirnum(char c){
	if(c=='U') return 0;
	else if(c=='D') return 1;
	else if(c=='L') return 2;
	else if(c=='R') return 3;
}
void bitmove(BS &s,int k){
	//cout<<s<<" "<<k<<endl;
	if(k>=0) s<<=k;
	else s>>=-k;
	//cout<<s<<endl;
}
BS tmp;
void move(char c){
	int d=dirnum(c);
	tmp=now&block[d];
	//print(tmp);
	now^=tmp;
	//print(now);
	//cout<<delta[d]<<endl;
	bitmove(now,delta[d]);
	//print(now);
	now|=tmp;
	now&=mask;
}
void work(void){
	if(now==goal){
		printf("0\n");
		return;
	}
	//cout<<goal<<endl;
	//print(now);
	//print(block[3]);
	for(int i=1;i<=K;i++){
		move(cmd[i-1]);
		//print(now);
		if(now==goal){
			printf("%d\n",i);
			return;
		}
	}
	printf("-1\n");
}
void prepare(void){
	for(int i=0;i<N;i++){
		for(int j=0;j<M;j++){
			if(board[i][j]=='.'||board[i][j]=='E') now[hash(i,j)]=mask[hash(i,j)]=1;
			if(board[i][j]=='E') goal[hash(i,j)]=1;
			if(board[i][j]=='#'){
				for(int d=0;d<4;d++){
					int i1=i+dx[d],j1=j+dy[d];
					if(0<=i1&&i1<N&&0<=j1&&j1<M){
						if(board[i1][j1]!='#') block[d^1][hash(i1,j1)]=1;
					}
				}
			}
		}
	}
}
void read(void){
	scanf("%d %d %d\n",&N,&M,&K);
	delta[0]=-M,delta[1]=M,delta[2]=-1,delta[3]=1;
	for(int i=0;i<N;i++) scanf("%s",board[i]);
	scanf("%s",cmd);
}
int main(){
	//freopen("robotinbasement.in","r",stdin);
	read();
	prepare();
	//cout<<now<<endl;now>>=5;cout<<now<<endl;print(now);
	//move('R');print(now);
	work();
	return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <bitset>
#define maxn 160
#define maxm maxn
#define maxk 100010
using namespace std;
//l:>>1 r:<<1 u:>>m d:<<m
int n,m,k;
char in[maxk];
bitset<100> cur,ext,ipb,cl,cr,cu,cd;

int main()
{
	scanf("%d%d%d",&n,&m,&k);
	register int i,j;
	cur|=1;
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			cur|=(cur<<1);
	cur>>=1;
	for(i=0;i<n;++i)
	{
		scanf("%s",in);
		for(j=0;j<m;++j)
		{
			if(in[j]=='#')
			{
				cur[i*m+j]=0;
				ipb|=(1<<i*m+j);
			}
			if(in[j]=='E')
				ext|=(1<<i*m+j);
		}
	}
	cl=ipb<<1;cr=ipb>>1;cu=ipb<<m;cd=ipb>>m;
	scanf("%s",in);
	for(i=0;i<k;++i)
	{
		if(cur==ext)
		{
			printf("%d",i);
			return 0;
		}
		switch(in[i])
		{
			case 'L':
				cur=(cur&cl) | ((cur^(cur&cl))>>1);
				break;
			case 'R':
				cur=(cur&cr) | ((cur^(cur&cr))<<1);
				break;
			case 'U':
				cur=(cur&cu) | ((cur^(cur&cu))>>m);
				break;
			case 'D':
				cur=(cur&cd) | ((cur^(cur&cd))<<m);
				break;
		}
	}
	if(cur==ext)
		printf("%d",k);
	else
		printf("-1");
	return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <bitset>
#define maxn 160
#define maxm maxn
#define maxk 100010
using namespace std;
//l:>>1 r:<<1 u:>>m d:<<m
int n,m,k;
char in[maxk];
bitset<100> cur,ext,ipb,cl,cr,cu,cd;

int main()
{
	scanf("%d%d%d",&n,&m,&k);
	register int i,j;
	cur|=1;
	for(i=0;i<n;++i)
		for(j=0;j<m;++j)
			cur|=(cur<<1);
	cur>>=1;
	for(i=0;i<n;++i)
	{
		scanf("%s",in);
		for(j=0;j<m;++j)
		{
			if(in[j]=='#')
			{
				cur[i*m+j]=0;
				ipb|=(1<<i*m+j);
			}
			if(in[j]=='E')
				ext|=(1<<i*m+j);
		}
	}
	cl=ipb|ipb<<1;cr=ipb|ipb>>1;cu=ipb|ipb<<m;cd=ipb|ipb>>m;
	scanf("%s",in);
	for(i=0;i<k;++i)
	{
		if(cur==ext)
		{
			printf("%d",i);
			return 0;
		}
		switch(in[i])
		{
			case 'L':
				cur=(cur&cl | cur^(cur&cl))>>1;
				break;
			case 'R':
				cur=(cur&cr | cur^(cur&cr))<<1;
				break;
			case 'U':
				cur=(cur&cu | cur^(cur&cu))>>m;
				break;
			case 'D':
				cur=(cur&cd | cur^(cur&cd))<<m;
				break;
		}
		cur&=~ipb;
	}
	if(cur==ext)
		printf("%d",k);
	else
		printf("-1");
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<map>
#include<bitset>
#define debug(x) cerr<<"\tdebug:"<<#x<<"="<<x<<endl
#define debugg(x,y) cerr<<"\tdebug;"<<x<<":"<<#y<<"="<<y<<endl
#define debugzu(x,a,b) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++)cerr<<x[i]<<" ";fprintf(stderr,"\n");
#define debugerzu(x,a,b,c,d) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++,fprintf(stderr,"\n\t"))for(int j=c;j<d;j++)cerr<<x[i][j]<<" ";fprintf(stderr,"\n");
#define R register
#define LL long long
#define I inline
using namespace std;
bitset <23000> a,b,c,e;

int main()
{
 	freopen("CF97D.in","r",stdin);
// 	freopen("CF97D.out","w",stdout);
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	getchar();
	for(int i=0;i<n;i++,getchar())
		for(int j=0;j<m;j++)
		{
			char s=getchar();
			(s=='#'?b:a).set(i*m+j);
			(s=='E'?e.set(i*m+j):0);
		}
	c=a;
	for(int i=0;i<k;i++)
	{
		char s=getchar();
		if(c==e)
		{
			printf("%d\n",i);
			return 0;
		}
		if(s=='U') c = ( (c>>m)&a) | (c&(b<<m));
		if(s=='L') c = ((c>>1)&a) | (c&(b<<1));
		if(s=='D') c = ((c<<m)&a) | (c&(b>>m));
		if(s=='R') c = ((c<<1)&a) | (c&(b>>1));
	}
	if(c==e)
		printf("%d\n",k);
	else
		printf("%d\n",-1);

	fclose(stdin);
	fclose(stdout);
 	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<map>
#include<bitset>
#define debug(x) cerr<<"\tdebug:"<<#x<<"="<<x<<endl
#define debugg(x,y) cerr<<"\tdebug;"<<x<<":"<<#y<<"="<<y<<endl
#define debugzu(x,a,b) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++)cerr<<x[i]<<" ";fprintf(stderr,"\n");
#define debugerzu(x,a,b,c,d) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++,fprintf(stderr,"\n\t"))for(int j=c;j<d;j++)cerr<<x[i][j]<<" ";fprintf(stderr,"\n");
#define R register
#define LL long long
#define I inline
using namespace std;
bitset <23000> a,b,c,e;

int main()
{
 	freopen("CF97D.in","r",stdin);
// 	freopen("CF97D.out","w",stdout);
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	getchar();
	for(int i=0;i<n;i++,getchar())
		for(int j=0;j<m;j++)
		{
			char s=getchar();
			(s=='#'?b:a).set(i*m+j);
			(s=='E'?e.set(i*m+j):0);
		}
	c=a;
	for(int i=0;i<k;i++)
	{
		char s=getchar();
		if(c==e)
		{
			printf("%d\n",i);
			return 0;
		}
		if(s=='U') c = ( (c>>m)&a) | (c&(b<<m));
		else if(s=='L') c = ((c>>1)&a) | (c&(b<<1));
		else if(s=='D') c = ((c<<m)&a) | (c&(b>>m));
		else if(s=='R') c = ((c<<1)&a) | (c&(b>>1));
	}
	if(c==e)
		printf("%d\n",k);
	else
		printf("%d\n",-1);;

	fclose(stdin);
	fclose(stdout);
 	return 0;
}

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include<cstring>
#include<string>
#include<cctype>
#include<cstdlib>
#include<map>
#include<bitset>
#define debug(x) cerr<<"\tdebug:"<<#x<<"="<<x<<endl
#define debugg(x,y) cerr<<"\tdebug;"<<x<<":"<<#y<<"="<<y<<endl
#define debugzu(x,a,b) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++)cerr<<x[i]<<" ";fprintf(stderr,"\n");
#define debugerzu(x,a,b,c,d) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++,fprintf(stderr,"\n\t"))for(int j=c;j<d;j++)cerr<<x[i][j]<<" ";fprintf(stderr,"\n");
#define R register
#define LL long long
#define I inline
using namespace std;
bitset <23000> a,b,c,e;

int main()
{
 	freopen("CF97D.in","r",stdin);
// 	freopen("CF97D.out","w",stdout);
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	getchar();
	for(int i=0;i<n;i++,getchar())
		for(int j=0;j<m;j++)
		{
			char s=getchar();
			(s=='#'?b:a).set(i*m+j);
			(s=='E'?e.set(i*m+j):0);
		}
	c=a;
	for(int i=0;i<k;i++)
	{
		char s=getchar();
		if(c==e)
		{
			printf("%d\n",i);
			return 0;
		}
		if(s=='U') c = ( (c>>m)&a) | (c&(b<<m));
		else if(s=='L') c = ((c>>1)&a) | (c&(b<<1));
		else if(s=='D') c = ((c<<m)&a) | (c&(b>>m));
		else if(s=='R') c = ((c<<1)&a) | (c&(b>>1));
	}
	if(c==e)
		printf("%d\n",k);
	else
		printf("%d\n",-1);;
 	return 0;
}

#include <cstdio>
#include <numeric>
#include <iostream>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <queue>
#include <sstream>
#include <deque>

using namespace std;

#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i = 0; i < (n); i++)
#define re return
#define fi first
#define se second
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(abs(x))
#define y0 y3487465
#define y1 y8687969
#define fill(x,y) memset(x,y,sizeof(x))

typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef double D;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<vi> vvi;

template<class T> T abs(T x) { re x > 0 ? x : -x; }

int n;
int m, k;

bitset<151> f[151], w[151], fn[151], cur[151];
string g[151];
string s;

int main () {
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++) cin >> g[i];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (g[i][j] == '#')
                w[i][j] = 1;
            else {
                f[i][j] = 1;
                if (g[i][j] == 'E') fn[i][j] = 1;
            }
        for (int i = 0; i < n; i++) cur[i] = f[i];
        cin >> s;
        for (int it = 0; it < k; it++) {
            if (s[it] == 'L')
                for (int i = 0; i < n; i++)
                    cur[i] = ((cur[i] >> 1) & f[i]) | (((cur[i] >> 1) & w[i]) << 1);
            if (s[it] == 'R')
                for (int i = 0; i < n; i++)
                    cur[i] = ((cur[i] << 1) & f[i]) | (((cur[i] << 1) & w[i]) >> 1);
            if (s[it] == 'U')
                for (int i = 1; i + 1 < n; i++)
                    cur[i] = (cur[i + 1] & f[i]) | (cur[i] & w[i - 1]);
            if (s[it] == 'D')
                for (int i = n - 2; i > 0; i--)
                    cur[i] = (cur[i - 1] & f[i]) | (cur[i] & w[i + 1]);
            int ok = 1;
            for (int i = 0; i < n; i++)
                if (cur[i] != fn[i])
                    ok = 0;
            if (ok) {
                printf ("%d\n", it + 1);
                re 0;
            }
        }
        printf ("-1\n");
    return 0;
}

#include <cmath>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
bitset<25000> now, ul, dl, ll, rl;
int n, m, k, endx, endy;
int main()
{
	scanf("%d%d%d", &n, &m, &k);
	char c;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
		{
			while ((c = getchar()) != '#' && c != '.' && c != 'E');
			if (c == '#')
			{
				now[i * m + j] = 0;
				if (i != 0 && now[(i - 1) * m + j] == 1) dl[(i - 1) * m + j] = 1;
				if (j != 0 && now[i * m + j - 1] == 1) rl[i * m + j - 1] = 1;
			}
			else
			{
				now[i * m + j] = 1;
				if (i != 0 && now[(i - 1) * m + j] == 0) ul[i * m + j] = 1;
				if (j != 0 && now[i * m + j - 1] == 0) ll[i * m + j] = 1;
				if (c == 'E')
				{
					endx = i;
					endy = j;
				}
			}
		}
	for (int i = 0; i < k; i++)
	{
			while ((c = getchar()) != 'U' && c != 'D' && c != 'L' && c != 'R');
			if (c == 'U') now = (now & ul) | ((now ^ (now & ul)) >> m);
			if (c == 'D') now = (now & dl) | ((now ^ (now & dl)) << m);
			if (c == 'L') now = (now & ll) | ((now ^ (now & ll)) >> 1);
			if (c == 'R') now = (now & rl) | ((now ^ (now & rl)) << 1);
			if (now.count() == 1 && now[endx * m + endy] == 1)
			{
				printf("%d\n", i + 1);
				return 0;
			}
	}
	printf("-1\n");
	return 0;
}

#pragma comment (linker,"/stack:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<bitset>
#include<queue>
#include<functional>
#include<iostream>
#include<string>
using namespace std;
const double PI=acos(-1.0);
#define N 100001
#define inf 0x7fffffff
#define fib 112233
#define mod 100007
#define ll __int64
#define able puts("0000000000000000000000")
template<class T_T> T_T f_max(T_T a,T_T b) { return a>b?a:b; }
template<class T_T> T_T f_min(T_T a,T_T b) { return a<b?a:b; }
template<class T_T> T_T f_abs(T_T a) { return a>0?a:-a; }
template<class T_T> T_T gcd(T_T a,T_T b){ return b?gcd(b,a%b):a; }
template<class T_T> void swap(T_T *a,T_T *b){T_T c;c=a;a=b;b=c;}
int n,m;

bitset<40000> bs,al,ar,au,ad,tp;
char mp[200][200];
char op[N];
int main()
{
    int q,w,e,r;
    int i,j,k;
    int t,pos,end;
    while(~scanf("%d%d%d",&n,&m,&k)){
        for(i=0;i<n;i++){
            scanf("%s",mp[i]);
        }
        al.set();
        ar.set();
        au.set();
        ad.set();
        bs.reset();
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                pos=i*m+j;
                if(mp[i][j]=='.'){
                    bs.set(pos);
                    if(j!=0 && mp[i][j-1]!='#') al.reset(pos);
                    if(j!=m-1 && mp[i][j+1]!='#') ar.reset(pos);
                    if(i!=0 && mp[i-1][j]!='#') au.reset(pos);
                    if(i!=n-1 && mp[i+1][j]!='#') ad.reset(pos);
                }
                else if(mp[i][j]=='E'){
                    end=pos;
                }
            }
        }
        scanf("%s",op);
        for(i=0;i<k;i++){
            if(bs.none()) break;
            if(op[i]=='L'){ tp=al;bs=( bs & al ) | ( (bs & tp.flip())>>1 );}
            if(op[i]=='R'){ tp=ar;bs=( bs & ar ) | ( (bs & tp.flip())<<1 );}
            if(op[i]=='U'){ tp=au;bs=( bs & au ) | ( (bs & tp.flip())>>m );}
            if(op[i]=='D'){ tp=ad;bs=( bs & ad ) | ( (bs & tp.flip())<<m );}
            bs.reset(end);
        }
        if(bs.any()) i=-1;
        printf("%d\n",i);
    }
    return 0;
}

#pragma comment (linker,"/stack:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<bitset>
#include<queue>
#include<functional>
#include<iostream>
#include<string>
using namespace std;
const double PI=acos(-1.0);
#define N 100001
#define inf 0x7fffffff
#define fib 112233
#define mod 100007
#define ll __int64
#define able puts("0000000000000000000000")
template<class T_T> T_T f_max(T_T a,T_T b) { return a>b?a:b; }
template<class T_T> T_T f_min(T_T a,T_T b) { return a<b?a:b; }
template<class T_T> T_T f_abs(T_T a) { return a>0?a:-a; }
template<class T_T> T_T gcd(T_T a,T_T b){ return b?gcd(b,a%b):a; }
template<class T_T> void swap(T_T *a,T_T *b){T_T c;c=a;a=b;b=c;}
int n,m;

bitset<40000> bs,al,ar,au,ad,tp;
char mp[200][200];
char op[N];
int main()
{
    int q,w,e,r;
    int i,j,k;
    int t,pos,end;
    while(~scanf("%d%d%d",&n,&m,&k)){
        for(i=0;i<n;i++){
            scanf("%s",mp[i]);
        }
        al.set();
        ar.set();
        au.set();
        ad.set();
        bs.reset();
        for(i=0;i<n;i++){
            for(j=0;j<m;j++){
                pos=i*m+j;
                if(mp[i][j]=='.'){
                    bs.set(pos);
                    if(j!=0 && mp[i][j-1]!='#') al.reset(pos);
                    if(j!=m-1 && mp[i][j+1]!='#') ar.reset(pos);
                    if(i!=0 && mp[i-1][j]!='#') au.reset(pos);
                    if(i!=n-1 && mp[i+1][j]!='#') ad.reset(pos);
                }
                else if(mp[i][j]=='E'){
                    end=pos;
                }
            }
        }
        scanf("%s",op);
        int ans=0;
        if(bs.count()){
        for(i=0;i<k;i++){
            if(op[i]=='L'){ tp=al;bs=( bs & al ) | ( (bs & tp.flip())>>1 );}
            if(op[i]=='R'){ tp=ar;bs=( bs & ar ) | ( (bs & tp.flip())<<1 );}
            if(op[i]=='U'){ tp=au;bs=( bs & au ) | ( (bs & tp.flip())>>m );}
            if(op[i]=='D'){ tp=ad;bs=( bs & ad ) | ( (bs & tp.flip())<<m );}
            if(bs.count()==1 && bs.test(end) ) break;
        }
        if(i<k) ans=i+1;
        else ans=-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<ctime>
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<deque>
#include<utility>
#include<vector>
#include<functional>
#include<string>
#include<stack>
#define rep(i,n) for(int i=0;i<n;i++)
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<pii,int> piii;
int n,m,k;
char cmap[155][155];
char order[100005];
bitset<23333> a,b,c;
void init(){
	scanf("%d%d%d",&n,&m,&k);
	rep(i,n)
		scanf("%s",cmap[i]);
	scanf("%s",order);
	rep(i,n)
		rep(j,m)
			if(cmap[i][j]=='#')
				b.set(i*m+j);
			else if(cmap[i][j]=='.'){
				a.set(i*m+j);
				c.set(i*m+j);
			}
}
void slove(){
	int i;
	for(i=0;i<k;i++)
		if(c.count()){
			char t=order[i];
			if(t=='R')
				c=((c<<1)&a)|((b>>1)&c);
			else if(t=='D')
				c=((c<<m)&a)|((b>>m)&c);
			else if(t=='L')
				c=((c>>1)&a)|((b<<1)&c);
			else
				c=((c>>m)&a)|((b<<m)&c);
		}
		else{
			printf("%d\n",i);
			return;
		}
	if(c.count())
		puts("-1");
	else
		printf("%d\n",k);
	return;
}
int main(){
	init();
	slove();
	return 0;
}

#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) ((S&two(X))>0)
#define containL(S,X) ((S&twoL(X))>0)
typedef pair<int,int> ipair;
int countbit(int n) {return (n==0)?0:(1+countbit(n&(n-1)));}
int lowbit(int n) {return (n^(n-1))&n;}
const double pi=acos(-1.0);
const double eps=1e-11;
template<class T> T sqr(T x) {return x*x;}
template<class T> void checkmin(T &a,T b) {if (b<a) a=b;}
template<class T> void checkmax(T &a,T b) {if (b>a) a=b;}
#define SIZE(X) ((int)((X).size()))
#define LENGTH(X) ((int)((X).length()))
#define MP(A,B) make_pair(A,B)

int n;
double p[105];

int main()
{
#ifdef _MSC_VER
	freopen("input.txt","r",stdin);
#endif
	scanf("%d",&n);
	for (int i=0;i<=n;i++) scanf("%lf",&p[i]);
	double R=0;
	for (int i=0;i<=n/2;i++) checkmax(R,p[i]);
	for (int i=0;i<=n;i++) for (int j=i+1;j<=n;j++)
	{
		double a1=1,b1=1,c1=-1;
		double a2=2*i,b2=2*j,c2=-n;
		double t=a1*b2-a2*b1;
		double x=+(b1*c2-b2*c1)/t;
		double y=-(a1*c2-a2*c1)/t;
		if (x>=0 && y>=0)
			checkmax(R,x*p[i]+y*p[j]);
	}
	printf("%.18lf\n",R);
	return 0;
}

#include <string>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <utility>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) ((S&two(X))>0)
#define containL(S,X) ((S&twoL(X))>0)
typedef pair<int,int> ipair;
int countbit(int n) {return (n==0)?0:(1+countbit(n&(n-1)));}
int lowbit(int n) {return (n^(n-1))&n;}
const double pi=acos(-1.0);
const double eps=1e-11;
template<class T> T sqr(T x) {return x*x;}
template<class T> void checkmin(T &a,T b) {if (b<a) a=b;}
template<class T> void checkmax(T &a,T b) {if (b>a) a=b;}
#define SIZE(X) ((int)((X).size()))
#define LENGTH(X) ((int)((X).length()))
#define MP(A,B) make_pair(A,B)
typedef unsigned int uint;

const int maxk=100000+5;

int n,m,k,dstx,dsty;
uint mask[160][5],state[160][5];
char cmd[maxk];

bool ck()
{
	for (int i=0;i<n;i++)
		if (i!=dstx)
		{
			for (int j=0;j<5;j++) if (state[i][j]) return false;
		}
		else
		{
			for (int j=0;j<5;j++) if (j!=(dsty>>5) && state[i][j]) return false;
			if (state[i][dsty>>5]!=(1u<<(dsty&31))) return false;
		}
	return true;
}
int main()
{
#ifdef _MSC_VER
	freopen("input.txt","r",stdin);
#endif
	memset(mask,0,sizeof(mask));
	scanf("%d%d%d",&n,&m,&k);
	for (int i=0;i<n;i++)
	{
		scanf("%s",cmd);
		for (int j=0;j<m;j++)
			if (cmd[j]!='#')
			{
				mask[i][j>>5]|=(1u<<(j&31));
				if (cmd[j]=='E') dstx=i,dsty=j;
			}
	}
	scanf("%s",cmd);
	memcpy(state,mask,sizeof(state));
	if (ck())
	{
		printf("0\n");
		return 0;
	}
	for (int step=0;step<k;step++)
	{
		char key=cmd[step];
		if (key=='L')
		{
			uint last=0;
			for (int x=0;x<n;x++) for (int i=4;i>=0;i--)
			{
				uint next=state[x][i]&1;
				state[x][i]=state[x][i]>>1;
				if (last) state[x][i]|=(1u<<31);
				last=next;
			}
		}
		else if (key=='R')
		{
			uint last=0;
			for (int x=0;x<n;x++) for (int i=0;i<5;i++)
			{
				uint next=state[x][i]>>31;
				state[x][i]=(state[x][i]<<1)|last;
				last=next;
			}
		}
		else if (key=='U')
		{
			for (int x=0;x<n-1;x++) for (int i=0;i<5;i++) state[x][i]=state[x+1][i];
			for (int i=0;i<5;i++) state[n-1][i]=0;
		}
		else
		{
			for (int x=n-1;x>0;x--) for (int i=0;i<5;i++) state[x][i]=state[x-1][i];
			for (int i=0;i<5;i++) state[0][i]=0;
		}
		for (int x=0;x<n;x++) for (int i=0;i<5;i++) state[x][i]&=mask[x][i];
		if (ck())
		{
			printf("%d\n",step+1);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li		long long
#define rep(i,to)	for(li i=0;i<((li)to);i++)
#define pb		push_back
#define sz(v)		((li)v.size())

#define MAX 155
int main(){
	bitset<MAX*MAX> goal,mask,wall,now;
	li n,m,k;
	cin>>n>>m>>k;
	rep(i,n){
		string str;
		cin>>str;
		rep(j,m){
			((str[j]=='#')?wall:mask).set(i*MAX+j);
			if(str[j]=='E') goal.set(i*MAX+j);
		}
	}
	string ins;
	cin>>ins;
	now=mask;
	rep(i,sz(ins)){
		if(now==goal){
			cout<<i<<endl;
			return 0;
		}
		if(ins[i]=='U')	now=((now>>MAX)&mask)|(now&(wall<<MAX));
		else if(ins[i]=='D') now=((now<<MAX)&mask)|(now&(wall>>MAX));
		else if(ins[i]=='R') now=((now<<1)&mask)|(now&(wall>>1));
		else if(ins[i]=='L') now=((now>>1)&mask)|(now&(wall<<1));
		else cout<<"error inst"<<endl;
	}
	cout<<"-1"<<endl;
}

#include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <bitset>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define li		long long
#define rep(i,to)	for(li i=0;i<((li)to);i++)
#define pb		push_back
#define sz(v)		((li)v.size())

#define MAX 155
int main(){
	bitset<MAX*MAX> goal,mask,wall,now;
	li n,m,k;
	cin>>n>>m>>k;
	rep(i,n){
		string str;
		cin>>str;
		rep(j,m){
			((str[j]=='#')?wall:mask).set(i*MAX+j);
			if(str[j]=='E') goal.set(i*MAX+j);
		}
	}
	string ins;
	cin>>ins;
	now=mask;
	rep(i,sz(ins)){
		if(now==goal){
			cout<<i<<endl;
			return 0;
		}
		if(ins[i]=='U')	now=((now>>MAX)&mask)|(now&(wall<<MAX));
		else if(ins[i]=='D') now=((now<<MAX)&mask)|(now&(wall>>MAX));
		else if(ins[i]=='R') now=((now<<1)&mask)|(now&(wall>>1));
		else if(ins[i]=='L') now=((now>>1)&mask)|(now&(wall<<1));
		else cout<<"error inst"<<endl;
	}
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <bitset>
using namespace std;

#define N 100005

int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<0?-x:x;}
int n, m, q;
char map[155][155];
char s[N];

int solve(){
	int i, j, k;
	bitset<155*155>dian, qiang, e, ro;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(map[i][j]=='#') qiang.set(i*m+j);
			if(map[i][j]=='.'||map[i][j]=='E') dian.set(i*m+j);
			if(map[i][j]=='E') e.set(i*m+j);
		}
	}
	ro=dian;
	for(i=0;i<q;i++){

		if(s[i]=='U') ro=((ro>>m)&dian)|(ro&(qiang<<m));
		else if(s[i]=='D') ro=((ro<<m)&dian)|(ro&(qiang>>m));
		else if(s[i]=='L') ro=((ro>>1)&dian)|(ro&(qiang<<1));
		else ro=((ro<<1)&dian)|(ro&(qiang>>1));
		if(ro==e) return i+1;
	}

	if(ro==e) return 11;
	return -1;
}

main()
{
	int i, j, k;
	while(scanf("%d %d %d",&n,&m,&q)==3){
		for(i=0;i<n;i++) scanf("%s",map[i]);
		scanf("%s",s);
		printf("%d\n",solve());
	}
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <bitset>
using namespace std;

#define N 100005

int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<0?-x:x;}
int n, m, q;
char map[155][155];
char s[N];

int solve(){
	int i, j, k;
	bitset<155*155>dian, qiang, e, ro;
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			if(map[i][j]=='#') qiang.set(i*m+j);
			if(map[i][j]=='.'||map[i][j]=='E') dian.set(i*m+j);
			if(map[i][j]=='E') e.set(i*m+j);
		}
	}
	ro=dian;
	for(i=0;i<q;i++){
		if(ro==e) return i;
		if(s[i]=='U') ro=((ro>>m)&dian)|(ro&(qiang<<m));
		else if(s[i]=='D') ro=((ro<<m)&dian)|(ro&(qiang>>m));
		else if(s[i]=='L') ro=((ro>>1)&dian)|(ro&(qiang<<1));
		else ro=((ro<<1)&dian)|(ro&(qiang>>1));

	}

	if(ro==e) return 11;
	return -1;
}

main()
{
	int i, j, k;
	while(scanf("%d %d %d",&n,&m,&q)==3){
		for(i=0;i<n;i++) scanf("%s",map[i]);
		scanf("%s",s);
		printf("%d\n",solve());
	}
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<vector>
using namespace std;

const int N = 155, K = 100005;
int n, m, ex, ey;
char map[N][N], op[K];
bitset<150> st[150], tmp[150], stableUp[150], stableDown[150], stableLeft[150], stableRight[150];

void goup() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableUp[i];
		st[i]  ^= tmp[i];
	}
	for (int i = 0; i + 1 < n; ++i) {
		st[i] = st[i + 1];
	}
	st[n - 1].reset();
	for (int i = 0; i < n; ++i) {
		st[i] |= tmp[i];
	}
}

void godown() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableDown[i];
		st[i] ^= tmp[i];
	}
	for (int i = n - 1; i > 0; --i) {
		st[i] = st[i - 1];
	}
	st[0].reset();
	for (int i = 0; i < n; ++i) {
		st[i] |= tmp[i];
	}
}

void goleft() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableLeft[i];
		st[i] ^= tmp[i];
	}
	for (int i = 0; i < n; ++i) {
		(st[i] >>= 1) |= tmp[i];
	}
}

void goright() {
	for (int i = 0; i < n; ++i) {
		tmp[i] = st[i] & stableRight[i];
		st[i] ^= tmp[i];
	}
	for (int i = 0; i < n; ++i) {
		(st[i] <<= 1) |= tmp[i];
	}
}

void delexit() {
	st[ex].reset(ey);
}

void initst() {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			st[i][j] = map[i][j] != '#';
		}
	}
	delexit();
}

void initUDLR() {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (map[i][j] == '#') {
				continue;
			}
			if (i == 0 || map[i - 1][j] == '#') {
				stableUp[i].set(j);
			}
			if (i == n - 1 || map[i + 1][j] == '#') {
				stableDown[i].set(j);
			}
			if (j == 0 || map[i][j - 1] == '#') {
				stableLeft[i].set(j);
			}
			if (j == m - 1 || map[i][j + 1] == '#') {
				stableRight[i].set(j);
			}
		}
	}
}

bool check() {
	for (int i = 0; i < n; ++i) {
		if (st[i].any()) {
			return false;
		}
	}
	return true;
}

void print() {
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			printf("%d", st[i][j] ? 1 : 0);
		}
		printf("\n");
	}
	printf("\n");
}

int main() {
	int k;
	scanf("%d%d%d", &n, &m, &k);
	int cntone = 0;
	for (int i = 0; i < n; ++i) {
		scanf("%s", map[i]);
		for (int j = 0; j < m; ++j) {
			cntone += map[i][j] != '#';
			if (map[i][j] == 'E') {
				ex = i;
				ey = j;
			}
		}
	}
	if (cntone == 1) {
		printf("0\n");
		return 0;
	}
	initst();
	initUDLR();
	scanf("%s", op);
	for (int i = 0; i < k; ++i) {
		if (op[i] == 'U') {
			goup();
		}
		if (op[i] == 'D') {
			godown();
		}
		if (op[i] == 'L') {
			goleft();
		}
		if (op[i] == 'R') {
			goright();
		}
		delexit();
		if (check()) {
			printf("%d\n", i + 1);
			return 0;
		}
	}
	printf("-1\n");
	return 0;
}

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
//#include <ext/hash_map>

using namespace std;
using namespace __gnu_cxx;

#define REP(i,n) for(int i = 0; i < int(n); ++i)
#define REPV(i, n) for (int i = (n) - 1; (int)i >= 0; --i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORV(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)

#define FE(i,t) for (typeof((t).begin())i=(t).begin();i!=(t).end();++i)
#define FEV(i,t) for (typeof((t).rbegin())i=(t).rbegin();i!=(t).rend();++i)

#define two(x) (1ULL << (x))
#define ALL(a) (a).begin(), (a).end()

#define pb push_back
#define ST first
#define ND second
#define MP(x,y) make_pair(x, y)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<string> vs;

template<class T> void checkmin(T &a, T b){if (b<a)a=b;}
template<class T> void checkmax(T &a, T b){if (b>a)a=b;}
template<class T> void out(T t[], int n){REP(i, n)cout<<t[i]<<" "; cout<<endl;}
template<class T> void out(vector<T> t, int n=-1){for (int i=0; i<(n==-1?t.size():n); ++i) cout<<t[i]<<" "; cout<<endl;}
inline int count_bit(int n){return (n == 0)?0:1+count_bit(n&(n-1));}
inline bool bit_set(int a, int b){return (a&two(b));}
inline int low_bit(int n){return (n^n-1)&n;}
inline int ctz(int n){return (n==0?-1:ctz(n>>1)+1);}
int toInt(string s){int a; istringstream(s)>>a; return a;}
string toStr(int a){ostringstream os; os<<a; return os.str();}

const int maxn=155;
const int maxm=4;
const int sz=64;
ull state[2][maxn*maxm];
ull *sa, *sb, *sc;
ull ans[maxn*maxm], mask[maxn*maxm], wall[maxn*maxm];
int n, m, om;
string sk;

int checkit(){ REP(i, n) REP(j, m) if (ans[i*4+j]!=sc[i*4+j]) return 0; return 1; }

int doit(){
    sa=state[0]; sb=state[1];
    memcpy(sb, mask, sizeof(mask));
    sc=sb;
    if (checkit()) return 0;

    int res=0;
    FE(it, sk){
        ++res;
        swap(sa, sb); sc=sb;
        memset(sb, 0, sizeof(ans));

        if (*it=='U'){
            REP(i, n-2) REP(j, m){
                int a=i*4+j+8, b=a-4;
                sb[b]|=sa[a]&mask[b];
                sb[a]|=sa[a]&wall[b];
            }
        }else if (*it=='D'){
            REP(i, n-2) REP(j, m){
                int a=i*4+j+4, b=a+4;
                sb[b]|=sa[a]&mask[b];
                sb[a]|=sa[a]&wall[b];
            }
        }else if (*it=='R'){
            REP(i, n-2) REP(j, m){
                int a=i*4+j+4, b=a+1;
                sb[a]|=(sa[a]<<1)&mask[a];
                sb[a]|=(wall[a]>>1|wall[b]<<63)&sa[a];
                sb[b]|=mask[b]&(sa[a]>>63);
            }
        }else if (*it=='L'){
            REP(i, n-2) REP(j, m){
                int a=i*4+j+4, b=a-1;
                sb[a]|=(sa[a]>>1)&mask[a];
                sb[a]|=(wall[a]<<1|wall[b]>>63)&sa[a];
                sb[b]|=mask[b]&(sa[a]<<63);
            }

        }

        //printf("done %c\n", *it);
        //REP(i, n){ REP(j, om) printf("%Lu", sb[i*4+j/sz]>>j%sz&1); printf("\n"); }
        //printf("\n\n");
        if (checkit()) return res;
    }
    return -1;
}

int main(){
    cin>>n>>m;
    int fuu; cin>>fuu;
    getchar();

    memset(mask, 0, sizeof(mask));
    memset(ans, 0, sizeof(ans));
    memset(wall, 0xff, sizeof(wall));
    REP(i, n){
        string s; cin>>s;
        REP(j, m){
            mask[i*4+j/sz]|=two(j%sz)*(s[j]!='#');
            wall[i*4+j/sz]^=two(j%sz)*(s[j]!='#');
            if (s[j]=='E') ans[i*4+j/sz]=two(j%sz);
        }
    }
    om=m;
    m=(m-1)/sz+1;
    cin>>sk;
    int res=doit();
    printf("%d\n", res);

    return 0;

}

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=250010;
const int inf=-0x3f3f3f3f;
inline int read(){
	int f=0,x=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[10050];int n,m,k,flag;
int work(){
	ch.reset();bl.reset();cn.reset();e.reset();flag=0;
	for(int i=0;i<n;++i){
		scanf("%s",s1);
		for(int j=0;j<m;++j){
			if(s1[j]=='#')bl[i*m+j]=1;
			else {
				cn[i*m+j]=1;
				if(s1[j]=='E')e[i*m+j]=1;
			}
		}
	}
	scanf("%s",s2);
	ch=cn;
	for(int i=0;i<k;++i){
		if(ch==e)return i;
		if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
		if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
		if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
		if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
	}
	if(ch==e)return k;
	return -1;
}
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	while(~scanf("%d %d %d",&n,&m,&k)){
		write(work());puts("");
	}
	return 0;
}

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=250010;
const int inf=-0x3f3f3f3f;
inline int read(){
	int f=0,x=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[10050];int n,m,k,flag;
int work(){
	ch.reset();bl.reset();cn.reset();e.reset();flag=0;
		for(int i=1;i<=n;++i){
			scanf("%s",s1+1);
			for(int j=1;j<=m;++j){
				if(s1[j]=='#')bl[(i-1)*m+j]=1;
				else if(s1[j]!='#')cn[(i-1)*m+j]=1;
				if(s1[j]=='E')e[(i-1)*m+j]=1;
			}
		}
		scanf("%s",s2+1);
		ch=cn;
		for(int i=1;i<=k;++i){
			if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
			if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
			if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
			if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
			if(ch==e){
			 write(i);puts("");flag=1;break;
			}
		}
		if(!flag){
			write(-1);puts("");
		}

}
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	n=read();m=read();k=read();
	ch.reset();bl.reset();cn.reset();e.reset();flag=0;
	for(int i=1;i<=n;++i){
		scanf("%s",s1+1);
		for(int j=1;j<=m;++j){
			if(s1[j]=='#')bl[(i-1)*m+j]=1;
			else if(s1[j]!='#')cn[(i-1)*m+j]=1;
			if(s1[j]=='E')e[(i-1)*m+j]=1;
		}
	}
	scanf("%s",s2+1);
	ch=cn;
	for(int i=1;i<=k;++i){
		if(ch==e){
		 write(i-1);puts("");flag=1;break;
		}
		if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
		if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
		if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
		if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
	}
	if(ch==e){
		write(k);puts("");flag=1;
	}
	if(!flag) {
		write(-1);puts("");
	}
	return 0;
}

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=250010;
const int inf=-0x3f3f3f3f;
inline int read(){
	int f=0,x=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[10050];int n,m,k,flag;
int work(){
	ch.reset();bl.reset();cn.reset();e.reset();flag=0;
		for(int i=1;i<=n;++i){
			scanf("%s",s1+1);
			for(int j=1;j<=m;++j){
				if(s1[j]=='#')bl[(i-1)*m+j]=1;
				else if(s1[j]!='#')cn[(i-1)*m+j]=1;
				if(s1[j]=='E')e[(i-1)*m+j]=1;
			}
		}
		scanf("%s",s2+1);
		ch=cn;
		for(int i=1;i<=k;++i){
			if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
			if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
			if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
			if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
			if(ch==e){
			 write(i);puts("");flag=1;break;
			}
		}
		if(!flag){
			write(-1);puts("");
		}

}
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	n=read();m=read();k=read();
	ch.reset();bl.reset();cn.reset();e.reset();flag=0;
	for(int i=1;i<=n;++i){
		scanf("%s",s1+1);
		for(int j=1;j<=m;++j){
			if(s1[j]=='#')bl[(i-1)*m+j]=1;
			else if(s1[j]!='#')cn[(i-1)*m+j]=1;
			if(s1[j]=='E')e[(i-1)*m+j]=1;
		}
	}
	scanf("%s",s2+1);
	ch=cn;
	for(int i=1;i<=k;++i){
		if(ch==e){
		 write(i-1);puts("");flag=1;
		}
		if(s2[i]=='U')ch=(((ch>>m)&cn)|((bl<<m)&ch));
		if(s2[i]=='D')ch=(((ch<<m)&cn)|((bl>>m)&ch));
		if(s2[i]=='L')ch=(((ch>>1)&cn)|((bl<<1)&ch));
		if(s2[i]=='R')ch=(((ch<<1)&cn)|((bl>>1)&ch));
	}
	if(ch==e){
		write(k);puts("");flag=1;
	}
	if(!flag) {
		write(-1);puts("");
	}
	return 0;
}

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<bitset>
#define ll long long
using namespace std;
const int maxn=250010;
const int inf=-0x3f3f3f3f;
inline int read(){
	int f=0,x=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
inline void write(int x){
    if(x<0){putchar('-');x=-x;}
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
bitset<maxn>ch,bl,cn,e;
char s1[155],s2[10050];int n,m,k,flag;
int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	while(~scanf("%d%d%d",&n,&m,&k)){
		ch.reset();bl.reset();cn.reset();e.reset();flag=0;
		for(int i = 0; i < n; i++)
	    {
	        scanf("%s", s1);
	        for(int j = 0; j < m; j++)
	        {
	            (s1[j]=='#'?bl:cn).set(i*m+j);
	            (s1[j]=='E'?e.set(i*m+j):0);
	        }
	    }
		scanf("%s", s2);
	    ch = cn;
	    for(int i = 0; i < k; i++)
	    {
	        if(ch==e){
	        	write(i);puts("");flag=1;break;
	        }
	        if(s2[i]=='U') ch = ((ch>>m)&cn) | (ch&(bl<<m));
	        if(s2[i]=='L') ch = ((ch>>1)&cn) | (ch&(bl<<1));
	        if(s2[i]=='D') ch = ((ch<<m)&cn) | (ch&(bl>>m));
	        if(s2[i]=='R') ch = ((ch<<1)&cn) | (ch&(bl>>1));
	    }
		if(!flag){
			write(-1);puts("");
		}

	}
	return 0;
}