Speeding up running time of a code

Hello, I hope everybody is fine at Corona virus times.
Can anyone help me in speeding my code up for large M, for example 20?Thank you.

Gren.nb (19.8 KB)
Gren.pdf (68.3 KB)

Crossposted here.

same