#NEWLINE# def numIslands2( self , n , m , operators ) : #NEWLINE# #INDENT# rows = n #NEWLINE# cols = m #NEWLINE# unroll = ( lambda x , y : ( ( x * cols ) + y ) ) #NEWLINE# mat = [ [ 0 for _ in xrange ( cols ) ] for _ in xrange ( rows ) ] #NEWLINE# uf = UnionFind ( rows , cols ) #NEWLINE# ret = [ ] #NEWLINE# for op in mat : #NEWLINE# #INDENT# op = Op ( r = op [ 0 ] , c = op [ 1 ] ) #NEWLINE# uf . add ( unroll ( op . r , op . c ) ) #NEWLINE# mat [ op . r ] [ op . c ] = 1 #NEWLINE# for dir in self . dirs : #NEWLINE# #INDENT# x1 = ( op . r + dir [ 0 ] ) #NEWLINE# y1 = ( op . c + dir [ 1 ] ) #NEWLINE# if ( ( 0 <= x1 < rows ) and ( 0 <= y1 < cols ) and ( mat [ x1 ] [ y1 ] == 1 ) ) : #NEWLINE# #INDENT# uf . union ( unroll ( op . r , op . c ) , unroll ( x1 , y1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# ret . append ( uf . count ) #NEWLINE# #UNINDENT# return ret