from typing import List
from typing import Any
from typing import Optional


def calculate_widths(
    rows: List[List[Any]], labels: Optional[List[Any]] = None
) -> List[int]:
    """Calculate the content width of each column.

    :param rows: 2D list containing objects that have a single-line representation (via `str`).
    All rows must be of the same length.
    :param labels: List containing the column labels., defaults to None
    :return: List of integers indicating the width of each cell (not including padding)
    :rtype: List[int]
    """
    col_widths = [0] * len(rows[0])
    if labels:
        col_widths = [len(str(label)) for label in labels]

    for row in rows:
        col_widths = [
            max(len(str(cell)), width) for cell, width in zip(row, col_widths)
        ]

    return col_widths