implementation class Matrix with exception handling and reference counting