upper margin = (width of mat + height of mat - 2*(dimension of print) / 4

Sounds complicated, but is is simply the mean between the left/right margin and the value of the upper/lower margin if the picture would be placed vertically in the middle of the mat. Thus the larger lower margin does not get so prominent.

Example:
width of mat ( w) = 40cm
height of mat (h) = 50cm
dimension of print (d) = 29cm
upper margin (u) = (40cm + 50cm - 2*29cm)/4 = 8cm

Or the long way:

left/right margin = (lr) (40cm - 29cm)/2 = 5.5cm
upper/lower margin if picture is placed in the middle (ul) = (50cm - 29cm)/2 = 10.5cm

u = lr + (ul - lr)/2 = 5.5cm + (10.5cm - 5.5cm)/2 = 8cm
lo = ul + (ul -lr)/2 = (10.5cm + 2.5cm) = 13cm

Ulrich