from typing import Tuple


def get_neighbors(
    cell: Tuple[int, int],
    shape: Tuple[int, int, int],
    with_start_position: bool = False,
):
    """
    :param cell:
    :param shape:
    :param with_start_position
    :return: The neighbor cells of the given cell, respecting the map shape
    """
    p, q = shape[0], shape[1]
    i, j = cell
    neighbors = [
        (x2, y2)
        for x2 in range(i - 1, i + 2)
        for y2 in range(j - 1, j + 2)
        if (
            -1 < i < p
            and -1 < j < q
            and (i != x2 or j != y2)
            and (0 <= x2 < p)
            and (0 <= y2 < q)
        )
    ]
    if with_start_position:
        neighbors.append((i, j))
    return neighbors