Comments Modular Each block 4 triangles, just divide it run the DFS "Count Number of Islands"
\n\n\n\npy\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n        n = len(grid)\n        # 0 = empty, 1 = /, 2 = \\\n        for i i
![image](https://assets.leetcode.com/users/images/9779a9dd-248b-4f1b-9a95-8d0c5ffa9d53_1621952597.9099793.png)\n\n\n\n```py\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n        n = len(grid)\n        # 0 = empty, 1 = /, 2 = \\\n        for i in range(n):\n            grid[i] = grid[i].replace(\' \',\'0\').replace(\'/\',\'1\').replace(\'\\\\\',\'2\')\n\n        # now "count the number of islands code"\n        vis = [[[] for j in r] for r in grid] # each block can be visited in 4 triangles, NSEW so this set will store that\n        # [\'N\',\'E\'] means upper and right triangles are visited \n        res = 0\n        \n        # each block is 4 triangles, with neighbours on 3 sides (may be blocked depending on \'/\'\' or \'\\\')\n        def getNeighbours(i,j,t):\n            neighbours = []\n            if t == \'N\':\n                neighbours.append((i-1,j,\'S\')) # of course we can go the South triangle above me\n                if grid[i][j] != \'1\': neighbours.append((i,j,\'E\')) # can go the the right!!!!\n                if grid[i][j] != \'2\': neighbours.append((i,j,\'W\')) # can go left\n            if t == \'S\':\n                neighbours.append((i+1,j,\'N\'))\n                if grid[i][j] != \'2\': neighbours.append((i,j,\'E\'))\n                if grid[i][j] != \'1\': neighbours.append((i,j,\'W\'))\n            if t == \'W\':\n                neighbours.append((i,j-1,\'E\'))\n                if grid[i][j] != \'1\': neighbours.append((i,j,\'S\'))\n                if grid[i][j] != \'2\': neighbours.append((i,j,\'N\'))\n            if t == \'E\':\n                neighbours.append((i,j+1,\'W\'))\n                if grid[i][j] != \'2\': neighbours.append((i,j,\'S\'))\n                if grid[i][j] != \'1\': neighbours.append((i,j,\'N\'))\n            return neighbours\n        \n        def dfs(i,j,t): # the cell and which triangle \'NSEW\'\n            nonlocal vis, n\n            vis[i][j].append(t)\n            for x,y,nextT in getNeighbours(i,j,t):\n                if 0<=x<n and 0<=y<n and nextT not in vis[x][y]:\n                    dfs(x,y,nextT)\n            \n            \n        for i in range(n):\n            for j in range(n):\n                for t in \'NSEW\':\n                    if t not in vis[i][j]:\n                        dfs(i,j,t)\n                        res += 1\n        return res\n```
['Depth-First Search', 'Graph', 'Python']