largest_label = 0; for x in 0 to n_columns { for y in 0 to n_rows { if occupied[x,y] { left = occupied[x-1,y]; above = occupied[x,y-1]; if (left == 0) and (above == 0) { # As-yet-isolated node largest_label = largest_label + 1; label[x,y] = largest_label; } else { if (left != 0) { if (above != 0) # Connected to leftward and upward node union(left,above); label[x,y] = find(above); # Connected to leftward node only } else { label[x,y] = find(above); # Connected to upward node only } } } } }