import numpy as np

def manhattan_distance_transform(binary_image):
    # Create a distance map of the same size as the input image, initialized with large values
    distance_map = np.ones_like(binary_image) * np.inf

    # Step 1: Initialize the distance of background pixels to 0
    distance_map[binary_image == 0] = 0

    # Initialize the queue for pixel traversal
    queue = []

    # Step 1: Initialize the distance of background pixels to 0 and add them to the queue
    for i in range(binary_image.shape[0]):
        for j in range(binary_image.shape[1]):
            if binary_image[i, j] == 0:
                distance_map[i, j] = 0
                queue.append((i, j))

    # Step 2: Iteratively update the distance values
    while queue:
        # Pop the pixel from the queue
        i, j = queue.pop(0)

        # Iterate over all pixels in the image
        for ni in range(binary_image.shape[0]):
            for nj in range(binary_image.shape[1]):
                # Compute Manhattan distance
                distance = abs(ni - i) + abs(nj - j)

                # Update the distance if it's smaller than the current value
                if distance < distance_map[ni, nj]:
                    distance_map[ni, nj] = distance

    return distance_map

# Example binary image
binary_image = np.array([[0, 1, 1, 1, 1, 1],
                         [1, 1, 1, 1, 1, 1],
                         [1, 1, 0, 0, 1, 1],
                         [1, 1, 0, 0, 1, 1],
                         [1, 1, 1, 1, 1, 1]])

# Compute Manhattan Distance Transform
distance_map = manhattan_distance_transform(binary_image)

print("Manhattan Distance Transform:")
print(distance_map)

