n, m = map(int, input().split()) S = [input() for _ in range(n)] t = "ULDR" dy = [1, 0, -1, 0] dx = [0, -1, 0 , 1] ans = 0 for i in range(n): s = S[i] x, y = 0, 0 rot0 = 0 rot1 = 0 stuck = [-1] pr0 = pr1 = 0 for j in range(m): ind = t.index(s[j]) nx, ny = x+dx[ind], y+dy[ind] if (x, nx) == (0, 1): if 1 <= y: rot0 += 1 else: rot0 -= 1 if (x, nx) == (1, 0): if 1 <= y: rot0 -= 1 else: rot0 += 1 if (x, nx) == (-1, 0): if 1 <= y: rot1 += 1 else: rot1 -= 1 if (x, nx) == (0, -1): if 1 <= y: rot1 -= 1 else: rot1 += 1 if (y, ny) == (0, 1): if x <= -1: rot0 += 1 rot1 += 1 elif x <= 0: rot0 += 1 rot1 -= 1 else: rot0 -= 1 rot1 -= 1 if (y, ny) == (1, 0): if x <= -1: rot0 -= 1 rot1 -= 1 elif x <= 0: rot0 -= 1 rot1 += 1 else: rot0 += 1 rot1 += 1 x, y = nx, ny if rot0 % 4 == 0 and pr0 != rot0: if pr0 < rot0: if stuck[-1] == 0: stuck.pop() else: stuck.append(1) else: if stuck[-1] == 1: stuck.pop() else: stuck.append(0) pr0 = rot0 if rot1 % 4 == 0 and pr1 != rot1: if pr1 < rot1: if stuck[-1] == 2: stuck.pop() else: stuck.append(3) else: if stuck[-1] == 3: stuck.pop() else: stuck.append(2) pr1 = rot1 if len(stuck) == 1: ans += 1 print(ans)