function [h,b,d,g]=coup(U,im);


IS=~isnan(U(:,:,im));

indxg=0;
indyh=0;
indxd=0;
indyb=0;

indice=0;

finxh=0;
finyh=0;
finxb=0;
finyb=0;

while indice==0;

if size(IS,1)==0|size(IS,2)==0
break
end
    
    h=size(IS,2)-sum(IS(1,:));
    g=size(IS,1)-sum(IS(:,1));
    b=size(IS,2)-sum(IS(size(IS,1),:));
    d=size(IS,1)-sum(IS(:,size(IS,2)));
    
    Vec=[g h d b];
    
    [valeur,cote]=max(Vec);
    Svec=sum(Vec);
    
    if Svec>0
    
        if cote==1
            if sum(IS(:,1))<size(IS,1)
               IStemp=zeros(size(IS,1),size(IS,2)-1);
               for m=1:size(IStemp,1)
                   for n=1:size(IStemp,2)
                       IStemp(m,n)=IS(m,n+1);
                   end
               end
               IS=IStemp;
               indxg=indxg+1;
               finxh=0;
            else
               finxh=1;
            end

        elseif cote==2
            if sum(IS(1,:))<size(IS,2)
               IStemp=zeros(size(IS,1)-1,size(IS,2));
               for m=1:size(IStemp,1)
                   for n=1:size(IStemp,2)
                       IStemp(m,n)=IS(m+1,n);
                   end
               end
               IS=IStemp;
               indyh=indyh+1;
               finyh=0;
            else
               finyh=1;
            end

        elseif cote==3        
            if sum(IS(:,size(IS,2)))<size(IS,1)
               IStemp=zeros(size(IS,1),size(IS,2)-1);
               for m=1:size(IStemp,1)
                   for n=1:size(IStemp,2)
                       IStemp(m,n)=IS(m,n);
                   end
               end
               IS=IStemp;
               indxd=indxd+1;
               finxb=0;
            else
               finxb=1;
            end

        elseif cote==4
            if sum(IS(size(IS,1),:))<size(IS,2)
               IStemp=zeros(size(IS,1)-1,size(IS,2));
               for m=1:size(IStemp,1)
                   for n=1:size(IStemp,2)
                       IStemp(m,n)=IS(m,n);
                   end
               end
               IS=IStemp;
               indyb=indyb+1;
               finyb=0;
            else
               finyb=1;
            end
        end
        
        indice=finxh*finyh*finyb*finxb;
        
        else
        indice=1;
    end
 
end

h=indyh;b=indyb;d=indxd;g=indxg;