Can you please tell a more optmized way in terms of space

This is klogk solution

int kthSmallest(vector<vector>& matrix, int k) {
    int n=matrix.size();
    set<pair<int,pair<int,int>>> s;
    s.insert({matrix[0][0],{0,0}});
    k--;
    while(k–){
        pair<int,pair<int,int>> p=*s.begin();
        s.erase(s.begin());
        int val=p.first;
        int row=p.second.first;
        int col=p.second.second;
        if(row+1<n)
              s.insert({matrix[row+1][col],{row+1,col}});
        if(col+1<n)
              s.insert({matrix[row][col+1],{row,col+1}});
    }
   return s.begin()->first;
}