      SUBROUTINE LLSQMP(M,N,A,KA,B,KB,NB,WK,IWK,IERR)
      DIMENSION A(KA,N),B(KB,NB),WK(1),IWK(N)
      LOGICAL EXIT
C     -------------------
C     DIMENSION WK(MN + 2M + N)
C     -------------------
      IERR = 0
      IF (1.LT.N .AND. N.LE.M) GO TO 10
      IERR = 2
      RETURN
C
   10 NP1 = N + 1
      LR = M + 1
      LS = LR + M
      LQ = LS + N
C
      CALL MCOPY(M, N, A, KA, WK(LQ), M)
      CALL ORTHO(M, N, WK(LQ), M, WK(LS), IWK, EXIT)
      IF (EXIT) GO TO 20
      IERR = 3
      RETURN
C
   20 DO 31 J = 1,NB
      DO 21 I = 1,M
   21 WK(I) = B(I,J)
      CALL ORSOL(M, N, WK(LQ), M, WK(LS), IWK, WK(1))
      CALL ORIMP(M, N, A, KA, WK(LQ), M, WK(LS), IWK, B(1,J),
     *                 WK(1), WK(LR), EXIT)
      DO 22 I = 1,N
   22 B(I,J) = WK(I)
      IF (.NOT.EXIT) IERR = 1
      IF (M .EQ. N) GO TO 31
C
      RNORM = 0.0
      DO 30 I = NP1,M
   30 RNORM = RNORM + WK(I)*WK(I)
      B(NP1,J) = SQRT(RNORM)
   31 CONTINUE
      RETURN
      END
