function [Wii, wflag] = connect_module2(Wi)

%Wi = [1 1 0 0 2;1 0 2 4 0;1 0 2 4 0;1 1 0 0 2];


[w1,w2] = size(Wi);
Wi = Wi(2:end,:);
[w1,w2] = size(Wi);

plus = ones(1,w2);

Wii = zeros(w1,w2);

wflag = zeros(1,w2);
wmem = zeros(1,w2);

cur = 1;
%while cur < w2
%    flag = 0;

    for i = 1:w2
        stack = [];
        if wmem(1,i) == 0
            con = 0;
            wmem(1,i) = 1;
            wflag(1,i) = cur;
            for j = 1:w2
                if Wi(i,j) > 0.01 && wmem(1,j) == 0
                    if j == 35
                        %disp('ok');
                    end

                    wmem(1,j) = 1;
                    wflag(1,j) = cur;
                    if abs(i,j) > 0 
                       if abs(j,i) > 0 
                          con = con + Wi(i,j) + Wi(j,i);
                    if j ~= i
                       stack = [stack j];
                    end
                end
            end

            
            [s1,s2] = size(stack);
            while s2 > 0
                wmem(1,stack(1,1)) = 1;
                %disp(w2);
                %disp(size(Wi));
                %disp(size(wmem));

                for k = 1:w2
                    if stack(1,1) == 33 && k == 35
                        %disp('here');
                    end

                    if Wi(stack(1,1),k) > 0.01 && wmem(1,k) == 0
                        if k == 35
                            %disp('here');
                        end

                        wmem(1,stack(1,1)) = 1;
                        wflag(1,k) = cur;
                        if abs(Wi(stack(1,1),k)) > 0 
                           if abs(Wi(k,stack(1,1))) > 0 
                                con = con + 1;
                            end
                        end

                        if stack(1,1) ~= k
                           stack = [stack k];
                        end
                    end
                end
                stack(:,1) = [];
                [s1,s2] = size(stack);
            end

    
            for j = 1:w2
                for k = 1:w2
                    if wflag(1,j) == cur && wflag(1,k) == cur
                        Wii(j,k) = con;
                    end
                end
            end
            flag = 1;
            cur = cur + 1;
        end
    end
%    if flag == 0
%        break;
%    end
%end

%Wii = zeros(w1,w2);
Wii = [plus;Wii];

%disp('ok');
    

