a few solutions
Perform BFS from start.\n\n---\n\nKotlin\n\nclass Solution {\n    fun nearestExit(A: Array<CharArray>, start: IntArray): Int {\n        var depth = 0\n        v
Perform BFS from `start`.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    fun nearestExit(A: Array<CharArray>, start: IntArray): Int {\n        var depth = 0\n        var (M, N) = listOf(A.size, A[0].size)\n        var isExit = { i: Int, j: Int -> i == 0 || i == M - 1 || j == 0 || j == N - 1 }\n        var q: Queue<Pair<Int,Int>> = LinkedList<Pair<Int, Int>>(listOf(Pair(start[0], start[1])))\n        var seen = mutableSetOf<String>("${start[0]},${start[1]}")\n        while (0 < q.size) {\n            var k = q.size\n            while (0 < k--) {\n                var (i, j) = q.poll()\n                if (isExit(i, j) && 0 < depth)\n                    return depth\n                for ((u, v) in listOf(Pair(i - 1, j), Pair(i, j + 1), Pair(i + 1, j), Pair(i, j - 1))) {\n                    if (0 <= u && u < M && 0 <= v && v < N && A[u][v] == \'.\' && !seen.contains("$u,$v")) {\n                        q.add(Pair(u, v)); seen.add("$u,$v")\n                    }\n                }\n            }\n            ++depth\n        }\n        return -1\n    }\n}\n```\n\n*Javascript*\n```\nlet nearestExit = (A, start, depth = 0) => {\n    let [M, N] = [A.length, A[0].length];\n    let isExit = (i, j) => !i || i == M - 1 || !j || j == N - 1;\n    let q = [[...start]];\n    let seen = new Set([`${start[0]},${start[1]}`]);\n    while (q.length) {\n        let k = q.length;\n        while (k--) {\n            let [i, j] = q.shift();\n            if (isExit(i, j) && depth)\n                return depth;\n            for (let [u, v] of [[i - 1, j], [i, j + 1], [i + 1, j], [i, j - 1]])\n                if (0 <= u && u < M && 0 <= v && v < N && A[u][v] == \'.\' && !seen.has(`${u},${v}`))\n                    q.push([u, v]), seen.add(`${u},${v}`);\n        }\n        ++depth;\n    }\n    return -1;\n};\n```\n\n*Python3*\n```\nclass Solution:\n    def nearestExit(self, A: List[List[str]], start: List[int], depth = 0) -> int:\n        M, N = len(A), len(A[0])\n        isExit = lambda i, j: not i or i == M - 1 or not j or j == N - 1\n        q = deque([[*start]])\n        seen = set([f\'{start[0]},{start[1]}\'])\n        while q:\n            k = len(q)\n            while k:\n                i, j = q.popleft()\n                if isExit(i, j) and depth:\n                    return depth\n                for u, v in [[i - 1, j], [i, j + 1], [i + 1, j], [i, j - 1]]:\n                    if 0 <= u < M and 0 <= v < N and A[u][v] == \'.\' and f\'{u},{v}\' not in seen:\n                        q.append([ u, v ]); seen.add(f\'{u},{v}\')\n                k -= 1\n            depth += 1\n        return -1\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VC = vector<char>;\n    using VVC = vector<VC>;\n    using VI = vector<int>;\n    using Pair = pair<int, int>;\n    using Dirs = vector<Pair>;\n    using Seen = unordered_map<int, unordered_map<int, bool>>;\n    using Queue = queue<Pair>;\n    int nearestExit(VVC& A, VI& start, Seen seen = {}, int depth = 0) {\n        int M = A.size(),\n            N = A[0].size();\n        auto isExit = [&](auto i, auto j) {\n            return !i || i == M - 1 || !j || j == N - 1;\n        };\n        auto [i, j] = tie(start[0], start[1]);\n        Queue q{{{ i, j }}};\n        seen[i][j] = true;\n        while (q.size()) {\n            int k = q.size();\n            while (k--) {\n                auto [i, j] = q.front(); q.pop();\n                if (isExit(i, j) && depth)\n                    return depth;\n                for (auto [u, v]: Dirs{{i - 1, j}, {i, j + 1}, {i + 1, j}, {i, j - 1}})\n                    if (0 <= u && u < M && 0 <= v && v < N && A[u][v] == \'.\' && !seen[u][v])\n                        q.push({ u, v }), seen[u][v] = true;\n            }\n            ++depth;\n        }\n        return -1;\n    }\n};\n```
[]