40 minutes would be very short for this connection. Munich is your first port of entry into the Schengen area. So you have to go through immigration control in Munich. Depending on your citizenship and time of day, this may take a little longer.
If Lufthansa is willing to sell you this ticket as a single ticket, this is a legal connection and you have a good chance of doing it. But there is no room for error: if something goes wrong, is delayed or just a little longer than usual, you will miss it. If you miss it, LH will rebook you for the next available flight for free, but you may miss your cruise so I would advise against it.
There is NO POSSIBILITY to do this with two separate tickets.