optimizing sum(matrix)