Added logic to iterate through the matrix.