Swift solution - BFS
\nclass Solution {\n    \n    let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n    \n    func cutOffTree(_ forest: [[Int]]) -> Int {\n        if forest.coun
```\nclass Solution {\n    \n    let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n    \n    func cutOffTree(_ forest: [[Int]]) -> Int {\n        if forest.count == 0 || forest[0].count == 0 {\n            return -1\n        }\n        \n        let m = forest.count\n        let n = forest[0].count\n        var result = 0\n        var startX = 0\n        var startY = 0\n        var heights = [(Int, (Int, Int))]()\n        \n        for i in 0..<m {\n            for j in 0..<n {\n                if forest[i][j] > 1 {\n                    heights .append((forest[i][j], (i, j)))\n                }\n            }\n        }\n        heights.sort { (height1: (Int, (Int, Int)), height2: (Int, (Int, Int))) -> Bool in\n            return height1.0 < height2.0\n        }\n        \n        for i in 0..<heights.count {\n            let endX = heights[i].1.0\n            let endY = heights[i].1.1\n            let steps = BFS(forest, m, n, startX, startY, endX, endY)\n            if steps == -1 {\n                return -1\n            }\n            result += steps\n            startX = endX\n            startY = endY\n        }\n        \n        return result\n    }\n    \n    private func BFS(_ forest: [[Int]], _ m: Int, _ n: Int, _ startX: Int, _ startY: Int, _ endX: Int, _ endY: Int) -> Int {\n        if startX == endX && startY == endY {\n            return 0\n        }\n        \n        var steps = 0\n        var queue = [(Int, Int)]()\n        var visited = [[Bool]](repeatElement([Bool](repeatElement(false, count: n)), count: m))\n        \n        queue.append((startX, startY))\n        visited[startX][startY] = true\n        \n        while !queue.isEmpty {\n            let count = queue.count\n            steps += 1\n            for _ in 0..<count {\n                let (x, y) = queue.removeFirst()\n                for i in 0..<directions.count {\n                    let nextX = x + directions[i].0\n                    let nextY = y + directions[i].1\n                    if nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && forest[nextX][nextY] > 0 && !visited[nextX][nextY] {\n                        if nextX == endX && nextY == endY {\n                            return steps\n                        }\n                        visited[nextX][nextY] = true\n                        queue.append((nextX, nextY))\n                    }\n                }\n            }\n        }\n        \n        return -1\n    }\n    \n}\n```
['Breadth-First Search', 'Swift']