public static int kthSmallest(int[][] matrix, int k){
        Comparator<Node> comparator = new Comparator<Node>() {
            @Override
            public int compare(Node n1, Node n2) {
                return n1.val - n2.val;
            }
        };
        int rows = matrix.length;
        int cols = matrix[0].length;
        int size = Math.min(k, cols);
        PriorityQueue<Node> queue = new PriorityQueue<Node>(k, comparator);

        for(int i = 0; i < size; i++){
            queue.offer(new Node(matrix[0][i], 0, i));
        }

        int min = Integer.MAX_VALUE;
        for(int i = 0; i < k ; i++){
            Node node = queue.poll();
            min = node.val;
            int currRow = node.row;
            int currCol = node.col;
            if(currRow + 1 == rows){
                queue.offer(new Node(Integer.MAX_VALUE, currRow + 1, currCol));
            }else{
                queue.offer(new Node(matrix[currRow + 1][currCol], currRow + 1, currCol));
            }
        }
        return min;
    }