def rect_sum(rect: list[list[int]], x1: int, y1: int, x2: int, y2: int) -> int:
    """Calculates sum of pixels of a rectangle within given borders
    Args:
        rect (list[list[int]]): matrix of integers
        x1 (int): top left border X coordinate
        y1 (int): top left border Y coordinate
        x2 (int): bottom right X coordinate
        y2 (int): bottom right Y coordinate

    Returns:
        int: sum of pixels
    """

    # Checking "image"
    if not isinstance(rect, list):
        raise ValueError("\"image\"'s must be a list")
    else:
        for index, item in enumerate(rect):
            if not isinstance(item, list):
                raise ValueError("items of \"image\" must be lists")
            for num in item:
                if not isinstance(num, int):
                    raise ValueError(
                        "items of items of \"image\" must be integers")
            if index == len(rect) - 1:
                pass
            else:
                if len(rect[index]) != len(rect[index + 1]):
                    raise ValueError(
                        "items of \"image\" must be of the same length")

    # Checking coordinates
    top_left_border = (x1, y1)
    bottom_right_border = (x2, y2)
    for coordinate in top_left_border:
        if not isinstance(coordinate, int) or coordinate < 0:
            raise ValueError(
                "border coordinate(s) must be positive integer(s)")
    for coordinate in bottom_right_border:
        if not isinstance(coordinate, int) or coordinate < 0:
            raise ValueError(
                "border coordinate(s) must be positive integer(s)")
    if x1 > len(rect[0]) - 1 or x2 > len(rect[0]) - 1:
        raise ValueError(
            "x1 and/or x2 must not be greater than the number of the matrix columns")
    if y1 > len(rect) - 1 or y2 > len(rect) - 1:
        raise ValueError(
            "y1 and/or y2 must not be greater than the number of the matrix rows")
    if x1 > x2 or y1 > y2:
        raise ValueError(
            "top-left border coordinates must not be greater than bottom-right border coordinates")

    # Calculatins sum
    c = sum(rect[y][x] for y in range(y2 + 1) for x in range(x2 + 1))
    a = sum(rect[y][x] for y in range(y1) for x in range(x1))
    b = sum(rect[y][x] for y in range(y1) for x in range(x2 + 1))
    d = sum(rect[y][x] for y in range(y2 + 1) for x in range(x1))
    s = a + c - b - d
    return s