@Arithma: I can do my own programming thank you very much. The idea was to see if someone could use the pseudo-code to write the program in C/C++ or any other language. The code presented here is in Matlab, enjoy :)

(PS: If the people on this forum are interested in image processing, I did this great project on liscence plate recognition and character segmentation and could post it)

First import an RGB image, or any image really, and perfrom what is known as edge detection to turn it into a binary image. This is a good way of identfying objects (i.e. edge detection) which is based on determining the contours of the various objects in the image. If this code is to be executed for a randomly generated binary matrix, skip the code sections of figure(1) and figure(2) as they are useless then.
% Object Detection

% This program detects the objects found in the imported image. The
% technique used is the 4-connected iterative approach for connected
% component labeling.

clear all
clc

figure(1)
I = imread('image.bmp');    % Importing an RGB picture from current directory
I = rgb2gray(I);
imshow(I,[]);               % Show picture in grayscale
[M,N] = size(I);

figure(2)
I_bw = edge(I,'canny');     % Performing edge detection with Canny method
imshow(I_bw,[]);            % Show edge detection 

figure(3)                   % Labeling with connected component algorithm
label = 1;                  % iterative approach 
for i = 1:M                 
   for j = 1:N
      if I_bw(i,j)          % Generate labels for all pixels with binary 
         L(i,j) = label;    % value 1
         label = label + 1;
      else
         L(i,j) = 0; 
      end
   end
end
L1 = L;
counter = 0;
A = I_bw;                   % Temporary image used for comparison purposes
B = L1;                     % Temporary image used for comparison purposes 
flip = 0;                   % Flag for tracking path of label generation
                            % 0: top to bottom, left to right
                            % 1: bottom to top, right to left
while(~isequal(A,B))        % Reiteration for updating label continues 
  A = L1;                   % Updating comparison matrix before alteration
                            % is incurred to the original matrix L1
  if(~flip)                 % until no further change occurs                
     for i = 2:M-1          
        for j = 2:N-1
           if L1(i,j)       % Labeling with respect to 4-neighbor connections
              if L1(i,j-1)
                 L2(i,j) = L1(i,j-1);
                 L1(i,j) = L2(i,j);
              elseif L1(i-1,j)  
                 L2(i,j) = L1(i-1,j);
                 L1(i,j) = L2(i,j);
              else
                  L2(i,j) = L1(i,j);
              end
           else
                 L2(i,j) = 0;
           end
        end
     end
     L2(M,:) = 0;
     L2(:,N) = 0;
     B = L2;                % Updating comparison matrix
     L1 = L2;               % Updating labeling matrices
     flip = ~flip;
  else
    j = N - 1;
    while (j ~= 1)
       i = M - 1; 
       while (i ~= 1)
          if L1(i,j)       % Labeling with respect to 4-neighbor connections
             if L1(i+1,j)
                L2(i,j) = L1(i+1,j);
                L1(i,j) = L2(i,j);
             elseif L1(i,j+1) 
                L2(i,j) = L1(i,j+1);
                L1(i,j) = L2(i,j);
             else
                L2(i,j) = L1(i,j); 
             end
          else
                L2(i,j) = 0;
          end
          i = i - 1;
       end
       j = j - 1;
    end
    L2(M,:) = 0;
    L2(:,N) = 0;
    B = L2;                 % Updating comparison matrix
    L1 = L2;                % Updating labeling matrices
    flip = ~flip;
  end
  counter = counter + 1;
end
imshow(L2,[]);             % Displaying labelled objects

figure(4)
[W,num] = bwlabel(I_bw,4); % Labeling with Matlab command bwlabel
imshow(W,[]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Note that the last section figure(4) is used to compare my code to the command used for connected component labeling in Matlab.

This concerns the iterative approach for the 4-connected labelling. Tomorrow I'll post that of the 8-connected iterative approach.
arithma wrote@Padre: Stack Overflow. You missed testing for the obvious use case? Though the solution is mathematically sound.
#define SIZE_X 10000
#define SIZE_Y 10000

int *arr[SIZE_X];

void checkPixel(int X, int Y, int **Ary)
{
    if (!Ary[X][Y]) return;
    Ary[X][Y]=0;
    if (X>0) checkPixel(X-1,Y,Ary);
    if (X<SIZE_X-1) checkPixel(X+1,Y,Ary);
    if (Y>0) checkPixel(X,Y-1,Ary);
    if (X<SIZE_Y-1) checkPixel(X,Y+1,Ary);

}

int countObjects(int **Ary)
{
    int i,j,count;
    count=0;
    for (i=0;i<SIZE_X;i++)
    {
        for (j=0;j<SIZE_Y;j++)
        {
            if (Ary[i][j]) 
            {
                count++;
                checkPixel(i,j,Ary);
            }
        }
    }
    return count;
}

void main(){
	for(int i = 0; i < SIZE_X; i++){
		arr[i] = new int[SIZE_Y];
		for(int j = 0; j < SIZE_Y; j++){
			arr[i][j] = 1;
		}
	}

	countObjects(arr);
}
well yh ... it is recusive !! if it's all ones it will overflow for the 100mil calls :P it's still valid for the presented case.
and i did say tested for average running time :)
But if i have time, i'll come up with a fix :)
% Object Detection

