// Arup Guha
// 11/6/2016
// Solution to SER D1/D2 Problem: Buggy Robot

import java.util.*;

public class buggy {

	// Moving directions.
	final public static int[] DX = {-1,0,0,1};
	final public static int[] DY = {0,-1,1,0};
	final public static String DIR = "ULRD";

	// Stores input grid and directions.
	public static int r;
	public static int c;
	public static char[][] grid;
	public static String directions;
	public static int n;

	public static void main(String[] args) {

		// Read in grid and directions.
		Scanner stdin = new Scanner(System.in);
		r = stdin.nextInt();
		c = stdin.nextInt();
		grid = new char[r][];
		for (int i=0; i<r; i++)
			grid[i] = stdin.next().toCharArray();
		directions = stdin.next();
		n = directions.length();

		// Solve it.
		System.out.println(bfs());
	}

	public static int bfs() {

		// Find start and end spot.
		int robot = find('R');
		int exit = find('E');

		// State: (loc,k) store min changes.
		int[][] best = new int[r*c][n+1];
		for (int i=0; i<r*c; i++) Arrays.fill(best[i], -1);

		// Set up priority queue for search.
		PriorityQueue<state> pq = new PriorityQueue<state>();
		pq.offer(new state(robot, 0, 0));

		// Run best first search.
		while (pq.size() > 0) {

			// Get next item,
			state cur = pq.poll();

			// We got out, yeah!
			if (cur.loc == exit) return cur.moves;

			// Add all possible moves to this arraylist.
			ArrayList<state> list = new ArrayList<state>();

			// delete a move, cost 1.
			if (cur.index < n) list.add(new state(cur.loc, cur.index+1, cur.moves+1));

			// insert one of four moves, cost 1.
			for (int i=0; i<DX.length; i++) {
				int next = apply(cur.loc, i);
				if (next != cur.loc)
					list.add(new state(next, cur.index, cur.moves+1));
			}

			// take this robot move, cost 0.
			if (cur.index < n) {
				int next = apply(cur.loc, getIndex(directions.charAt(cur.index)));
				list.add(new state(next, cur.index+1, cur.moves));
			}

			// Enqueue all items that might be worth building off of.
			for (state s: list) {
				if (best[s.loc][s.index] == -1 || best[s.loc][s.index] > s.moves) {
					 best[s.loc][s.index] = s.moves;
					 pq.offer(s);
				}
			}
		}

		// Should never get here...
		return 1000000000;
	}

	public static int apply(int curLoc, int moveI) {

		// Where we would go.
		int x = curLoc/c + DX[moveI];
		int y = curLoc%c + DY[moveI];

		// No move occurs.
		if (!inbounds(x,y)) return curLoc;
		if (grid[x][y] == '#') return curLoc;

		// New locaiton.
		return c*x + y;
	}

	// Returns index of character c in DIR array.
	public static int getIndex(char ch) {
		for (int i=0; i<DIR.length(); i++)
			if (DIR.charAt(i) == ch)
				return i;
		return 0; // Should never get here.
	}

	// Returns location of ch.
	public static int find(char ch) {
		for (int i=0; i<r; i++)
			for (int j=0; j<c; j++)
				if (grid[i][j] == ch)
					return c*i + j;
		return -1;
	}

	public static boolean inbounds(int x, int y) {
		return x >= 0 && x < r && y >= 0 && y < c;
	}
}

class state implements Comparable<state> {

	public int loc;
	public int index;
	public int moves;

	public state(int myloc, int myindex, int mymoves) {
		loc = myloc;
		index = myindex;
		moves = mymoves;
	}

	public int compareTo(state other) {
		return this.moves - other.moves;
	}
}