Pass one : top left to bottom right
for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , N ) : NEW_LINE INDENT if grid [ i ] [ j ] == 1 : NEW_LINE INDENT grid [ i ] [ j ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT grid [ i ] [ j ] = min ( grid [ i - 1 ] [ j ] , grid [ i ] [ j - 1 ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT