Queries to find the count of connected Non | Count of connected cells ; Function to return the representative of the Set to which x belongs ; If x is parent of itself ; x is representative of the Set ; Otherwise ; Path Compression ; Unites the set that includes x and the set that includes y ; Find the representatives ( or the root nodes ) for x an y ; If both are in the same set ; Decrement count ; If x ' s ▁ rank ▁ is ▁ less ▁ than ▁ y ' s rank ; Otherwise ; Then move x under y ( doesn 't matter which one goes where) ; And increment the result tree 's rank by 1 ; Function to count the number of connected cells in the matrix ; Store result for queries ; Store representative of each element ; Initially , all elements are in their own set ; Stores the rank ( depth ) of each node ; If the grid [ x * m + y ] is already set , store the result ; Set grid [ x * m + y ] to 1 ; Increment count . ; Check for all adjacent cells to do a Union with neighbour 's  set if neighbour is also 1 ; Store result . ; Driver Code
ctr = 0 NEW_LINE def find ( parent , x ) : NEW_LINE INDENT if ( parent [ x ] == x ) : NEW_LINE INDENT return x NEW_LINE DEDENT parent [ x ] = find ( parent , parent [ x ] ) NEW_LINE return parent [ x ] NEW_LINE DEDENT def setUnion ( parent , rank , x , y ) : NEW_LINE INDENT global ctr NEW_LINE parentx = find ( parent , x ) NEW_LINE parenty = find ( parent , y ) NEW_LINE if ( parenty == parentx ) : NEW_LINE INDENT return NEW_LINE DEDENT ctr -= 1 NEW_LINE if ( rank [ parentx ] < rank [ parenty ] ) : NEW_LINE INDENT parent [ parentx ] = parenty NEW_LINE DEDENT elif ( rank [ parentx ] > rank [ parenty ] ) : NEW_LINE INDENT parent [ parenty ] = parentx NEW_LINE DEDENT else : NEW_LINE INDENT parent [ parentx ] = parenty NEW_LINE rank [ parenty ] += 1 NEW_LINE DEDENT DEDENT def solve ( n , m , query ) : NEW_LINE INDENT global ctr NEW_LINE result = [ 0 ] * len ( query ) NEW_LINE parent = [ 0 ] * ( n * m ) NEW_LINE for i in range ( n * m ) : NEW_LINE INDENT parent [ i ] = i NEW_LINE DEDENT rank = [ 1 ] * ( n * m ) NEW_LINE grid = [ 0 ] * ( n * m ) NEW_LINE for i in range ( len ( query ) ) : NEW_LINE INDENT x = query [ i ] [ 0 ] NEW_LINE y = query [ i ] [ 1 ] NEW_LINE if ( grid [ m * x + y ] == 1 ) : NEW_LINE INDENT result [ i ] = ctr NEW_LINE continue NEW_LINE DEDENT grid [ m * x + y ] = 1 NEW_LINE ctr += 1 NEW_LINE if ( x > 0 and grid [ m * ( x - 1 ) + y ] == 1 ) : NEW_LINE INDENT setUnion ( parent , rank , m * x + y , m * ( x - 1 ) + y ) NEW_LINE DEDENT if ( y > 0 and grid [ m * ( x ) + y - 1 ] == 1 ) : NEW_LINE INDENT setUnion ( parent , rank , m * x + y , m * ( x ) + y - 1 ) NEW_LINE DEDENT if ( x < n - 1 and grid [ m * ( x + 1 ) + y ] == 1 ) : NEW_LINE INDENT setUnion ( parent , rank , m * x + y , m * ( x + 1 ) + y ) NEW_LINE DEDENT if ( y < m - 1 and grid [ m * ( x ) + y + 1 ] == 1 ) : NEW_LINE INDENT setUnion ( parent , rank , m * x + y , m * ( x ) + y + 1 ) NEW_LINE DEDENT result [ i ] = ctr NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE M = 3 NEW_LINE K = 4 NEW_LINE query = [ [ 0 , 0 ] , [ 1 , 1 ] , [ 1 , 0 ] , [ 1 , 2 ] ] NEW_LINE result = solve ( N , M , query ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT