import math


def cluster_variance(X, centroids, labels):
    """
    Returns the within-cluster variance for a particular clustering solution.

    Parameters
    ----------
    X : ndarray
        A 2D array of input data where each row represents a sample and
        each column represents a feature.
    centroids : ndarray
        A 2D array of coordinates where each row represents a cluster
        center and each column represents an axis.
    labels : ndarray
        A 1D array containing the index of the cluster that each sample
        belongs to.

    Returns
    -------
    float
        The sum of within-cluster variance across all clusters.
    """
    total = 0
    # iterate through the rows of input data
    for i in range(X.shape[0]):
        instance = X[i]
        cluster_id = labels[i]
        center = centroids[cluster_id]
        # iterate through the columns of each row
        for j in range(X.shape[1]):
            # compute the euclidean distance from this instance to its centroid
            total += math.pow(instance[j] - center[j], 2)
    # return total distance
    return total