class Solution:
    def minMalwareSpread(self, graph: [[int]], initial: [int]) -> int:
        parent = {c: c for c in range(len(graph))}

        def union(a, b):
            x = find(a)
            y = find(b)
            if x != y:
                parent[x] = y

        def find(item):
            if parent[item] == item:
                return item
            parent[item] = find(parent[item])
            return parent[item]
        for i in range(0, len(initial)):
            for j in range(i+1, len(initial)):
                if graph[initial[i], initial[j]] == 1:
                    union(initial[i], initial[j])
