Java BFS
\nclass Solution {\n    public int cutOffTree(List<List<Integer>> forest) {\n        if (forest == null || forest.size() == 0 || forest.get(0).size() == 0) {\n 
```\nclass Solution {\n    public int cutOffTree(List<List<Integer>> forest) {\n        if (forest == null || forest.size() == 0 || forest.get(0).size() == 0) {\n            return -1;\n        }\n    \n        int[][] matrix = new int[forest.size()][forest.get(0).size()];\n        List<Integer> trees = new ArrayList<>();\n        \n        for (int i = 0; i < forest.size(); i++) {\n            List<Integer> list = forest.get(i);\n            for (int j = 0; j < list.size(); j++) {\n                matrix[i][j] = list.get(j);\n                if (matrix[i][j] > 1) {\n                    trees.add(matrix[i][j]);\n                }\n            }\n        }\n        \n        Collections.sort(trees);\n        int row = 0, col = 0;\n        int steps = 0;\n        \n        for (Integer tree: trees) {\n            boolean[][] visited = new boolean[matrix.length][matrix[0].length];\n            int[] result = cut(matrix, visited, row, col, tree);\n            \n            if (result[0] == 1) {\n                steps += result[1];\n                row = result[2];\n                col = result[3];\n            } else {\n                return -1;\n            }\n        }\n        return steps;\n    }\n    \n    private int[] cut(int[][] matrix, boolean[][] visited, int row, int col, int tree) {\n        if (matrix[row][col] == tree) {\n            matrix[row][col] = 1;\n            return new int[] {1, 0, row, col};\n        }\n        \n        Queue<String> q = new LinkedList<>();\n        q.add(row + "_" + col);\n        visited[row][col] = true;\n        q.add(null);\n        \n        int steps = 0;\n        \n        while (!q.isEmpty()) {\n            String current = q.remove();\n            if (current == null) {\n                if (!q.isEmpty()) {\n                    q.add(null);\n                }\n                steps++;\n                continue;\n            }\n            \n            String[] parts = current.split("_");\n            row = Integer.parseInt(parts[0]);\n            col = Integer.parseInt(parts[1]);\n            if (matrix[row][col] == tree) {\n                return new int[] {1, steps, row, col};\n            }\n            \n            if (isValidCell(matrix, visited, row - 1, col)) {\n                q.add((row - 1) + "_" + (col));\n                visited[row - 1][col] = true;\n            }\n            if (isValidCell(matrix, visited, row + 1, col)) {\n                q.add((row + 1) + "_" + (col));\n                visited[row + 1][col] = true;\n            }\n            if (isValidCell(matrix, visited, row, col - 1)) {\n                q.add((row) + "_" + (col - 1));\n                visited[row][col - 1] = true;\n            }\n            if (isValidCell(matrix, visited, row, col + 1)) {\n                q.add((row) + "_" + (col + 1));\n                visited[row][col + 1] = true;\n            }\n        }\n        return new int[] {0, 0, row, col};\n        \n    }\n    \n    private boolean isValidCell(int[][] matrix, boolean[][] visited, int row, int col) {\n        return row >= 0 && col >= 0 && row < matrix.length && col < matrix[0].length && matrix[row][col] != 0 && !visited[row][col];\n    }\n}\n```
[]