def numIslands2(self, n, m, operators):
        """
        :type n: int
        :type m: int
        :type operators: list[Point]
        :rtype: list[int]
        """
        rows = n
        cols = m
        unroll = lambda x, y: x*cols+y  # hash will be slower
        mat = [[0 for _ in xrange(cols)] for _ in xrange(rows)]
        uf = UnionFind(rows, cols)
        ret = []
        for op in operators:
            uf.add(unroll(op.x, op.y))
            mat[op.x][op.y] = 1
            for dir in self.dirs:
                x1 = op.x+dir[0]
                y1 = op.y+dir[1]
                if 0 <= x1 < rows and 0 <= y1 < cols and mat[x1][y1] == 1:
                    uf.union(unroll(op.x, op.y), unroll(x1, y1))

            ret.append(uf.count)

        return ret
dataset/ETHPy150Open algorhythms/LintCode/Number of Islands II.py/Solution.numIslands2/original