Just a runnable solution
Code\n\nimpl Solution {\n    pub fn cut_off_tree(forest: Vec<Vec<i32>>) -> i32 {\n        let mut trees = forest\n            .iter()\n            .enumerate()\
# Code\n```\nimpl Solution {\n    pub fn cut_off_tree(forest: Vec<Vec<i32>>) -> i32 {\n        let mut trees = forest\n            .iter()\n            .enumerate()\n            .flat_map(|(i, row)| {\n                row.iter()\n                    .enumerate()\n                    .filter_map(move |(j, &height)| if height > 1 { Some((height, i, j)) } else { None })\n            })\n            .collect::<Vec<_>>();\n        trees.sort_unstable();\n\n        let mut steps = 0;\n        let mut start = (0, 0);\n        for (_, i, j) in trees {\n            let step = Self::bfs(&forest, start, (i, j));\n            if step < 0 {\n                return -1;\n            }\n            steps += step;\n            start = (i, j);\n        }\n        steps\n    }\n\n    fn bfs(forest: &[Vec<i32>], start: (usize, usize), end: (usize, usize)) -> i32 {\n        let mut queue = std::collections::VecDeque::new();\n        queue.push_back((start, 0));\n        let mut visited = std::collections::HashSet::new();\n        visited.insert(start);\n\n        while let Some(((i, j), step)) = queue.pop_front() {\n            if (i, j) == end {\n                return step;\n            }\n\n            for (di, dj) in &[(0, 1), (0, -1), (1, 0), (-1, 0)] {\n                let (ni, nj) = (i as i32 + di, j as i32 + dj);\n                if ni >= 0\n                    && ni < forest.len() as i32\n                    && nj >= 0\n                    && nj < forest[0].len() as i32\n                    && forest[ni as usize][nj as usize] > 0\n                    && visited.insert((ni as usize, nj as usize))\n                {\n                    queue.push_back(((ni as usize, nj as usize), step + 1));\n                }\n            }\n        }\n        -1\n    }\n}\n```
['Rust']