use proconio::{input, marker::Chars}; fn main() { input! { n: usize, m: usize, curves: [Chars; n], } let mut ans = 0; for curve in curves { let mut y = 0; let mut x = 0; let mut cross1 = 0; let mut cross2 = 0; for c in curve { let (ny, nx) = match c { 'U' => (y + 1, x), 'D' => (y - 1, x), 'L' => (y, x - 1), 'R' => (y, x + 1), _ => unreachable!() }; if x >= 1 && (y, ny) == (0, 1) { cross1 += 1; } if x >= 1 && (y, ny) == (1, 0) { cross1 -= 1; } if x >= 0 && (y, ny) == (0, 1) { cross2 += 1; } if x >= 0 && (y, ny) == (1, 0) { cross2 -= 1; } x = nx; y = ny; } if cross1 == 0 && cross2 == 0 { ans += 1; } } println!("{ans}"); }