use std::fmt::format; fn main() { let mut nm = String::new(); std::io::stdin().read_line(&mut nm).ok(); let nm: Vec = nm.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nm[0]; let m = nm[1]; let words = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp = temp.trim(); temp.chars().collect::>() }) .collect::>(); let a = &words[0]; let b = &words[1]; let start = (0..m).filter(|&i| a[i] != b[i]).nth(0).unwrap(); let end = (0..m).rev().filter(|&i| a[i] != b[i]).nth(0).unwrap(); let t1 = format!("{}{}{}{}" , (0..start).map(|i| a[i].to_string()).collect::>().join("") , (start..=end).map(|i| a[i].to_string()).collect::>().join("") , b[end] , (end+1..m).map(|i| a[i].to_string()).collect::>().join("")) .chars().collect::>(); let t2 = format!("{}{}{}{}" , (0..start).map(|i| a[i].to_string()).collect::>().join("") , (start..=end).map(|i| b[i].to_string()).collect::>().join("") , a[end] , (end+1..m).map(|i| a[i].to_string()).collect::>().join("")) .chars().collect::>(); let mut result = 0usize; let word_ok = |i: usize, t: &Vec| { let start = (0..m+1).filter(|&j| j == m || words[i][j] != t[j]).nth(0).unwrap(); (start..m).map(|j| words[i][j] == t[j+1]).fold(true, |x, y| x && y) }; let temp = (0..n).filter(|&i| word_ok(i, &t1)).count() == n; if temp { result += 1; } let temp = (0..n).filter(|&i| word_ok(i, &t2)).count() == n; if temp { result += 1; } println!("{}", result); }