My code needs to be improved

Hello;
I cannot change the NN and M values ​​as I want in the attached code.

I need your help. I would appreciate it if you could review it when you have time. Thank you.