4-connectivity and 8-connectivity of components

A pixel is 4-connected to another pixel if they share an adjacent
vertical or horizontal side.
   Image[i,j] is 4 connected to the pixels 
              Image[i+1,j], Image[i-1,j], Image[i,j+1], Image[i,j-1]

A pixel is 8-connected to another pixel if they share an adjacent
vertical or horizontal side OR a diagonal adjacency.
   Image[i,j] is 8 connected to the pixels 
              Image[i+1,j], Image[i-1,j], 
              Image[i,j+1], Image[i,j-1],
              Image[i+1,j+1], Image[i+1,j-1], 
              Image[i-1,j+1], Image[i-1,j-1]


1. A binary image example:

    0    1    0    0    0    0    0    0
    0    1    1    0    0    0    0    0
    0    0    0    1    0    0    0    0
    0    0    0    0    1    1    1    0
    0    0    0    0    1    0    1    0
    1    1    1    0    1    1    1    0
    0    1    1    0    0    0    0    0
    0    0    0    0    0    0    0    0


The binary image converted to labeled "4-connected" components:\

    0    1    0    0    0    0    0    0
    0    1    1    0    0    0    0    0
    0    0    0    2    0    0    0    0
    0    0    0    0    3    3    3    0
    0    0    0    0    3    0    3    0
    4    4    4    0    3    3    3    0
    0    4    4    0    0    0    0    0
    0    0    0    0    0    0    0    0


2. Another binary image:

    0    1    0    0    0    1    1    1
    0    1    1    0    0    0    1    0
    0    0    0    1    0    0    0    0
    0    0    0    0    1    1    1    0
    0    0    0    0    1    0    1    0
    1    1    1    0    1    1    1    0
    0    1    1    0    0    0    0    0
    0    0    0    0    0    0    4    0


The image converted to labeled "8-connected" components:

    0    1    0    0    0    2    2    2
    0    1    1    0    0    0    2    0
    0    0    0    1    0    0    0    0
    0    0    0    0    1    1    1    0
    0    0    0    0    1    0    1    0
    3    3    3    0    1    1    1    0
    0    3    3    0    0    0    0    0
    0    0    0    0    0    0    4    0