Searching in Sorted Matrix using ruby