Write a function to perform connected-component labeling on a binary image without using external computer vision libraries.
Implement label_components(image, connectivity), where image is a rectangular list of lists containing only 0 and 1, and connectivity is either 4 or 8. Return a same-sized integer matrix where background pixels remain 0, and each foreground component receives a positive label assigned in row-major discovery order. Components use orthogonal neighbors for connectivity 4, and orthogonal plus diagonal neighbors for connectivity 8.
def label_components(image, connectivity):