public boolean searchMatrix(int[][] matrix, int target) {
        int i = 0, j = matrix.length - 1, row = 0;
        while (i <= j) {
            int mid = (i + j) / 2;
            if (matrix[mid][0] == target) return true;
            if (matrix[mid][0] > target) {
                j = mid - 1;
            }
            else {
                row = mid;
                i = mid + 1;
            }
        }

        i = 0;
        j = matrix[row].length - 1;
        while (i <= j) {
            int mid = (i + j) / 2;
            if (matrix[row][mid] == target) return true;
            if (matrix[row][mid] > target) {
                j = mid - 1;
            } else {
                i = mid + 1;
            }
        }

        return false;
    }