The function connected is generic, with methods
  for pixel images (class "im") and windows (class "owin")
  described here. There are also methods for tessellations
  (connected.tess), point patterns
  (connected.ppp and connected.lpp),
  and linear networks (connected.linnet).
  
The functions described here compute the connected component transform
  (Rosenfeld and Pfalz, 1966)
  of a binary image or binary mask. The argument X is first
  converted into a pixel image with logical values. Then the algorithm
  identifies the connected components (topologically-connected clumps
  of pixels) in the foreground.
Two pixels belong to the same connected component if they have the value
  TRUE and if they are neighbours (in the 8-connected
  sense). This rule is applied repeatedly until it terminates.
  Then each connected component
  contains all the pixels that can be reached by stepping from neighbour
  to neighbour.
If method="C", the computation is performed by a compiled C language
  implementation of the classical algorithm of Rosenfeld and Pfalz
  (1966). If method="interpreted", the computation is performed
  by an R implementation of the algorithm of Park et al (2000).
The result is a factor-valued image, with levels that correspond to
  the connected components. The Examples show how to extract each
  connected component as a separate window object.