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 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: rot0 -= 1 else: rot0 += 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 == rot1 == 0: ans += 1 print(ans)