! Collection of code snippets by Arne Vajhøj 
! posted to eksperten.dk, usenet and other places (2002-now) 
program fun
    logical more
    integer i, j
    integer a(5, 5)
    real r(5, 5)
    logical process
    call random_seed()
    call random_number(r)
    a = int(20 * r)
    write(*,*) 'Before:'
    do i = lbound(a,1), ubound(a,1)
        write(*,*) (a(i,j), j=lbound(a,2),ubound(a,2))
    end do
    more = .true.
    do while(more)
        more = .false.
        do i = lbound(a,1)+1, ubound(a,1)-1
            do j = lbound(a,2)+1, ubound(a,2)-1
                more = more .or. process(a(i-1:i+1,j-1:j+1))
            end do
        end do
    end do
    write(*,*) 'After:'
    do i = lbound(a,1), ubound(a,1)
        write(*,*) (a(i,j), j=lbound(a,2),ubound(a,2))
    end do
end program fun

logical function process(a)
    integer a(3,3)
    if(a(2,2) > 4) then
        a(2,2) = a(2,2) - 4
        a(1,2) = a(1,2) + 1
        a(3,2) = a(3,2) + 1
        a(2,1) = a(2,1) + 1
        a(2,3) = a(2,3) + 1
        process = .true.
    else
        process = .false.
    end if
end function
