swift solution
\nClass Solution {\n    func minimumMoves(_ grid: [[Int]]) -> Int {\n        let n = grid.count\n        var dp = Array(repeating: Array(repeating: Array(repeat
```\nClass Solution {\n    func minimumMoves(_ grid: [[Int]]) -> Int {\n        let n = grid.count\n        var dp = Array(repeating: Array(repeating: Array(repeating: 0, count: 2), count: n), count: n)\n        \n        for i in 0..<n {\n            for j in 0..<n {\n                for k in 0..<2 {\n                    if i == 0 && j == 0 && k == 0 {\n                        dp[i][j][k] = 0\n                    } else if grid[i][j] == 1 {\n                        dp[i][j][k] = Int.max\n                    } else if k == 0 && (j + 1 >= n || grid[i][j + 1] == 1) {\n                        dp[i][j][k] = Int.max\n                    } else if k == 1 && (i + 1 >= n || grid[i + 1][j] == 1) {\n                        dp[i][j][k] = Int.max\n                    } else {\n                        let choice1 = i - 1 >= 0 ? dp[i - 1][j][k] : Int.max\n                        let choice2 = j - 1 >= 0 ? dp[i][j - 1][k] : Int.max\n                        let tempMinChoice = min(choice1, choice2)\n                        dp[i][j][k] = tempMinChoice == Int.max ? tempMinChoice : (tempMinChoice + 1)\n                    }\n                }\n                //rotate\n                if i + 1 < n && j + 1 < n && grid[i][j + 1] != 1 && grid[i + 1][j + 1] != 1 && dp[i][j][1] != Int.max && dp[i][j][0] > dp[i][j][1] + 1 {\n                    dp[i][j][0] = dp[i][j][1] + 1\n                }\n                if i + 1 < n && j + 1 < n && grid[i + 1][j] != 1 && grid[i + 1][j + 1] != 1 && dp[i][j][0] != Int.max && dp[i][j][1] > dp[i][j][0] + 1 {\n                    dp[i][j][1] = dp[i][j][0] + 1\n                }\n            }\n        }\n        return dp[n - 1][n - 2][0] == Int.max ? -1 : dp[n - 1][n - 2][0]\n    }\n}\n```
['Swift']