SUBROUTINE correct (u, v, ie, je, xm_u, xm_v, nc, nr, im, jm, xland, lrr)
IMPLICIT NONE

INTEGER, INTENT(IN)  :: ie, im, je, jm, nc, nr
REAL   , INTENT(IN)  :: u(nc,nr), v(nc,nr), xland
REAL   , INTENT(OUT) :: xm_u, xm_v
INTEGER, INTENT(OUT) :: lrr
INTEGER :: lr, ist, iend, jst, jend, i, j
REAL    :: cc
lr = 1

50 CONTINUE
ist  = ie-lr
iend = ie+lr
jst  = je-lr
jend = je+lr
IF( ist  .LT. 1)   ist = 1
IF( iend .GT. im)  iend = im
IF( jst  .LT. 1)   jst = 1
IF( jend .GT. jm)  jend = jm
cc   = 0.0
xm_u = 0.0
xm_v = 0.0
DO i = ist, iend
   DO j = jst, jend
      IF( u(i, j) .NE. xland) THEN
         cc = cc + 1.
         xm_u = xm_u + u(i, j)
         xm_v = xm_v + v(i, j)
      ENDIF
   ENDDO
ENDDO
IF( cc .eq. 0. ) THEN
   lr = lr + 1
   GOTO 50
ENDIF
xm_u = xm_u / cc
xm_v = xm_v / cc
lrr = lr

RETURN
END SUBROUTINE correct
