use std::collections::{HashSet, VecDeque}; fn main() { let (n, k) = { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse::().unwrap(), iter.next().unwrap().parse::().unwrap(), ) }; let mut ccc: Vec> = vec![]; for _ in 0..n { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); ccc.push(line.trim().chars().collect()); } let sss: Vec> = ccc .iter() .map(|cc| cc.iter().map(|&c| (c as u8 - b'0') as usize).collect()) .collect(); let set: HashSet> = sss.iter().cloned().collect(); let mut covered_set = HashSet::new(); let mut visited = HashSet::new(); let create_next_nodes = |cur_node: &Vec| { (0..k) .filter(|&i| cur_node[i] != 0) .map(|i| { let mut next_node = cur_node.clone(); next_node[i] -= 1; next_node }) .collect::>>() }; for ss in &sss { if visited.contains(ss) { continue; } let mut que = VecDeque::from(vec![ss.clone()]); visited.insert(ss.clone()); while let Some(cur_node) = que.pop_front() { for next_node in create_next_nodes(&cur_node) { if set.contains(&next_node) { covered_set.insert(next_node.clone()); } if !visited.contains(&next_node) { que.push_back(next_node.clone()); visited.insert(next_node); } } } } println!("{}", covered_set.len()); }