Sum of a matrix using a foreach