going back to older algorithm, last commit was looping in case of the arrive point in the extreme of the matrix