Python straightforward O(N) DFS ~100 ms beats 100%
For any square, we have 4 regions named top, right, down, left in the clockwise direction. If you draw diagonals onto a square, you can see regions more clearly
* For any square, we have 4 regions named top, right, down, left in the clockwise direction. If you draw diagonals onto a square, you can see regions more clearly.\n* Firstly, modify grid list, changing "\\\\\\" to "*" for simplicity for reading grid[i][j]\n* Make matrix array marking [top, right, down, left] of each square to zero.\n* Iterate over matrix and process (dfs) if current region of the unit square is zero.\n* Increase group number (cnt) by one before processing\n* For DFS part:\n\t* If character of square is "*" ("\\\\"), update matrix values of\n\t\t* Top and right if k <= 1 and go to related neighbours\n\t\t* Else, down and left and go to related neighbours\n\t* Elif character of square is "/", update matrix values of\n\t\t* Right and down if 1 <= k <= 2 and go to related neighbours\n\t\t* Else top and left and go to related neighbours\n\t* If square has no slash, \n\t\t* Update all 4 regions of current matrix square\n\t\t* Go to all related neighbours\n```\nclass Solution:\n    def regionsBySlashes(self, grid):\n        def dfs(i, j, k):\n            if 0 <= i < n > j >= 0 and not matrix[i][j][k]:\n                if grid[i][j] == "*":\n                    if k <= 1:\n                        matrix[i][j][0] = matrix[i][j][1] = cnt\n                        dfs(i - 1, j, 2)\n                        dfs(i, j + 1, 3)\n                    else:\n                        matrix[i][j][2] = matrix[i][j][3] = cnt\n                        dfs(i + 1, j, 0)\n                        dfs(i, j - 1, 1)\n                elif grid[i][j] == "/":\n                    if 1 <= k <= 2:\n                        matrix[i][j][1] = matrix[i][j][2] = cnt\n                        dfs(i, j + 1, 3)\n                        dfs(i + 1, j, 0)\n                    else:\n                        matrix[i][j][0] = matrix[i][j][3] = cnt\n                        dfs(i - 1, j, 2)\n                        dfs(i, j - 1, 1)\n                else:\n                    matrix[i][j][0] = matrix[i][j][1] = matrix[i][j][2] = matrix[i][j][3] = cnt\n                    dfs(i - 1, j, 2)\n                    dfs(i, j + 1, 3)\n                    dfs(i + 1, j, 0)\n                    dfs(i, j - 1, 1)\n        grid, n = [row.replace("\\\\", "*") for row in grid], len(grid)\n        matrix, cnt = [[[0, 0, 0, 0] for j in range(n)] for i in range(n)], 0\n        for i in range(n):\n            for j in range(n):\n                for k in range(4):\n                    if not matrix[i][j][k]:\n                        cnt += 1\n                        dfs(i, j, k)\n        return cnt\n```
[]