added the matrix algorithm