Python Hard
```python3 []
class Solution:
    def maxDistance(self, side: int, points: List[List[int]], k: int) -> int:
        N = side
        y_0 = []
        x_N = []
        y_N = []
        x_0 = []
        for x, y in points:
            if y == 0:
                y_0.append((x, y))
            elif x == N:
                x_N.append((x, y))
            elif y == N:
                y_N.append((x, y))
            elif x == 0:
                x_0.append((x, y))


        A = []

        for x, y in y_0:
            A.append(x)
        for x, y in x_N:
            A.append(side + y)
        for x, y in y_N:
            A.append(side * 2 + (N - x))
        for x, y in x_0:
            A.append(side * 3 + (N - y))

        A.sort()
        

      

        def good(target):

            for start in range(len(A)):
                
                kk = 1
                index = start
                
                while kk < k:
                    dist = A[index] + target
                    j = bisect_left(A, dist)
                    if j == len(A):
                        break

                    index = j
                    kk += 1

                

                    if (A[start] + 4 * N) - A[j] < target:
                        kk = 0
                        break


                if kk == k:
                    return True

            return False



        l, r = 0, N
        best = 0

        while l <= r:
            mid = (l + r) // 2
         
            if good(mid):
                best = mid
                l = mid + 1
            else:
                r = mid - 1

        return best



        
```
['Python3']