% This program detects the objects found in the imported image. The
% technique used is the 8-connected iterative approach for connected
% component labeling.

clear all
clc

figure(1)
I = imread('image.bmp');    % Importing RGB image from current directory
I = rgb2gray(I);
imshow(I,[]);               % Show image as grayscale picture
[M,N] = size(I');

figure(2)
I_bin = edge(I,'canny');    % Performing edge detection with Canny method
imshow(I_bin,[]);           % Show edge detection 
I_bw = I_bin';

figure(3)                   % Labeling with connected component algorithm
label = 1;                  % iterative approach 
for i = 1:M                 
   for j = 1:N
      if I_bw(i,j)          % Generate labels for all pixels with binary 
         L(i,j) = label;    % value 1
         label = label + 1;
      else
         L(i,j) = 0; 
      end
   end
end
L1 = L;
C = zeros(M,N);
counter = 0;
A = I_bw;                   % Temporary image used for comparison purposes
B = L1;                     % Temporary image used for comparison purposes 
flip = 0;                   % Flag for tracking path of label generation
                            % 0: top to bottom, left to right
                            % 1: bottom to top, right to left
L2 = zeros(M,N);

while ~(isequal(A,B)||(isequal(B,C)&& ~isequal(A,B)))        
                            % Reiteration for updating label continues 
  C = A;                    % Matrix from two prior iterations  
  A = L1;                   % Updating comparison matrix before alteration
                            % is incurred to the original matrix L1
  if(~flip)                 % until no further change occurs                
     for i = 2:M-1          
        for j = 2:N-1
           if L1(i,j)       % Labeling with respect to 8-neighbor connections
              if L1(i-1,j-1)
                 L2(i,j) = L1(i-1,j-1);
                 if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                 end
                 L1(i,j) = L2(i,j);
              elseif L1(i,j-1) 
                 L2(i,j) = L1(i,j-1);
                 if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                 end
                 L1(i,j) = L2(i,j);
              elseif L1(i+1,j-1) 
                 L2(i,j) = L1(i+1,j-1);
                 if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                 end
                 L1(i,j) = L2(i,j);
              elseif L1(i-1,j)
                 L2(i,j) = L1(i-1,j);
                 if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                 end
                 L1(i,j) = L2(i,j);
              else
                 L2(i,j) = L1(i,j);
              end
           end
        end
     end
     B = L2;                  % Updating comparison matrix
     L1 = L2;                 % Updating labeling matrices
     flip = ~flip;
  else
    j = N - 1;
    while (j ~= 1)
       i = M - 1; 
       while (i ~= 1)
          if L1(i,j)          % Labeling with respect to 8-neighbor connections
             if L1(i+1,j+1) 
                L2(i,j) = L1(i+1,j+1);
                if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                end
                L1(i,j) = L2(i,j);
             elseif L1(i,j+1) 
                L2(i,j) = L1(i,j+1);
                if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                end
                L1(i,j) = L2(i,j);
             elseif L1(i-1,j+1)
                L2(i,j) = L1(i-1,j+1);
                if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                end
                L1(i,j) = L2(i,j);   
             elseif L1(i+1,j) 
                L2(i,j) = L1(i+1,j);
                if (L2(i,j) == C(i,j))&&(L2(i,j) ~= L1(i,j))
                     if L2(i-1,j-1)
                        L2(i-1,j-1) = min(C(i,j),L1(i,j));
                        L1(i-1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j)
                        L2(i-1,j) = min(C(i,j),L1(i,j));
                        L1(i-1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i-1,j+1)
                        L2(i-1,j+1) = min(C(i,j),L1(i,j));
                        L1(i-1,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j-1)
                        L2(i,j-1) = min(C(i,j),L1(i,j));
                        L1(i,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i,j+1)
                        L2(i,j+1) = min(C(i,j),L1(i,j));
                        L1(i,j+1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j-1)
                        L2(i+1,j-1) = min(C(i,j),L1(i,j));
                        L1(i+1,j-1) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j)
                        L2(i+1,j) = min(C(i,j),L1(i,j));
                        L1(i+1,j) = min(C(i,j),L1(i,j));
                     end
                     if L2(i+1,j+1)
                        L2(i+1,j+1) = min(C(i,j),L1(i,j));
                        L1(i+1,j+1) = min(C(i,j),L1(i,j));
                     end
                end
                L1(i,j) = L2(i,j);
             else
                L2(i,j) = L1(i,j); 
             end
          end
          i = i - 1;
       end
       j = j - 1;
    end
    B = L2;                 % Updating comparison matrix
    L1 = L2;                % Updating labeling matrices
    flip = ~flip;
  end
  counter = counter + 1
end
imshow(L2',[]);             % Displaying labelled objects

figure(4)
[W,num] = bwlabel(I_bin,8); % Labeling with Matlab command bwlabel
imshow(W,[]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%