(de)allocation code for M matrix