Complete implementation of cacheMatrix.