SortedMatrixToSortedArray. Refactored