#!/usr/bin/python3 from math import inf from collections import deque n, m = map(int, input().split()) field = [] for y in range(n): field.append(list(input().strip())) if "D" in field[-1]: start = (field[-1].index("D"), y) if "U" in field[-1]: end = (field[-1].index("U"), y) if "P" in field[-1]: xc, yc = (field[-1].index("P"), y) # Zjistíme si kdy bude políčko zabrané karnevalem carnival = [[inf]*m for i in range(n)] direction = (-1, 0) # Po 4*n*m (princip holubníku) krocích karneval navštíví stejné políčko # ve stejném směru, takže bude chodit dál dokola for time in range(4*n*m): if not (0 <= xc < m and 0 <= yc < n): break # Karneval vylezl z plochy carnival[yc][xc] = min(time, carnival[yc][xc]) for i in range(4): ny, nx = yc + direction[0], xc + direction[1] if field[ny][nx] in "_P": break direction = (direction[1], -direction[0]) if i == 4: break # Karneval se zasekl xc, yc = nx, ny # Uděláme bfs queue = deque([(start, None, 0)]) visited = [[None]*m for i in range(n)] while len(queue): (x, y), last, time = queue.popleft() if field[y][x] == "X" or visited[y][x] or carnival[y][x] <= time: continue visited[y][x] = last direction = (1, 0) for i in range(4): ny, nx = y + direction[0], x + direction[1] if 0 <= nx < m and 0 <= ny < n: queue.append(((nx, ny), (x, y), time+1)) direction = (direction[1], -direction[0]) # Zrekonstruujeme cestu directions = { (0, +1): 'D', (0, -1): 'N', (+1, 0): 'P', (-1, 0): 'L', } result = "" last = end while last != start: this = visited[last[1]][last[0]] result += directions[(last[0] - this[0], last[1] - this[1])] last = this print(result[::-1])