n, m = map(int, input().split()) ans = 0 for _ in range(n): s = input() d = [] x = y = 0 for c in s: if c == 'L': match (x, y): case (0, 0): d.append(0) case (0, 1): d.append(1) case (0, 1): d.append(2) case (1, 1): d.append(3) x -= 1 elif c == 'R': x += 1 match (x, y): case (0, 0): d.append(0) case (0, 1): d.append(1) case (0, 1): d.append(2) case (1, 1): d.append(3) elif c == 'U': y += 1 else: y -= 1 if len(d) >= 2 and d[-1] == d[-2]: d.pop() d.pop() if len(d) == 0: ans += 1 print(ans)