malloc for alt_matrix
Final solution with dynamic memory allocation