https en wikipedia orgwikimedian calculate the median of a sorted matrix args matrix a 2d matrix of integers returns the median value of the matrix examples matrix 1 3 5 2 6 9 3 6 9 medianmatrix 5 matrix 1 2 3 4 5 6 medianmatrix 3 flatten the matrix into a sorted 1d list calculate the middle index return the median calculate the median of a sorted matrix args matrix a 2d matrix of integers returns the median value of the matrix examples matrix 1 3 5 2 6 9 3 6 9 median matrix 5 matrix 1 2 3 4 5 6 median matrix 3 flatten the matrix into a sorted 1d list calculate the middle index return the median
def median(matrix: list[list[int]]) -> int:
    
    
    linear = sorted(num for row in matrix for num in row)

    
    mid = (len(linear) - 1) // 2

    
    return linear[mid]


if __name__ == "__main__":
    import doctest

    doctest.testmod()