      subroutine MaxValue(q,d,n1,n2,mm,nn,grnd,maxe,mine,maxx,
     &  maxy,minx,miny)
      real*8 d(mm,nn),q(mm,nn)
      real*8 grnd,maxe,mine
      integer maxx,maxy,minx,miny

      maxe = q(1,1)-d(1,1)
      mine = maxe
      maxx = 1
      maxy = 1
      minx = 1
      miny = 1

      do i = 1,n1
         do j = 1,n2
            if(q(i,j).GE.grnd) then
              if((q(i,j)-d(i,j)).GE.maxe) then
                   maxe = (q(i,j)-d(i,j))
                   maxx = i
                   maxy = j
              endif
              if((q(i,j)-d(i,j)).LE.mine) then
                   mine = (q(i,j)-d(i,j))
                   minx = i
                   miny = j
              endif
            endif
          end do
       end do 

       return
       end
