Java - Priority Queue + BFS
\npublic int shortestPathBfs(int[] start, int[] end, List<List<Integer>> forest) {\n        int n = forest.size();\n        int m = forest.get(0).size();\n     
```\npublic int shortestPathBfs(int[] start, int[] end, List<List<Integer>> forest) {\n        int n = forest.size();\n        int m = forest.get(0).size();\n        boolean[][] visited = new boolean[n][m];\n        for(int i = 0; i<n; i++) {\n            for(int j = 0; j<m; j++) {\n                visited[i][j] = false;\n            }\n        }\n\n        int[][] matrixTraversal = {{0,-1}, {-1, 0}, {0, 1}, {1, 0}};\n        Queue<int[]> bfs = new LinkedList<>();\n        bfs.add(new int[]{start[0], start[1], 0});\n        visited[start[0]][start[1]] = true;\n\n        while(!bfs.isEmpty()) {\n            int[] tree = bfs.poll();\n            if(tree[0] == end[0] && tree[1] == end[1]) {\n                return tree[2];\n            }\n\n            /*\n                marking visited as true in loop only as we will be reaching the node in the shortest distance in first visit itself.\n             */\n            for(int i = 0; i<4; i++) {\n                int x = tree[0] + matrixTraversal[i][0];\n                int y = tree[1] + matrixTraversal[i][1];\n                if(x >=0 && y >=0 && x<n && y<m && forest.get(x).get(y)!=0 && !visited[x][y]) {\n                    bfs.add(new int[]{x, y, tree[2] + 1});\n                    visited[x][y] = true;\n                }\n            }\n        }\n\n        return -1;\n    }\n\n\n\n    public int cutOffTree(List<List<Integer>> forest) {\n        int n = forest.size();\n        int m = forest.get(0).size();\n        PriorityQueue<int[]> trees = new PriorityQueue<>((a,b) -> {\n            if(a[2] > b[2]) {\n                return 1;\n            }\n            return -1;\n        });\n\n        for(int i = 0; i<n; i++) {\n            for(int j = 0; j<m; j++) {\n                if(forest.get(i).get(j) > 1) {\n                    trees.add(new int[]{i, j, forest.get(i).get(j)});\n                }\n            }\n        }\n\n        int[] start = new int[]{0, 0, forest.get(0).get(0)};\n        int totalSteps = 0;\n        while(!trees.isEmpty()) {\n            int[] end = trees.poll();\n            int steps = shortestPathBfs(start, end, forest);\n            if(steps == -1) {\n                return -1;\n            }\n            totalSteps += steps;\n\n            start[0] = end[0];\n            start[1] = end[1];\n        }\n\n        return totalSteps;\n    }\n```
['Breadth-First Search', 'Heap (Priority Queue)', 'Java']