Another perspective: finding total numbers of cycles in undirected graph
For every grid (i, j), mark the coordinates of the 4 corner points as:\n- top left: (i, j)\n- top right: (i, j+1)\n- bottom left: (i+1, j)\n- bottom right: (i+1
For every grid (i, j), mark the coordinates of the 4 corner points as:\n- top left: (i, j)\n- top right: (i, j+1)\n- bottom left: (i+1, j)\n- bottom right: (i+1, j+1)\n\nIn this way, we can essentially build an Undirected Graph with all connected points:\n- The points along the 4 sides of the matrix are connected with their neighbours\n- If a grid has "/", top-right and bottom-left two points are connected\n- If a grid has "\\\\", top-left and bottom-right two points are connected\n\nThe problem is then translated to: Finding how many cycles in this Undirected Graph\nWe can solve it by DFS. One trick is to remove visited edges along the traversal to avoid duplicates.\nAnd whenever we encounter a visited node, total number of cycles is increased by 1.\n\n```\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n\n        r, c = len(grid), len(grid[0])\n        graph = defaultdict(set)\n        \n        # Add points from top and bottom sides of the matrix to the graph\n        for i in [0, r-1]:\n            for j in range(c):\n                start = (i, j) if i == 0 else (i+1, j)\n                end = (i, j+1) if i == 0 else (i+1, j+1)\n                graph[start].add(end)\n                graph[end].add(start)\n                \n        # Add points from left and right sides of the matrix to the graph\n        for i in range(r):\n            for j in [0, c-1]:\n                start = (i, j) if j == 0 else (i, j+1)\n                end = (i+1, j) if j == 0 else (i+1, j+1)\n                graph[start].add(end)\n                graph[end].add(start)\n            \n        # Add points from grids with slashes to the graph\n        for i in range(r):\n            for j in range(c):\n                if grid[i][j] == \' \': continue\n                start = (i+1, j) if grid[i][j] == \'/\' else (i, j)\n                end = (i, j+1) if grid[i][j] == \'/\' else (i+1, j+1)\n                graph[start].add(end)\n                graph[end].add(start)\n        \n        # DFS to count the total number of cycles from any unvisited node.\n        def dfs(node):\n            area = 0\n            visited.add(node)\n            while graph[node]:\n                v = graph[node].pop()  # pop out random neighbour v from node to check\n                graph[v].discard(node)  # remove the edge v-node in graph[v] to avoid duplicates\n                if v in visited:    # if v is visited, add total number of cycles by 1\n                    area += 1\n                else:\n                    area += dfs(v)\n            return area\n        \n        area = 0\n        visited = set()\n        for node in graph:\n            if node not in visited: area += dfs(node)\n    \n        return area\n```\n\nAlternatively, one can also use Euler\'s formula: Facets = Components - Nodes + Edges.\nThe total numbers of components can be calculated by simple dfs.\nNumber of nodes = len(graph), and the number of edges can be obtained when building graph.
[]