Check for all adjacent cells to do a Union with neighbour 's  set if neighbour is also 1
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