	module belong

contains

	integer recursive function belong_to(a,i,j,g) result (res)

	implicit none

	integer, dimension (:,:) :: a
	double precision g(3)
	integer i,j

	if (a(i,j) == 4) then
	  a(i,j)=a(i,j)/2
	  g(1)=g(1)+dble(i)
	  g(2)=g(2)+dble(j)
	  g(3)=g(3)+1.d0

	  if (i-1 >= 1) res=belong_to(a,i-1,j,g)
	  if (j-1 >= 1) res=belong_to(a,i,j-1,g)
	  if (i+1 < size(a,1) ) res=belong_to(a,i+1,j,g)
	  if (j+1 < size(a,2) ) res=belong_to(a,i,j+1,g)
	  res=1
	endif
	end function

	end module
