Java Simple BFS Solution
\nclass Solution {\n    int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n    int rows;\n    int cols;\n    public int cutOffTree(List<List<Integer>> forest) 
```\nclass Solution {\n    int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};\n    int rows;\n    int cols;\n    public int cutOffTree(List<List<Integer>> forest) {\n        rows = forest.size();\n        cols = forest.get(0).size();\n        int[][] matrix = new int[rows][cols];\n        TreeMap<Integer, int[]> map = new TreeMap<>();\n        for (int r = 0; r < rows; ++r) {\n            for (int c = 0; c < cols; ++c) {\n                matrix[r][c] = forest.get(r).get(c);\n                if (matrix[r][c] > 1) {\n                    map.put(matrix[r][c], new int[]{r, c});\n                }\n            }\n        }\n        int x = 0, y = 0, res = 0;\n        for (int t : map.keySet()) {\n            int[] pos = map.get(t);\n            boolean[][] visited = new boolean[rows][cols];\n            int step = bfs(x, y, pos[0], pos[1], matrix, visited);\n            if (step == -1) {\n                return -1;\n            }\n            res += step;\n            matrix[x][y] = 1;\n            x = pos[0];\n            y = pos[1];\n        }\n        return res;\n    }\n    \n    private int bfs(int r, int c, int x, int y, int[][] matrix, boolean[][] visited) {\n        Queue<int[]> queue = new ArrayDeque<>();\n        queue.offer(new int[]{r, c});\n        visited[r][c] = true;\n        int step = 0;\n        while (!queue.isEmpty()) {\n            int size = queue.size();\n            for (int i = 0; i < size; ++i) {\n                int[] cur = queue.poll();\n                if (cur[0] == x && cur[1] == y) {\n                    return step;\n                }\n                for (int[] dir : dirs) {\n                    int nr = dir[0] + cur[0];\n                    int nc = dir[1] + cur[1];\n                    if (0 <= nr && nr < rows && 0 <= nc && nc < cols && matrix[nr][nc] > 0 && !visited[nr][nc]) {\n                        visited[nr][nc] = true;\n                        queue.offer(new int[]{nr, nc});\n                    }\n                }\n            }\n            step++;\n        }\n        return -1;\n    }\n}\n```
['Java']