!------------------------------------------------------------------------------
!
!  Program name:
!
!    sum5
!
!  Purpose:
!
!    This program sums all elements of a two dimensional array using 
!    the array intrinsic function SUM.
!
!------------------------------------------------------------------------------

PROGRAM sum5

  IMPLICIT NONE
  REAL, DIMENSION (:,:), ALLOCATABLE :: ra
  INTEGER :: nsum, n1, i, j, status

  WRITE (*,'("Input matrix size:")')
  READ (*,*) n1
  ALLOCATE (ra(n1,n1), STAT = status)

  IF (status > 0) STOP 'Fail to allocate memory for allocatable array'

  DO i=1,n1
    DO j=1,n1
      ra(j,i) = (i-1)*n1 + j
    END DO
  END DO

  nsum = SUM(ra)
  WRITE (*,'("Sum for matrix of size ",I3," = ",I10)') n1,nsum

  DEALLOCATE (ra)

END PROGRAM sum5
