Loop to find the maximum element within the distance of less than K
for q in range ( 1 , K + 1 ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT for j in range ( len ( A [ 0 ] ) ) : NEW_LINE INDENT maxi = ans [ i ] [ j ] ; NEW_LINE if ( i > 0 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT if ( j > 0 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i ] [ j - 1 ] ) ; NEW_LINE DEDENT if ( i < len ( A ) - 1 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i + 1 ] [ j ] ) ; NEW_LINE DEDENT if ( j < len ( A [ 0 ] ) - 1 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i ] [ j + 1 ] ) ; NEW_LINE DEDENT A [ i ] [ j ] = maxi ; NEW_LINE DEDENT DEDENT ans = A ; NEW_LINE DEDENT return ans ; NEW_LINE