Python BFS working solution
\n    def cutOffTree(self, forest: List[List[int]]) -> int:\n        rows = len(forest)\n        cols = len(forest[0])\n        def is_valid_move(row, col, matr
```\n    def cutOffTree(self, forest: List[List[int]]) -> int:\n        rows = len(forest)\n        cols = len(forest[0])\n        def is_valid_move(row, col, matrix, visited):\n            nonlocal rows, cols\n            if new_row >= rows or new_row < 0 or new_col >= cols or new_col < 0: return False\n            if (new_row, new_col) in visited: return False\n            if matrix[row][col] == 0: return False\n            return True\n        trees = []\n        for r in range(rows):\n            for c in range(cols):\n                if forest[r][c] > 1: trees.append(forest[r][c])\n        trees.sort()\n        index = 0\n        steps = 0\n        start_node = (0,0)\n        found_flag = True\n        while index < len(trees):\n            start_row, start_col = start_node\n            if not found_flag: break\n            found_flag = False\n            queue = deque([(start_row, start_col, 0)])\n            visited = set([(start_row, start_col)])\n            while queue:\n                # look for current_tree\n                r,c,s = queue.popleft()\n                if forest[r][c] == trees[index]:\n                    forest[r][c] = 1\n                    index += 1\n                    steps += s\n                    found_flag = True\n                    start_node = (r,c)\n                    break\n                for direction_row, direction_col in [(-1,0), (1,0), (0,1), (0,-1)]:\n                    new_row, new_col = direction_row + r, direction_col + c\n                    if is_valid_move(new_row, new_col, forest, visited):\n                        queue.append((new_row, new_col, s+1))\n                        visited.add((new_row, new_col))\n        return steps if index >= len(trees) else -1\n```
[]