#!/usr/bin/env python3

import sys
import re

MAX_RC = 99

dirs = [[-1,0], [1,0], [0,1], [0,-1]]
def dfs(i, j, R, C, G, v):
  v[i][j] = True
  for d in range(4):
    ni, nj = i + dirs[d][0], j + dirs[d][1]
    if ni < 0 or ni >= R or nj < 0 or nj >= C or v[ni][nj] or G[ni][nj] == '#': continue
    dfs(ni, nj, R, C, G, v) 
  
def valid(G):
  R, C = len(G), len(G[0])
  vu = [[False] * C for _ in range(R)]
  vd = [[False] * C for _ in range(R)]
  for i in range(R):
    for j in range(C):
      if G[i][j] == 'U': dfs(i, j, R, C, G, vu)
      if G[i][j] == 'D': dfs(i, j, R, C, G, vd)
  for i in range(R):
    for j in range(C):
      if G[i][j] != '#':
        if not vu[i][j] or not vd[i][j]: return False
  return True
  
line = sys.stdin.readline()
assert re.match('^[1-9][0-9]* [1-9][0-9]*\n$', line)
R, C = [int(x) for x in line.split(' ')]
G = [[''] * C for _ in range(R)]

count_U, count_D = 0, 0
for i in range(R):
  line = sys.stdin.readline()
  assert re.match('^[.#UD]{%d}\n$' % C, line)
  count_U += line.count('U')
  count_D += line.count('D')
  for j in range(C):
    G[i][j] = line[j]

assert count_U == 1 and count_D == 1
assert valid(G), 'invalid connectivity'

assert sys.stdin.readline() == ''

sys.exit(42)
