Set up top row and left column
maxi = grid [ N - 1 ] [ N - 1 ] NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT grid [ N - 1 ] [ i ] = min ( grid [ N - 1 ] [ i ] , grid [ N - 1 ] [ i + 1 ] + 1 ) NEW_LINE maxi = max ( grid [ N - 1 ] [ i ] , maxi ) NEW_LINE DEDENT for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT grid [ i ] [ N - 1 ] = min ( grid [ i ] [ N - 1 ] , grid [ i + 1 ] [ N - 1 ] + 1 ) NEW_LINE maxi = max ( grid [ i ] [ N - 1 ] , maxi + 1 ) NEW_LINE DEDENT