import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Queue;
import java.util.LinkedList;
import java.util.StringTokenizer;

/* A runner captured in a maze needs your help. Your goal is to find the shortest
 * way through the maze from the runner's initial position to the exit.
 *
 * The maze is build out of walls ('#'), corridors (' '), and exit ('E').
 * You may assume the maze is enclosed by walls (including 'E').
 *
 * This is an alternative solution, which actively uses the 2d array
 * representation of the map during the search algorithm. The map during the
 * search is used to mark the places that has been already visited and at the
 * end it (implicitly) contains the shortest path through the maze.s
 */
public class mazerunner2
{
    /* The directions in which the maze runner can move. */
    public static final int[][] DIRECTIONS = new int[][]{{-1, 0},{1, 0},{0, -1},{0, 1}};

    public static final int PATH     = -4;
    public static final int WALL     = -3;
    public static final int EXIT     = -2;
    public static final int CORRIDOR = -1;
    public static final int RUNNER   = 0;

    /* The representation of a state of the world. */
    public static class QueueState
    {
        public int x; // Runner's x position in the world.
        public int y; // Runner's y position in the world.

        /* Returns a new state which was created from this state
         * by moving the runner in the specified direction.
         */
        public QueueState move(int[] direction)
        {
            QueueState copy = new QueueState();

            copy.x = this.x + direction[0];
            copy.y = this.y + direction[1];

            return copy;
        }
    }

    /* The queue for bread-first search. */
    public static Queue<QueueState> queue = new LinkedList<QueueState>();

    /* Stores the map read from the input. */
    public static int[][] map = null;

    /* Read the map from the standard input and return the state associated
     * with the initial position of the heretic.
     */
    public static QueueState readMap(BufferedReader reader) throws IOException
    {
        StringTokenizer tokenizer = new StringTokenizer(reader.readLine());

        int width = Integer.valueOf(tokenizer.nextToken());
        int height = Integer.valueOf(tokenizer.nextToken());

        map = new int[height][width];

        QueueState start = new QueueState();

        int iRow = 0;

        String line = null;

        while ((line = reader.readLine()) != null)
        {
            int iCol = 0;
                        
            for (int i = 0; i < line.length(); i++)
            {
                switch (line.charAt(i))
                {
                case '#':
                    map[iRow][iCol] = WALL;
                    break;

                case 'E':
                    map[iRow][iCol] = EXIT;
                    break;

                case 'R':
                    map[iRow][iCol] = RUNNER;
                    start.x = iCol;
                    start.y = iRow;
                    break;

                default:
                    map[iRow][iCol] = CORRIDOR;
                    break;
                }

                ++iCol;
            }

            ++iRow;
        }

        return start;
    }

    public static void printMap(char reachable)
    {
        for (int iRow = 0; iRow < map.length; iRow++)
        {
            for (int iCol = 0; iCol < map[0].length; iCol++)
            {
                switch(map[iRow][iCol])
                {
                case WALL:     System.out.print('#'); break;
                case EXIT:     System.out.print('E'); break;
                case RUNNER:   System.out.print('R'); break;
                case CORRIDOR: System.out.print(' '); break;
                case PATH:     System.out.print('.'); break;
                default:       System.out.print(reachable); break;
                }
                
            }
            System.out.println();
        }
    }

    public static void main(String[] arguments) throws IOException
    {
        /* Read the map from the standard input and get the initial state. */
        QueueState state = readMap(new BufferedReader(new InputStreamReader(System.in)));

        /* Add the initial state into the queue. */
        queue.add(state);

        boolean foundExit = false;

        /* Until the queue get emptied or a solution has been found. */
        while (!foundExit && (state = queue.poll()) != null)
        {
            for (int[] direction : DIRECTIONS)
            {
                QueueState nextState = state.move(direction);

                if (map[nextState.y][nextState.x] == CORRIDOR)
                {
                    map[nextState.y][nextState.x] = map[state.y][state.x] + 1;
                    queue.add(nextState);
                }
                else if (map[nextState.y][nextState.x] == EXIT)
                {
                    foundExit = true;
                    break;
                }
            }
        }

        /* Print out the instructuons or prompt there is no way out. */
        if (foundExit)
        {
            while (map[state.y][state.x] != RUNNER)
            {
                for (int[] direction : DIRECTIONS)
                {
                    QueueState prevState = state.move(direction);

                    if (map[prevState.y][prevState.x] + 1 == map[state.y][state.x])
                    {
                        map[state.y][state.x] = PATH;
                        state = prevState;
                        break;
                    }
                }
            }

            printMap(' ');
        }
        else
        {
            printMap('.');
            System.out.println("There is no way out, arrrgh!");
        }
    }
}