Inserting nodes whose value in matrix is 1 in the queue .
k = 1 NEW_LINE q = deque ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT dist [ k ] = 0 NEW_LINE visit [ k ] = 1 NEW_LINE q . append ( k ) NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT