Beats 100% Other Solutions
\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n        n = len(grid)\n        self.sideLength = n\n        TOTAL_POINTS = (n+1)*(n+
```\nclass Solution:\n    def regionsBySlashes(self, grid: List[str]) -> int:\n        n = len(grid)\n        self.sideLength = n\n        TOTAL_POINTS = (n+1)*(n+1)\n        self.parent = [i for i in range(TOTAL_POINTS)]\n        self.rank = [0]*TOTAL_POINTS\n        regions = 1\n        self.addSquareSidePointsToGraph()\n        \n        for row in range(n):\n            for col in range(n):\n                pointA, pointB = -1, -1\n                if grid[row][col] == \'/\':\n                    pointA = self.getPointByCoordinates(row, col+1)\n                    pointB = self.getPointByCoordinates(row+1, col)\n                elif grid[row][col] == \'\\\\\':\n                    pointA = self.getPointByCoordinates(row, col)\n                    pointB = self.getPointByCoordinates(row+1, col+1)\n                else:\n                    continue\n                if pointA != -1 and pointB != -1 and self.union(pointA, pointB):\n                    regions += 1\n        return regions\n        \n        \n    \n    def addSquareSidePointsToGraph(self):\n        for row in range(self.sideLength + 1):\n            for col in range(self.sideLength + 1):\n                if row == 0 or row == self.sideLength or col == 0 or col == self.sideLength:\n                    point = self.getPointByCoordinates(row, col)\n                    self.union(0, point)\n    \n    def getPointByCoordinates(self, x, y):\n        return (self.sideLength+1)*x + y\n    \n    def union(self, A, B):\n        rootA, rootB = self.find(A), self.find(B)\n        if A == B:\n            return False\n        elif rootA == rootB:\n            return True\n        elif self.rank[rootA] > self.rank[rootB]:\n            self.parent[rootB] = rootA\n        elif self.rank[rootA] < self.rank[rootB]:\n            self.parent[rootA] = rootB\n        else:\n            self.rank[rootA] += 1\n            self.parent[rootB] = rootA\n    \n    def find(self, node):\n        parent = self.parent[node]\n        while parent != self.parent[parent]:\n            self.parent[parent] = self.parent[self.parent[parent]]\n            parent = self.parent[parent]\n        return parent\n    \n    \n    \n    """\n    row =  num//4\n    col = num%4\n    """\n```
['Matrix']