Java Code, faster than 100.00% of Java online submissions. using BFS
\n public int nearestExit(char[][] maze, int[] entrance) {\n        LinkedList<Integer> que = new LinkedList<>();\n            int n = maze.length, m = maze[0].
```\n public int nearestExit(char[][] maze, int[] entrance) {\n        LinkedList<Integer> que = new LinkedList<>();\n            int n = maze.length, m = maze[0].length;\n            que.addLast(entrance[0] * m + entrance[1]);\n            maze[entrance[0]][entrance[1]] = \'+\';\n            int [][]dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};\n            \n            int level = 0;\n            \n            while(que.size() != 0) {\n                    int size = que.size();\n                    \n                    while(size-->0) {\n                            int idx = que.removeFirst();\n                            int sr = idx / m, sc = idx % m;\n                            \n                            \n                            if((sr == 0 || sr == n - 1|| sc == 0 || sc == m - 1) && level != 0) {\n                                    return level;\n                            }\n                            \n                            for(int d = 0; d < 4; d++) {\n                                    int r = sr + dir[d][0];\n                                    int c = sc + dir[d][1];\n                                    \n                                    if(r >= 0 && c >= 0 && r < n && c < m && maze[r][c] == \'.\') {\n                                            maze[r][c] = \'+\';\n                                            que.addLast(r * m + c);\n                                    }\n                            }\n                    }\n                    level++;\n            }\n            \n            return -1;\n    }\n```
[]