Refactor 'search 2d matrix'.