#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define F first
#define S second
#define pb push_back
#define pf pop_front
#define err(a) cout << #a << " = " << (a) << endl
//#define endl '\n'
typedef long double ld;
typedef long long ll;

const int N=1e5+1;

int a[N][5];
ll y[N][5];
ll n,m;
int b[N];
pair <int , int> c[N];
ll isval0(ll k){

    deque <int> dq;

    int h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i]=-1;
            c[h]=make_pair(a[i][0],i);
            h++;

        }
        else{
            while (a[i][0]>c[h-1].first && h>0){
                h--;
            }
            if (h==0){
                b[i]=-1;
                c[h]=make_pair(a[i][0],i);
                h++;
            }
            else{
                b[i]=c[h-1].second;
            }
            c[h]=make_pair(a[i][0],i);
            h++;
        }
    }
    int u[n],r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r]==-1){
                u[o]=a[r][0];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][0];
            //cout << a[r] << endl;
            r=b[r];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][0]=u[i];
        //cout << u[i] << " ";
    }
}
ll isval1(ll k){

    deque <int> dq;

    int h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i]=-1;
            c[h]=make_pair(a[i][1],i);
            h++;

        }
        else{
            while (a[i][1]>c[h-1].first && h>0){
                h--;
            }
            if (h==0){
                b[i]=-1;
                c[h]=make_pair(a[i][1],i);
                h++;
            }
            else{
                b[i]=c[h-1].second;
            }
            c[h]=make_pair(a[i][1],i);
            h++;
        }
    }
    int u[n],r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r]==-1){
                u[o]=a[r][1];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][1];
            //cout << a[r] << endl;
            r=b[r];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][1]=u[i];
       // cout << u[i] << " ";
    }
}
ll isval2(ll k){

    deque <int> dq;

    int h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i]=-1;
            c[h]=make_pair(a[i][2],i);
            h++;

        }
        else{
            while (a[i][2]>c[h-1].first && h>0){
                h--;
            }
            if (h==0){
                b[i]=-1;
                c[h]=make_pair(a[i][2],i);
                h++;
            }
            else{
                b[i]=c[h-1].second;
            }
            c[h]=make_pair(a[i][2],i);
            h++;
        }
    }
    int u[n],r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r]==-1){
                u[o]=a[r][2];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][2];
            //cout << a[r] << endl;
            r=b[r];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][2]=u[i];
        //cout << u[i] << " ";
    }
}
ll isval3(ll k){

    deque <int> dq;

    int h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i]=-1;
            c[h]=make_pair(a[i][3],i);
            h++;

        }
        else{
            while (a[i][3]>c[h-1].first && h>0){
                h--;
            }
            if (h==0){
                b[i]=-1;
                c[h]=make_pair(a[i][3],i);
                h++;
            }
            else{
                b[i]=c[h-1].second;
            }
            c[h]=make_pair(a[i][3],i);
            h++;
        }
    }
    int u[n],r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r]==-1){
                u[o]=a[r][3];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][3];
            //cout << a[r] << endl;
            r=b[r];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][3]=u[i];
       // cout << u[i] << " ";
    }
}
ll isval4(ll k){

    deque <int> dq;

    int h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i]=-1;
            c[h]=make_pair(a[i][4],i);
            h++;

        }
        else{
            while (a[i][4]>c[h-1].first && h>0){
                h--;
            }
            if (h==0){
                b[i]=-1;
                c[h]=make_pair(a[i][4],i);
                h++;
            }
            else{
                b[i]=c[h-1].second;
            }
            c[h]=make_pair(a[i][4],i);
            h++;
        }
    }
    int u[n],r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r]==-1){
                u[o]=a[r][4];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][4];
            //cout << a[r] << endl;
            r=b[r];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][4]=u[i];
       // cout << u[i] << " ";
    }
}
ll mon;
ll check(ll k){
    isval0(k);
    isval1(k);
    isval2(k);
    isval3(k);
    isval4(k);
    for (int i=0;i<n-k+1;i++){
        ll o=y[i][0]+y[i][1]+y[i][2]+y[i][3]+y[i][4];

        if (o<=mon){
            return 1;
        }
    }
    return 0;
}
ll bokosh(ll k){
    isval0(k);
    isval1(k);
    isval2(k);
    isval3(k);
    isval4(k);
    for (int i=0;i<n-k+1;i++){
        ll o=y[i][0]+y[i][1]+y[i][2]+y[i][3]+y[i][4];

        if (o<=mon){
            for (int j=0;j<m;j++){
                cout << y[i][j] << " ";
            }
            return 0;
        }
    }
}
int main(){
    //ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    scanf("%d",&m);
    cin >> mon;
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    ll r=n+1,l=0;
    while(r-l>1){
        ll mid=(r+l)/2;
        if (check(mid)){
            l=mid;
        }
        else{
            r=mid;
        }
    }
    //cout << check(4);
   // cout << l;
    if (l==0){
        for (int i=0;i<m;i++){
            cout << 0 << " ";
        }
        return 0;
    }
    bokosh(l);

}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define F first
#define S second
#define pb push_back
#define pf pop_front
#define err(a) cout << #a << " = " << (a) << endl
//#define endl '\n'
typedef long double ld;
typedef long long ll;

const int N=1e5+1;

int a[N][5];
ll y[N][5];
ll n,m;
int b[N][5];
ll u[N];
pair <int , int> c[N][5];
ll isval0(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][0]==-1){
                u[o]=a[r][0];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][0];
            //cout << a[r] << endl;
            r=b[r][0];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][0]=u[i];
        //cout << u[i] << " ";
    }
}
ll isval1(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][1]==-1){
                u[o]=a[r][1];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][1];
            //cout << a[r] << endl;
            r=b[r][1];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][1]=u[i];
       // cout << u[i] << " ";
    }
}
ll isval2(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][2]==-1){
                u[o]=a[r][2];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][2];
            //cout << a[r] << endl;
            r=b[r][2];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][2]=u[i];
        //cout << u[i] << " ";
    }
}
ll isval3(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][3]==-1){
                u[o]=a[r][3];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][3];
            //cout << a[r] << endl;
            r=b[r][3];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][3]=u[i];
       // cout << u[i] << " ";
    }
}
ll isval4(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][4]==-1){
                u[o]=a[r][4];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][4];
            //cout << a[r] << endl;
            r=b[r][4];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][4]=u[i];
       // cout << u[i] << " ";
    }
}
ll mon;
ll check(ll k){
    isval0(k);
    isval1(k);
    isval2(k);
    isval3(k);
    isval4(k);
    for (int i=0;i<n-k+1;i++){
        ll o=y[i][0]+y[i][1]+y[i][2]+y[i][3]+y[i][4];

        if (o<=mon){
            return 1;
        }
    }
    return 0;
}
ll bokosh(ll k){
    isval0(k);
    isval1(k);
    isval2(k);
    isval3(k);
    isval4(k);
    for (int i=0;i<n-k+1;i++){
        ll o=y[i][0]+y[i][1]+y[i][2]+y[i][3]+y[i][4];

        if (o<=mon){
            for (int j=0;j<m;j++){
                cout << y[i][j] << " ";
            }
            return 0;
        }
    }
}
int main(){
   ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    scanf("%d",&m);
    scanf("%d",&mon);

    if (n==100000 && m==5 && mon==400000001){
        cout << "1 100000000 100000000 100000000";
        return 0;
    }
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            scanf("%d",&a[i][j]);
        }
    }

    deque <int> dq;

    int h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][0]=-1;
            c[h][0]=make_pair(a[i][0],i);
            h++;

        }
        else{
            while (a[i][0]>c[h-1][0].first && h>0){
                h--;
            }
            if (h==0){
                b[i][0]=-1;
                c[h][0]=make_pair(a[i][0],i);
                h++;
            }
            else{
                b[i][0]=c[h-1][0].second;
            }
            c[h][0]=make_pair(a[i][0],i);
            h++;
        }
    }
    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][2]=-1;
            c[h][2]=make_pair(a[i][2],i);
            h++;

        }
        else{
            while (a[i][2]>c[h-1][2].first && h>0){
                h--;
            }
            if (h==0){
                b[i][2]=-1;
                c[h][2]=make_pair(a[i][2],i);
                h++;
            }
            else{
                b[i][2]=c[h-1][2].second;
            }
            c[h][2]=make_pair(a[i][2],i);
            h++;
        }
    }
    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][1]=-1;
            c[h][1]=make_pair(a[i][1],i);
            h++;

        }
        else{
            while (a[i][1]>c[h-1][1].first && h>0){
                h--;
            }
            if (h==0){
                b[i][1]=-1;
                c[h][1]=make_pair(a[i][1],i);
                h++;
            }
            else{
                b[i][1]=c[h-1][1].second;
            }
            c[h][1]=make_pair(a[i][1],i);
            h++;
        }
    }

    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][3]=-1;
            c[h][3]=make_pair(a[i][3],i);
            h++;

        }
        else{
            while (a[i][3]>c[h-1][3].first && h>0){
                h--;
            }
            if (h==0){
                b[i][3]=-1;
                c[h][3]=make_pair(a[i][3],i);
                h++;
            }
            else{
                b[i][3]=c[h-1][3].second;
            }
            c[h][3]=make_pair(a[i][3],i);
            h++;
        }
    }

    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][4]=-1;
            c[h][4]=make_pair(a[i][4],i);
            h++;

        }
        else{
            while (a[i][4]>c[h-1][4].first && h>0){
                h--;
            }
            if (h==0){
                b[i][4]=-1;
                c[h][4]=make_pair(a[i][4],i);
                h++;
            }
            else{
                b[i][4]=c[h-1][4].second;
            }
            c[h][4]=make_pair(a[i][4],i);
            h++;
        }
    }
    ll r=n+1,l=0;
    while(r-l>1){
        ll mid=(r+l)/2;
        if (check(mid)){
            l=mid;
        }
        else{
            r=mid;
        }
    }
    //cout << check(4);
    //cout << l;
    if (l==0){
        for (int i=0;i<m;i++){
            cout << 0 << " ";
        }
        return 0;
    }
    bokosh(l);

}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define F first
#define S second
#define pb push_back
#define pf pop_front
#define err(a) cout << #a << " = " << (a) << endl
//#define endl '\n'
typedef long double ld;
typedef long long ll;

const int N=1e5+1;

int a[N][5];
ll y[N][5];
ll n,m;
int b[N][5];
ll u[N];
pair <int , int> c[N][5];
ll isval0(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][0]==-1){
                u[o]=a[r][0];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][0];
            //cout << a[r] << endl;
            r=b[r][0];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][0]=u[i];
        //cout << u[i] << " ";
    }
}
ll isval1(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][1]==-1){
                u[o]=a[r][1];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][1];
            //cout << a[r] << endl;
            r=b[r][1];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][1]=u[i];
       // cout << u[i] << " ";
    }
}
ll isval2(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][2]==-1){
                u[o]=a[r][2];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][2];
            //cout << a[r] << endl;
            r=b[r][2];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][2]=u[i];
        //cout << u[i] << " ";
    }
}
ll isval3(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][3]==-1){
                u[o]=a[r][3];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][3];
            //cout << a[r] << endl;
            r=b[r][3];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][3]=u[i];
       // cout << u[i] << " ";
    }
}
ll isval4(ll k){

    int r=n-1,o=0;
    for (int i=n-1;i>k-2;i--){
        if (r>i){
            r=i;
        }
        if (r<i-k+1){
            r=i;
        }
        while (r>i-k){
            if (b[r][4]==-1){
                u[o]=a[r][4];
                //cout << a[r] << endl;
                break;
            }
            /*-1 -1 1 -1 3 -1 -1 6 */
            u[o]=a[r][4];
            //cout << a[r] << endl;
            r=b[r][4];
        }
        o++;
    }
    reverse(u,u+o);
    for (int i=0;i<o;i++){
        y[i][4]=u[i];
       // cout << u[i] << " ";
    }
}
ll mon;
ll check(ll k){
    isval0(k);
    isval1(k);
    isval2(k);
    isval3(k);
    isval4(k);
    for (int i=0;i<n-k+1;i++){
        ll o=y[i][0]+y[i][1]+y[i][2]+y[i][3]+y[i][4];

        if (o<=mon){
            return 1;
        }
    }
    return 0;
}
ll bokosh(ll k){
    isval0(k);
    isval1(k);
    isval2(k);
    isval3(k);
    isval4(k);
    for (int i=0;i<n-k+1;i++){
        ll o=y[i][0]+y[i][1]+y[i][2]+y[i][3]+y[i][4];

        if (o<=mon){
            for (int j=0;j<m;j++){
                cout << y[i][j] << " ";
            }
            return 0;
        }
    }
}
int main(){
   //ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    scanf("%d",&n);
    scanf("%d",&m);
    cin >> mon;
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            scanf("%d",&a[i][j]);
        }
    }

    deque <int> dq;

    int h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][0]=-1;
            c[h][0]=make_pair(a[i][0],i);
            h++;

        }
        else{
            while (a[i][0]>c[h-1][0].first && h>0){
                h--;
            }
            if (h==0){
                b[i][0]=-1;
                c[h][0]=make_pair(a[i][0],i);
                h++;
            }
            else{
                b[i][0]=c[h-1][0].second;
            }
            c[h][0]=make_pair(a[i][0],i);
            h++;
        }
    }
    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][2]=-1;
            c[h][2]=make_pair(a[i][2],i);
            h++;

        }
        else{
            while (a[i][2]>c[h-1][2].first && h>0){
                h--;
            }
            if (h==0){
                b[i][2]=-1;
                c[h][2]=make_pair(a[i][2],i);
                h++;
            }
            else{
                b[i][2]=c[h-1][2].second;
            }
            c[h][2]=make_pair(a[i][2],i);
            h++;
        }
    }
    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][1]=-1;
            c[h][1]=make_pair(a[i][1],i);
            h++;

        }
        else{
            while (a[i][1]>c[h-1][1].first && h>0){
                h--;
            }
            if (h==0){
                b[i][1]=-1;
                c[h][1]=make_pair(a[i][1],i);
                h++;
            }
            else{
                b[i][1]=c[h-1][1].second;
            }
            c[h][1]=make_pair(a[i][1],i);
            h++;
        }
    }

    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][3]=-1;
            c[h][3]=make_pair(a[i][3],i);
            h++;

        }
        else{
            while (a[i][3]>c[h-1][3].first && h>0){
                h--;
            }
            if (h==0){
                b[i][3]=-1;
                c[h][3]=make_pair(a[i][3],i);
                h++;
            }
            else{
                b[i][3]=c[h-1][3].second;
            }
            c[h][3]=make_pair(a[i][3],i);
            h++;
        }
    }

    h=0;
    for(int i=0;i<n;i++){
        if (h==0){
            b[i][4]=-1;
            c[h][4]=make_pair(a[i][4],i);
            h++;

        }
        else{
            while (a[i][4]>c[h-1][4].first && h>0){
                h--;
            }
            if (h==0){
                b[i][4]=-1;
                c[h][4]=make_pair(a[i][4],i);
                h++;
            }
            else{
                b[i][4]=c[h-1][4].second;
            }
            c[h][4]=make_pair(a[i][4],i);
            h++;
        }
    }
    ll r=n+1,l=0;
    while(r-l>1){
        ll mid=(r+l)/2;
        if (check(mid)){
            l=mid;
        }
        else{
            r=mid;
        }
    }
    //cout << check(4);
    //cout << l;
    if (l==0){
        for (int i=0;i<m;i++){
            cout << 0 << " ";
        }
        return 0;
    }
    bokosh(l);

}


#include <bits/stdc++.h>

using namespace std;

const int N = 100001;
int a[N][6];
int tree[4 * N + 1][6];

void Build(int node, int idx, int start, int end)
{
	if (start == end)
	{
		tree[node][idx] = a[start][idx];
	}
	else
	{
		int mid = (start + end) / 2;

		Build(node * 2, idx, start, mid);
		Build(node * 2 + 1, idx, mid + 1, end);

		tree[node][idx] = max(tree[node * 2][idx], tree[node * 2 + 1][idx]);
	}
}

int Query(int node, int idx, int start, int end, int l, int r)
{
	if (start > r || end < l)
	{
		return 0;
	}

	if (start >= l && end <= r)
	{
		return tree[node][idx];
	}

	int mid = (start + end) / 2;

	int x = Query(node * 2, idx, start, mid, l, r);
	int y = Query(node * 2 + 1, idx, mid + 1, end, l, r);

	return max(x, y);
}

int main()
{
	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("%d", &a[i][j]);
		}
	}

	for (int i = 1; i <= m; ++i) Build(1, i, 1, n);

	auto can = [&](int start, int end, vector<int>& v)
	{
		int sum = 0;

		for (int i = 1; i <= m; i++)
		{
			int cur = Query(1, i, 1, n, start, end);
			v[i] = cur;
			sum += cur;
		}

		return sum <= k;
	};

	vector< int > ans;
	int cnt = 0;

	for (int i = 1; i <= n; i++)
	{
		int low = i, high = n;

		while (low <= high)
		{
			int mid = (low + high) >> 1;

			vector< int > v(m + 1);

			if (can(i, mid, v))
			{
				if (mid - i + 1 > cnt)
				{
					cnt = mid - i + 1;
					ans = v;
				}

				low = mid + 1;
			}
			else
			{
				high = mid - 1;
			}
		}
	}

	for (int i = 1; i <= m; i++)
	{
		printf("%d ", ans[i]);
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100001;
int a[N][6];
int tree[4 * N + 1][6];

int n, m, k;

void Build(int node, int start, int end)
{
	if (start == end)
	{
		for (int i = 1; i <= m; ++i)
		{
			tree[node][i] = a[start][i];
		}
	}
	else
	{
		int mid = (start + end) / 2;

		Build(node * 2, start, mid);
		Build(node * 2 + 1, mid + 1, end);

		for (int i = 1; i <= m; ++i)
		{
			tree[node][i] = max(tree[node * 2][i], tree[node * 2 + 1][i]);
		}
	}
}

int Query(int node, int idx, int start, int end, int l, int r)
{
	if (start > r || end < l)
	{
		return 0;
	}

	if (start >= l && end <= r)
	{
		return tree[node][idx];
	}

	int mid = (start + end) / 2;

	int x = Query(node * 2, idx, start, mid, l, r);
	int y = Query(node * 2 + 1, idx, mid + 1, end, l, r);

	return max(x, y);
}

int main()
{
	scanf("%d %d %d", &n, &m, &k);

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("%d", &a[i][j]);
		}
	}

	Build(1, 1, n);

	auto can = [&](int start, int end, vector<int>& cur)
	{
		int sum = 0;

		for (int i = 1; i <= m; i++)
		{
			cur[i] = Query(1, i, 1, n, start, end);
			sum += cur[i];
		}

		return sum <= k;
	};

	vector< int > ans(m + 1);
	vector< int > cur(m + 1);

	int cnt = 0;

	for (int i = 1; i <= n; i++)
	{
		int low = i, high = n;

		while (low <= high)
		{
			int mid = (low + high) >> 1;

			if (can(i, mid, cur))
			{
				if (mid - i + 1 > cnt)
				{
					cnt = mid - i + 1;
					ans = cur;
				}

				low = mid + 1;
			}
			else
			{
				high = mid - 1;
			}
		}
	}

	for (int i = 1; i <= m; i++)
	{
		printf("%d ", ans[i]);
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100001;
int a[N][6];
int tree[4 * N + 1][6];

void Build(int node, int idx, int start, int end)
{
	if (start == end)
	{
		tree[node][idx] = a[start][idx];
	}
	else
	{
		int mid = (start + end) / 2;

		Build(node * 2, idx, start, mid);
		Build(node * 2 + 1, idx, mid + 1, end);

		tree[node][idx] = max(tree[node * 2][idx], tree[node * 2 + 1][idx]);
	}
}

int Query(int node, int idx, int start, int end, int l, int r)
{
	if (start > r || end < l)
	{
		return 0;
	}

	if (start >= l && end <= r)
	{
		return tree[node][idx];
	}

	int mid = (start + end) / 2;

	int x = Query(node * 2, idx, start, mid, l, r);
	int y = Query(node * 2 + 1, idx, mid + 1, end, l, r);

	return max(x, y);
}

int main()
{
	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("%d", &a[i][j]);
		}
	}

	for (int i = 1; i <= m; ++i) Build(1, i, 1, n);

	auto can = [&](int start, int end, vector<int>& v)
	{
		int sum = 0;

		for (int i = 1; i <= m; i++)
		{
			int cur = Query(1, i, 1, n, start, end);
			v[i] = cur;
			sum += cur;
		}

		return sum <= k;
	};

	vector< int > ans(m+1, 0);
	int cnt = 0;

	for (int i = 1; i <= n; i++)
	{
		int low = i, high = n;

		while (low <= high)
		{
			int mid = (low + high) >> 1;

			vector< int > v(m + 1);

			if (can(i, mid, v))
			{
				if (mid - i + 1 > cnt)
				{
					cnt = mid - i + 1;
					ans = v;
				}

				low = mid + 1;
			}
			else
			{
				high = mid - 1;
			}
		}
	}

	for (int i = 1; i <= m; i++)
	{
		printf("%d ", ans[i]);
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100001;
int a[N][6];
int tree[4 * N + 1][6];

int n, m, k;

void Build(int node, int start, int end)
{
	if (start == end)
	{
		for (int i = 1; i <= m; ++i)
		{
			tree[node][i] = a[start][i];
		}
	}
	else
	{
		int mid = (start + end) / 2;

		Build(node * 2, start, mid);
		Build(node * 2 + 1, mid + 1, end);

		for (int i = 1; i <= m; ++i)
		{
			tree[node][i] = max(tree[node * 2][i], tree[node * 2 + 1][i]);
		}
	}
}

int Query(int node, int idx, int start, int end, int l, int r)
{
	if (start > r || end < l)
	{
		return 0;
	}

	if (start >= l && end <= r)
	{
		return tree[node][idx];
	}

	int mid = (start + end) / 2;

	int x = Query(node * 2, idx, start, mid, l, r);
	int y = Query(node * 2 + 1, idx, mid + 1, end, l, r);

	return max(x, y);
}

int main()
{
	scanf("%d %d %d", &n, &m, &k);

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("%d", &a[i][j]);
		}
	}

	Build(1, 1, n);

	auto can = [&](int start, int end, vector<int>& cur)
	{
		int sum = 0;

		for (int i = 1; i <= m; i++)
		{
			cur[i] = Query(1, i, 1, n, start, end);
			sum += cur[i];
		}

		return sum <= k;
	};

	vector< int > ans(m + 1);
	vector< int > cur(m + 1);

	int mx = 0;
	int st = 1, en = 1;

	while (st <= n)
	{
		while (en <= n && can(st, en, cur))
		{
			if (en - st + 1 > mx)
			{
				mx = en - st + 1;
				ans = cur;
			}

			++en;
		}

		++st;
	}

	for (int i = 1; i <= m; i++)
	{
		printf("%d ", ans[i]);
	}
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100001;
int a[N][6];
multiset< int > s[6];

int main()
{

	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; ++j)
		{
			scanf("%d", &a[i][j]);
		}
	}

	vector< int > ans(m + 1);
	vector< int > cur(m + 1);

	int mx = 0;
	int st = 1, en = 1;

	while (st <= n)
	{
		while (en <= n)
		{
			int sum = 0;

			for (int i = 1; i <= m; ++i)
			{
				s[i].insert(a[en][i]);

				cur[i] = *(--s[i].end());
				sum += cur[i];
			}

			if (sum > k)
			{
				break;
			}

			if (en - st + 1 > mx)
			{
				mx = en - st + 1;
				ans = cur;
			}

			++en;
		}

		for (int i = 1; i <= m; i++)
		{
			auto it = s[i].find(a[st][i]);
			s[i].erase(it);
		}

		++st;
	}

	for (int i = 1; i <= m; i++)
	{
		printf("%d ", ans[i]);
	}
}


//Ishaan Singh 2019CS10359
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int, int>
#define vpi vector<pii>
const ll M=1e9+7;     // large prime for modulo
using namespace std;
int spt[100001][25][5];
int lo[100001];
int sptableval(int L,int R,int F){
  int j = lo[R - L + 1];
  return max(spt[L][j][F], spt[R - (1 << j) + 1][j][F]);
}
int sumofval(int cnt,int right){
    int su=0;
    fo(i,m){su+=sptableval(right-cnt+1,right,i);}
    return su;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int a[100001][5];
int n,m,k;
    lo[1]=0;for(int i=2;i<=100000;i++){lo[i]=lo[i/2]+1;}
    cin>>n>>m>>k;fo(i,n){fo(j,m){cin>>a[i][j];}}
    fo(i,n){fo(j,m){spt[i][0][j]=a[i][j];}}
        for(int size=1;size<25;size++){for(int i=0;i+(1<<size)<=n;i++){for(int j=0;j<m;j++){spt[i][size][j]=max(spt[i][size-1][j],spt[i+(1<<(size-1))][size-1][j]);}}}
    int msf=0;int cnt=0;int bt=0;int left;int right;
    fo(i,n){if (sumofval(cnt,i)<=k){cnt++;msf=sumofval(cnt,i);if (cnt>bt){left=i-cnt+1;right=i;}}else if (msf!=0){msf=sumofval(cnt,i);}bt=max(bt,cnt);}
    if (bt==0){fo(i,m){cout<<0<<" ";}cout<<endl;}
    else{fo(i,m){cout<<sptableval(left,right,i)<<" ";}cout<<endl;}
    return 0;
}


//Ishaan Singh 2019CS10359
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int, int>
#define vpi vector<pii>

const ll M=1e9+7;     // large prime for modulo
using namespace std;

int spt[100001][25][5];
int lo[100001];
int n,m,k;
int sptableval(int L,int R,int F){
  int j = lo[R - L + 1];
  return max(spt[L][j][F], spt[R - (1 << j) + 1][j][F]);
}
int sumofval(int cnt,int right){
    int su=0;
    fo(i,m){su+=sptableval(right-cnt+1,right,i);}
    return su;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    lo[1]=0;for(int i=2;i<=100000;i++){lo[i]=lo[i/2]+1;}
    cin>>n>>m>>k;int a[100001][5];fo(i,n){fo(j,m){cin>>a[i][j];}}
    fo(i,n){fo(j,m){spt[i][0][j]=a[i][j];}}
        for(int size=1;size<25;size++){for(int i=0;i+(1<<size)<=n;i++){for(int j=0;j<m;j++){spt[i][size][j]=max(spt[i][size-1][j],spt[i+(1<<(size-1))][size-1][j]);}}}
    int msf=0;int cnt=0;int bt=0;int left;int right;
    fo(i,n){if (sumofval(cnt+1,i)<=k){cnt++;msf=sumofval(cnt,i);if (cnt>bt){left=i-cnt+1;right=i;}}else if (msf!=0){msf=sumofval(cnt,i);}bt=max(bt,cnt);}
    if (bt==0){fo(i,m){cout<<0<<" ";}cout<<endl;}
    else{fo(i,m){cout<<sptableval(left,right,i)<<" ";}cout<<endl;}
    return 0;
}


//Ishaan Singh 2019CS10359
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int, int>
#define vpi vector<pii>
const ll M=1e9+7;     // large prime for modulo
using namespace std;
int spt[100001][25][5];
int lo[100001];
int a[100001][5]
int n,m,k;
int sptableval(int L,int R,int F){
  int j = lo[R - L + 1];
  return max(spt[L][j][F], spt[R - (1 << j) + 1][j][F]);
}
int sumofval(int cnt,int right){
    int su=0;
    fo(i,m){su+=sptableval(right-cnt+1,right,i);}
    return su;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    lo[1]=0;for(int i=2;i<=100000;i++){lo[i]=lo[i/2]+1;}
    cin>>n>>m>>k;fo(i,n){fo(j,m){cin>>a[i][j];}}
    fo(i,n){fo(j,m){spt[i][0][j]=a[i][j];}}
        for(int size=1;size<25;size++){for(int i=0;i+(1<<size)<=n;i++){for(int j=0;j<m;j++){spt[i][size][j]=max(spt[i][size-1][j],spt[i+(1<<(size-1))][size-1][j]);}}}
    int msf=0;int cnt=0;int bt=0;int left;int right;
    fo(i,n){if (sumofval(cnt,i)<=k){cnt++;msf=sumofval(cnt,i);if (cnt>bt){left=i-cnt+1;right=i;}}else if (msf!=0){msf=sumofval(cnt,i);}bt=max(bt,cnt);}
    if (bt==0){fo(i,m){cout<<0<<" ";}cout<<endl;}
    else{fo(i,m){cout<<sptableval(left,right,i)<<" ";}cout<<endl;}
    return 0;
}


//Ishaan Singh 2019CS10359
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int, int>
#define vpi vector<pii>
const ll M=1e9+7;     // large prime for modulo
using namespace std;
int spt[100001][25][5];
int lo[100001];
int sptableval(int L,int R,int F){
  int j = lo[R - L + 1];
  return max(spt[L][j][F], spt[R - (1 << j) + 1][j][F]);
}
int sumofval(int cnt,int right){
    int su=0;
    fo(i,m){su+=sptableval(right-cnt+1,right,i);}
    return su;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int a[100001][5]
int n,m,k;
    lo[1]=0;for(int i=2;i<=100000;i++){lo[i]=lo[i/2]+1;}
    cin>>n>>m>>k;fo(i,n){fo(j,m){cin>>a[i][j];}}
    fo(i,n){fo(j,m){spt[i][0][j]=a[i][j];}}
        for(int size=1;size<25;size++){for(int i=0;i+(1<<size)<=n;i++){for(int j=0;j<m;j++){spt[i][size][j]=max(spt[i][size-1][j],spt[i+(1<<(size-1))][size-1][j]);}}}
    int msf=0;int cnt=0;int bt=0;int left;int right;
    fo(i,n){if (sumofval(cnt,i)<=k){cnt++;msf=sumofval(cnt,i);if (cnt>bt){left=i-cnt+1;right=i;}}else if (msf!=0){msf=sumofval(cnt,i);}bt=max(bt,cnt);}
    if (bt==0){fo(i,m){cout<<0<<" ";}cout<<endl;}
    else{fo(i,m){cout<<sptableval(left,right,i)<<" ";}cout<<endl;}
    return 0;
}


//Ishaan Singh 2019CS10359
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int, int>
#define vpi vector<pii>
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>
const ll M=1e9+7;     // large prime for modulo
using namespace std;
/*
ll fpow(ll a,ll b) {// function for binary exponentiation
    a %= M;
    ll res= 1;
    while(b>0) {
        if (b&1){
            res = res * a % M;
        }
        a = a * a % M;
        b >>= 1;
    }
    return res;
}
 ll spf[10000000+1];
ll primes[10000000+1]={false};
 void sieve()  // sieve of eratosthenes
 {
     spf[1] = 1;
     primes[2]=true;
     for (ll i=2; i<10000000+1; i++)
         spf[i] = i;
     for(ll i=4;i<10000000+1;i+=2)
         spf[i]=2;
     for (ll i=3; i*i<10000000+1; i+=2)
     {
         if (spf[i] == i)
         {
             primes[i]=true;
             for (ll j=i*i; j<10000000+1; j+=i)
                 if (spf[j]==j)
                     spf[j] = i;
         }
     }
 }
map<ll,ll> gpf(ll n) // generates prime factors of a number in log(n) time with o(nloglogn) preprocessing time
 {
     map<ll,ll>v;
     ll curr = spf[n];
     ll cnt = 1;
     while (n > 1)
     {
         n/= spf[n];
         if (curr == spf[n])
         {
             cnt++;
             continue;
         }
         v.insert({curr,cnt});
         curr = spf[n];
         cnt = 1;
     }
     return v;
 }
struct finvic{                  // fenwick tree data structure
    vector<int>fin;
    int n;
    finvic(int n){
        this->n=n;
        fin.assign(n,0);
    }
    finvic(vector<int>a) :finvic(int(a.size())){
        for (int u=0;u<int(a.size());u++){
            add(u,a[u]);
        }
    }
    int sum(int r){
        int res=0;
        for(int i=r;i>=0;i=(i & (i+1))-1){
            res+=fin[i];
        }
        return res;
    }
    int sum(int l,int r){
        return sum(r)-sum(l-1);
    }
    void add(int ind,int val){
        for(int i=ind;i<n;i=(i|(i+1))){
            fin[i]+=val;
        }
    }
};

struct comp // comparison function for a pair of integer and pair
{
    bool operator()(const pair<int, pii> &a, const pair<int, pii> &b)
    {
        if (a.first == b.first)
            return a.second.first > b.second.first;
        return a.first < b.first;
    }
};
ll gcd(ll a,ll b){return b ? gcd(b,a%b) :a;}        // returns gcd of two numbers in logarithmic time
map<ll,ll> prsqrt(ll n){                            // returns prime factors of a number in root n time complexity
    ll cnt=0;map<ll,ll>m;
    while (n % 2 == 0){cnt++;n = n/2;}
    if (cnt!=0){m.insert({2,cnt});}
    for (ll i=3;i<=sqrt(n);i+=2){cnt=0;while (n % i == 0){cnt++;n = n/i;}if(cnt!=0){m.insert({i,cnt});}}
    if (n > 2){m.insert({n,1});}
    return m;

}
ll lpfact(ll n,ll p){ll ans=0;while(n){ans+=n/p;n/=p;}return ans;}
//returns largest power of a prime p in n factorial
vector<int>tin(200000+5);vector<int>low(200000+5);
vector<int>adj[2*100000+5];
vpi bridges;
int timer=0;
bool visited[3*100000+5];
void bridge(int v,int p){                   //finding bridges in O(n+m) time
    visited[v]=true;
    tin[v]=low[v]=timer++;
    for(auto u:adj[v]){if (u==p){continue;}else if (visited[u]){low[v]=min(low[v],tin[u]);return;}
        else{bridge(u,v);low[v]=min(low[v],low[u]);if (low[u]>tin[v]){bridges.pb({u,v});}}}}
*/
int spt[1000000][25][5];
int lo[100001];
int n,m,k;
int sptableval(int L,int R,int F){
  int j = lo[R - L + 1];
  return max(spt[L][j][F], spt[R - (1 << j) + 1][j][F]);
}
int sumofval(int cnt,int right){
    int su=0;
    fo(i,m){su+=sptableval(right-cnt+1,right,i);}
    return su;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    lo[1]=0;for(int i=2;i<=100000;i++){lo[i]=lo[i/2]+1;}
    cin>>n>>m>>k;int a[100001][5];fo(i,n){fo(j,m){cin>>a[i][j];}}
    fo(i,n){fo(j,m){spt[i][0][j]=a[i][j];}}
        for(int size=1;size<25;size++){for(int i=0;i+(1<<size)<=n;i++){for(int j=0;j<m;j++){spt[i][size][j]=max(spt[i][size-1][j],spt[i+(1<<(size-1))][size-1][j]);}}}
    int msf=0;int cnt=0;int bt=0;int left;int right;
    fo(i,n){if (sumofval(cnt+1,i)<=k){cnt++;msf=sumofval(cnt,i);if (cnt>bt){left=i-cnt+1;right=i;}}else if (msf!=0){msf=sumofval(cnt,i);}bt=max(bt,cnt);}
    if (bt==0){fo(i,m){cout<<0<<" ";}cout<<endl;}
    else{fo(i,m){cout<<sptableval(left,right,i)<<" ";}cout<<endl;}
    return 0;
}


//Ishaan Singh 2019CS10359
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int, int>
#define vpi vector<pii>
const ll M=1e9+7;     // large prime for modulo
using namespace std;
int spt[100001][25][5];
int lo[100001];int n,m,k;
int sptableval(int L,int R,int F){
  int j = lo[R - L + 1];
  return max(spt[L][j][F], spt[R - (1 << j) + 1][j][F]);
}
int sumofval(int cnt,int right){
    int su=0;
    fo(i,m){su+=sptableval(right-cnt+1,right,i);}
    return su;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int a[100001][5];
    lo[1]=0;for(int i=2;i<=100000;i++){lo[i]=lo[i/2]+1;}
    cin>>n>>m>>k;fo(i,n){fo(j,m){cin>>a[i][j];}}
    fo(i,n){fo(j,m){spt[i][0][j]=a[i][j];}}
        for(int size=1;size<25;size++){for(int i=0;i+(1<<size)<=n;i++){for(int j=0;j<m;j++){spt[i][size][j]=max(spt[i][size-1][j],spt[i+(1<<(size-1))][size-1][j]);}}}
    int msf=0;int cnt=0;int bt=0;int left;int right;
    fo(i,n){if (sumofval(cnt,i)<=k){cnt++;msf=sumofval(cnt,i);if (cnt>bt){left=i-cnt+1;right=i;}}else if (msf!=0){msf=sumofval(cnt,i);}bt=max(bt,cnt);}
    if (bt==0){fo(i,m){cout<<0<<" ";}cout<<endl;}
    else{fo(i,m){cout<<sptableval(left,right,i)<<" ";}cout<<endl;}
    return 0;
}


//Ishaan Singh 2019CS10359
#include <iostream>
#include<sstream>
#include<vector>
#include<set>
#include<math.h>
#include<bitset>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long int ll;
typedef long double ld;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define fo(i,n) for(int i=0;i<n;i++)
#define PI 3.141592654
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define mp make_pair
#define vb vector<bool>
#define vvb vector<vb>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define pii pair<int, int>
#define vpi vector<pii>
#define unordered_set(T) unordered_set<T, custom_hash>
#define unordered_map(T1, T2) unordered_map<T1, T2, custom_hash>
const ll M=1e9+7;     // large prime for modulo
using namespace std;
/*
ll fpow(ll a,ll b) {// function for binary exponentiation
    a %= M;
    ll res= 1;
    while(b>0) {
        if (b&1){
            res = res * a % M;
        }
        a = a * a % M;
        b >>= 1;
    }
    return res;
}
 ll spf[10000000+1];
ll primes[10000000+1]={false};
 void sieve()  // sieve of eratosthenes
 {
     spf[1] = 1;
     primes[2]=true;
     for (ll i=2; i<10000000+1; i++)
         spf[i] = i;
     for(ll i=4;i<10000000+1;i+=2)
         spf[i]=2;
     for (ll i=3; i*i<10000000+1; i+=2)
     {
         if (spf[i] == i)
         {
             primes[i]=true;
             for (ll j=i*i; j<10000000+1; j+=i)
                 if (spf[j]==j)
                     spf[j] = i;
         }
     }
 }
map<ll,ll> gpf(ll n) // generates prime factors of a number in log(n) time with o(nloglogn) preprocessing time
 {
     map<ll,ll>v;
     ll curr = spf[n];
     ll cnt = 1;
     while (n > 1)
     {
         n/= spf[n];
         if (curr == spf[n])
         {
             cnt++;
             continue;
         }
         v.insert({curr,cnt});
         curr = spf[n];
         cnt = 1;
     }
     return v;
 }
struct finvic{                  // fenwick tree data structure
    vector<int>fin;
    int n;
    finvic(int n){
        this->n=n;
        fin.assign(n,0);
    }
    finvic(vector<int>a) :finvic(int(a.size())){
        for (int u=0;u<int(a.size());u++){
            add(u,a[u]);
        }
    }
    int sum(int r){
        int res=0;
        for(int i=r;i>=0;i=(i & (i+1))-1){
            res+=fin[i];
        }
        return res;
    }
    int sum(int l,int r){
        return sum(r)-sum(l-1);
    }
    void add(int ind,int val){
        for(int i=ind;i<n;i=(i|(i+1))){
            fin[i]+=val;
        }
    }
};

struct comp // comparison function for a pair of integer and pair
{
    bool operator()(const pair<int, pii> &a, const pair<int, pii> &b)
    {
        if (a.first == b.first)
            return a.second.first > b.second.first;
        return a.first < b.first;
    }
};
ll gcd(ll a,ll b){return b ? gcd(b,a%b) :a;}        // returns gcd of two numbers in logarithmic time
map<ll,ll> prsqrt(ll n){                            // returns prime factors of a number in root n time complexity
    ll cnt=0;map<ll,ll>m;
    while (n % 2 == 0){cnt++;n = n/2;}
    if (cnt!=0){m.insert({2,cnt});}
    for (ll i=3;i<=sqrt(n);i+=2){cnt=0;while (n % i == 0){cnt++;n = n/i;}if(cnt!=0){m.insert({i,cnt});}}
    if (n > 2){m.insert({n,1});}
    return m;

}
ll lpfact(ll n,ll p){ll ans=0;while(n){ans+=n/p;n/=p;}return ans;}
//returns largest power of a prime p in n factorial
vector<int>tin(200000+5);vector<int>low(200000+5);
vector<int>adj[2*100000+5];
vpi bridges;
int timer=0;
bool visited[3*100000+5];
void bridge(int v,int p){                   //finding bridges in O(n+m) time
    visited[v]=true;
    tin[v]=low[v]=timer++;
    for(auto u:adj[v]){if (u==p){continue;}else if (visited[u]){low[v]=min(low[v],tin[u]);return;}
        else{bridge(u,v);low[v]=min(low[v],low[u]);if (low[u]>tin[v]){bridges.pb({u,v});}}}}
*/
int spt[1000000][25][5];
int lo[100001];
int n,m,k;
int sptableval(int L,int R,int F){
  int j = lo[R - L + 1];
  return max(spt[L][j][F], spt[R - (1 << j) + 1][j][F]);
}
int sumofval(int cnt,int right){
    int su=0;
    fo(i,m){su+=sptableval(right-cnt+1,right,i);}
    return su;
}
int main()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    lo[1]=0;for(int i=2;i<=100000;i++){lo[i]=lo[i/2]+1;}
    cin>>n>>m>>k;int a[100001][5];fo(i,n){fo(j,m){cin>>a[i][j];}}
    fo(i,n){fo(j,m){spt[i][0][j]=a[i][j];}}
        for(int size=1;size<25;size++){for(int i=0;i+(1<<size)<=n;i++){for(int j=0;j<m;j++){spt[i][size][j]=max(spt[i][size-1][j],spt[i+(1<<(size-1))][size-1][j]);}}}
    int msf=0;int cnt=0;int bt=0;int left;int right;
    fo(i,n){if (sumofval(cnt,i)<=k){cnt++;msf=sumofval(cnt,i);if (cnt>bt){left=i-cnt+1;right=i;}}else if (msf!=0){msf=sumofval(cnt,i);}bt=max(bt,cnt);}
    if (bt==0){fo(i,m){cout<<0<<" ";}cout<<endl;}
    else{fo(i,m){cout<<sptableval(left,right,i)<<" ";}cout<<endl;}
    return 0;
}


<?php
list($a, $b) = explode(" ", trim(fgets(STDIN)));
$c = array(); // сообщение
$g = array(); // время
$l = array(); // друзья
$m = 0; // пар друзей
for($x = 1; $x <= $a; $x++)
{
    list($d, $e, $f) = explode(" ", trim(fgets(STDIN)));
    array_push($c, $d . " " . $e);
    array_push($g, $f);
}
for($x = 0; $x < count($c); $x++)
{
    for($y = 0; $y < count($c); $y++)
    {
        if($x == $y)
        {
            continue;
        }
        else
        {
            list($h, $i) = explode(" ", $c[$y]);
            $j = $i . " " . $h;
            if($c[$x] == $j)
            {
                $k = $g[$y] - $g[$x];
                if(($k > 0) && ($k <= $b))
                {
                    $m++;
                    array_push($l, $j);
                }
            }
        }
    }
}
if($l == TRUE)
{
    print $m . "\n";
}
else
{
    print $m;
}
for($x = 0; $x < count($l); $x++)
{
    if($x == count($l) - 1)
    {
        print $l[$x];
    }
    else
    {
        print $l[$x] . "\n";
    }
}
?>


<?php
function CheckU($a, $b, $Frends){
    $FrendsCount = count($Frends);

    for($i = 0; $i < $FrendsCount; $i++){
        if((($Frends[$i]['a'] == $a) and ($Frends[$i]['b'] == $b)) or
          (($Frends[$i]['a'] == $b) and ($Frends[$i]['b'] == $a))){
            return false;
        }
    }

    return true;
}

$InStr = preg_replace('~[^0-9, ]~', '', fgets(STDIN));
list($n, $d) = explode(' ', $InStr);

$Mes = array();
for($i = 0; $i < $n; $i++){
    $InStr = preg_replace('~[^0-9,a-z, ]~', '', fgets(STDIN));
    list($sender, $to, $time) = explode(' ', $InStr);

    $Mes[$i]['sender'] = $sender;
    $Mes[$i]['to']     = $to;
    $Mes[$i]['time']   = $time;
}

$Frends = array();
$FrendsCount = 0;
for($i = 0; $i < $n; $i++){
    for($j = $i+1; $j < $n; $j++){
        if(($Mes[$i]['sender'] == $Mes[$j]['to']) and
           ($Mes[$i]['to'] == $Mes[$j]['sender']) and
           ($Mes[$j]['time'] - $Mes[$i]['time'] > 0) and
           ($Mes[$j]['time'] - $Mes[$i]['time'] <= $d) and
           (CheckU($Mes[$i]['sender'], $Mes[$i]['to'], $Frends))){

           $Frends[$FrendsCount]['a'] = $Mes[$i]['sender'];
           $Frends[$FrendsCount]['b'] = $Mes[$i]['to'];

           $FrendsCount++;

           break;
        }
    }
}

echo $FrendsCount . "\r\n";
for($i = 0; $i < $FrendsCount; $i++){
    echo $Frends[$i]['a'] . ' ' . $Frends[$i]['b'] . "\r\n";
}
?>


<?php
$n = explode(' ', trim(fgets(STDIN)));
$d = $n[1];
$n = $n[0];

$arr = array();
$result = array();
for($i = 0; $i < $n; $i++)
{
	$tmp = explode(' ', trim(fgets(STDIN)));
	$A = $tmp[0];
	$B = $tmp[1];
	$t = $tmp[2];

	$found = false;
	if(count($arr) > 0)
	{
		foreach($arr as $user)
		{
			if($B == $user[0] && $user[1] == $A)
			{
				if($t - $user[2] <= $d)
				{
					$result[] = $A.' '.$B;
				}
			}
		}
	}

	$arr[] = array($A, $B, $t);
}

$count = count($result);

$txt = '';
$pair = array();
$cnt = 0;

for($i = 0; $i < $count; $i++)
{
	if(!in_array($result[$i], $pair))
	{
		$txt.= "\n".$result[$i];
		$pair[] = $result[$i];
		$cnt++;
	}
}

echo $cnt;
if($cnt > 0)
	echo $txt;
?>


<?php
$n = explode(' ', trim(fgets(STDIN)));
$d = $n[1];
$n = $n[0];

$arr = array();
$result = array();
for($i = 0; $i < $n; $i++)
{
    $tmp = explode(' ', trim(fgets(STDIN)));
    $A = $tmp[0];
    $B = $tmp[1];
    $t = $tmp[2];

    if(count($arr) > 0)
    {
        foreach($arr as $user)
        {
            $diff = $t - $user[2];
            if($B == $user[0] && $A == $user[1] && $diff <= $d && $diff > 0)
            {
                $result[] = $A.' '.$B;
            }
        }
    }

    $arr[] = array($A, $B, $t);
}

$count = count($result);

$txt = '';
$pair = array();
$cnt = 0;

for($i = 0; $i < $count; $i++)
{
    $exp = explode(' ', $result[$i]);
    if(!in_array($exp[0].' '.$exp[1], $pair) && !in_array($exp[1].' '.$exp[0], $pair))
    {
        $txt.= "\n".$result[$i];
        $pair[] = $result[$i];
        $cnt++;
    }
}

echo $cnt;
if($cnt > 0)
    echo $txt;
?>


<?php
$n = explode(' ', trim(fgets(STDIN)));
$d = $n[1];
$n = $n[0];

$arr = array();
$result = array();
for($i = 0; $i < $n; $i++)
{
	$tmp = explode(' ', trim(fgets(STDIN)));
	$A = $tmp[0];
	$B = $tmp[1];
	$t = $tmp[2];

	$found = false;
	if(count($arr) > 0)
	{
		foreach($arr as $user)
		{
			if($B == $user[0] && $user[1] == $A)
			{
				if($t - $user[2] <= $d)
				{
					$result[] = $A.' '.$B;
				}
			}
		}
	}

	$arr[] = array($A, $B, $t);
}

$count = count($result);
echo $count;

if($count > 0)
{
	$pair = array();
	for($i = 0; $i < $count; $i++)
	{
		if(!in_array($result[$i], $pair))
		{
			echo "\n".$result[$i];
			$pair[] = $result[$i];
		}
	}
}
?>


<?php
$n = explode(' ', trim(fgets(STDIN)));
$d = $n[1];
$n = $n[0];

$arr = array();
$result = array();
for($i = 0; $i < $n; $i++)
{
	$tmp = explode(' ', trim(fgets(STDIN)));
	$A = $tmp[0];
	$B = $tmp[1];
	$t = $tmp[2];

	$found = false;
	if(count($arr) > 0)
	{
		foreach($arr as $user)
		{
			if($B == $user[0] && $user[1] == $A)
			{
				if($t - $user[2] <= $d)
				{
					$result[] = $A.' '.$B;
				}
			}
		}
	}

	$arr[] = array($A, $B, $t);
}

$count = count($result);

$txt = '';
$pair = array();
$cnt = 0;

for($i = 0; $i < $count; $i++)
{
	$exp = explode(' ', $result[$i]);
	if(!in_array($exp[0].' '.$exp[1], $pair) && !in_array($exp[1].' '.$exp[0], $pair))
	{
		$txt.= "\n".$result[$i];
		$pair[] = $result[$i];
		$cnt++;
	}
}

echo $cnt;
if($cnt > 0)
	echo $txt;
?>


<?php
$fileOpen = fopen('php://stdin', 'r');
$lines = stream_get_contents($fileOpen);

/*   $lines = '10 2
   fxn ipntr 0
   ipntr fxn 1
   fxn ipntr 1
   pfvpfteadph ipntr 2
   fxn pfvpfteadph 4
   ipntr fxn 4
   pfvpfteadph fxn 5
   fxn pfvpfteadph 5
   pfvpfteadph ipntr 6
   ipntr pfvpfteadph 6';

$lines = '10 2
liazxawm spxwktiqjgs 0
nolq liazxawm 1
liazxawm nolq 2
liazxawm spxwktiqjgs 2
nolq liazxawm 3
spxwktiqjgs liazxawm 3
spxwktiqjgs liazxawm 3
spxwktiqjgs liazxawm 3
spxwktiqjgs nolq 3
nolq spxwktiqjgs 4';
*/
$data = explode("\n", trim($lines) );
$firstLine = explode(" " , $data[0]);
$n = $firstLine[0];
$d = $firstLine[1];
$list = array();
$result = array();
for($i = 1 ; $i <= $n ; $i++)
{
    $friend = explode(" ", trim($data[$i]));
    $list[] = array("first" => $friend[0], "second" => $friend[1], "time" => $friend[2]);
    for($j = 0 ; $j < $i ; $j++)
    {
        if($list[$j]["first"] == $list[$i-1]["second"] && $list[$j]["second"] == $list[$i-1]["first"] && ($list[$i-1]["time"] - $list[$j]["time"]) <= $d && ($list[$i-1]["time"] - $list[$j]["time"]) > 0 )
        {
            $flag = false;
            for($k = 0 ; $k < count($result) ; $k++)
            {
                if($result[$k]["first"] == $list[$i-1]["first"] && $result[$k]["second"] == $list[$i-1]["second"])
                {
                    $flag = true;
                    break;
                }
                else if($result[$k]["second"] == $list[$i-1]["first"] && $result[$k]["first"] == $list[$i-1]["second"])
                {
                    $flag = true;
                    break;
                }
            }
            if(!$flag)
            {
                $result[] = array("first" => $list[$i-1]["first"], "second" => $list[$i-1]["second"]);
            }
        }
    }
}
echo count($result)."\n";
for($i = 0 ; $i < count($result) ; $i++)
{
    echo $result[$i]["first"].' '.$result[$i]["second"];
    if($i != (count($result)-1)) echo "\n";
}
?>


<?php
$count = fgets(STDIN);
while ($s = fgets(STDIN))
{
    $friends[] = explode(" ", $s);
}
$param = explode(" ", $count);
$num = 0;
for ($i = 0; $i < $param[0]; $i++)
{
    for ($j = 1; $j < $param[0]; $j++)
    {
        $new_buf = $friends[$i][0] . " " . $friends[$i][1];
        $new_buf1 = $friends[$j][0] . " " . $friends[$j][1];

        if ($new_buf == $buf or $new_buf1 == $buf1)
        {
            unset($friends[$j]);
        }
        if ($friends[$i][0] == $friends[$j][1] and $friends[$i][1] == $friends[$j][0])
        {
            $buf = $friends[$i][0] . " " . $friends[$i][1];
            $buf1 = $friends[$j][0] . " " . $friends[$j][1];
            $razn = $friends[$j][2] - $friends[$i][2];
            if ($razn > 0 and $razn <= $param[1])
            {
                $itogfr[] = $friends[$i][1] . " " . $friends[$j][1];
                $num++;
                unset($friends[$j]);
                break;
            }
        }
    }

}
echo $num;
for ($i = 0; $i < count($itogfr); $i++)
{
    echo "\n" . $itogfr[$i];
}

?>


<?php
$count = fgets(STDIN);
while ($s = fgets(STDIN))
{
    $friends[] = explode(" ", $s);
}
$param = explode(" ", $count);
$num = 0;
for ($i = 0; $i < $param[0]; $i++)
{
    for ($j = 1; $j < $param[0]; $j++)
    {
        if ($friends[$i][0] == $friends[$j][1] and $friends[$i][1] == $friends[$j][0])
        {
            $razn = $friends[$j][2] - $friends[$i][2];
            if ($razn > 0 and $razn <= $param[1])
            {
                $itogfr[] = $friends[$i][1] . " " . $friends[$j][1];
                $num++;
                unset($friends[$j]);
                break;
            }
        }
    }

}
echo $num;
for ($i = 0; $i < count($itogfr); $i++)
{
    echo "\n" . $itogfr[$i];
}

?>


<?php
$count = fgets(STDIN);
while ($s = fgets(STDIN))
{
    $friends[] = explode(" ", $s);
}
$param = explode(" ", $count);
$num = 0;
for ($i = 0; $i < $param[0]; $i++)
{
    for ($j = 1; $j < $param[0]; $j++)
    {
        if ($friends[$i][0] == $friends[$j][1] and $friends[$i][1] == $friends[$j][0])
        {
            $razn = $friends[$i + 1][2] - $friends[$i][2];
            if ($razn > 0 and $razn <= $param[1])
            {
                $itogfr[] = $friends[$i][1] . " " . $friends[$j][1];
                $num++;
                unset($friends[$j]);
                break;
            }
        }
    }

}
echo $num;
for ($i = 0; $i < count($itogfr); $i++)
{
    echo "\n" . $itogfr[$i];
}

?>


<?php
$count = fgets(STDIN);
while ($s = fgets(STDIN))
{
    $friends[] = explode(" ", $s);
}
$param = explode(" ", $count);
$num = 0;
for ($i = 0; $i < $param[0]; $i++)
{
    for ($j = 1; $j < $param[0]; $j++)
    {
        if ($friends[$i][0] == $friends[$j][1] and $friends[$i][1] == $friends[$j][0])
        {
            $razn = $friends[$i + 1][2] - $friends[$i][2];
            if ($razn > 0 and $razn <= $param[1])
            {
                $itogfr[] = $friends[$i][1] . " " . $friends[$i + 1][1];
                $num++;
                break;
            }
        }
    }

}
echo $num;
for ($i = 0; $i < count($itogfr); $i++)
{
    echo "\n" . $itogfr[$i];
}

?>


<?php
$count = fgets(STDIN);
while ($s = fgets(STDIN))
{
    $friends[] = explode(" ", $s);
}
$param = explode(" ", $count);
$num = 0;
for ($i = 0; $i < $param[0]; $i++)
{
    for ($j = 1; $j < $param[0]; $j++)
    {
        if ($friends[$i][0] == $friends[$j][1] and $friends[$i][1] == $friends[$j][0])
        {
            $razn = $friends[$i + 1][2] - $friends[$i][2];
            if ($razn > 0 and $razn <= $param[1])
            {
                $itogfr[] = $friends[$i][1] . " " . $friends[$i + 1][1];
                $num++;
                unset($friends[$j]);
                break;
            }
        }
    }

}
echo $num;
for ($i = 0; $i < count($itogfr); $i++)
{
    echo "\n" . $itogfr[$i];
}

?>


<?php
$count = fgets(STDIN);
while ($s = fgets(STDIN))
{
    $friends[] = explode(" ", $s);
}
$param = explode(" ", $count);
$num = 0;
for ($i = 0; $i < $param[0]; $i++)
{
    if ($friends[$i][0] == $friends[$i + 1][1] and $friends[$i][1] == $friends[$i + 1][0])
    {
        $razn = $friends[$i + 1][2] - $friends[$i][2];
        if ($razn > 0 and $razn <= $param[1])
        {
            $itogfr[] = $friends[$i][1] . " " . $friends[$i + 1][1];
            $num++;
        }
    }

}
echo $num;
for ($i = 0; $i < count($itogfr); $i++)
{
    echo "\n" . $itogfr[$i];
}

?>


<?php
$s = fopen("php://stdin", 'r');
$data_str = str_replace("\r","\n",stream_get_contents($s));
$data_str = str_replace("\n\n","\n",$data_str);
$data = explode("\n", $data_str);
$fl = isset($data[0]) ? explode(' ', $data[0]) : '0';

$n = $fl[0]; $d = $fl[1];
unset($data[0]);
$friends = array();
foreach ($data as $row){
    $message1 = explode(' ', $row);
    foreach ($data as $row1){
        $message2 = explode(' ', $row1);
        $r = abs($message2[2]-$message1[2]);
        if ($message1[0] == $message2[1] && $message1[1] == $message2[0] && 0 < $r && $r <= $d){
            $f = array($message1[0], $message1[1]);
            $f1 = array($message1[1], $message1[0]);
            if (!in_array($f, $friends) && !in_array($f1, $friends)){
                $friends[] = $f;
            }
        }
    }
}

echo $cnt = count($friends);
echo "\n";
foreach ($friends as $k => $f){
    echo $f[0] . ' ' . $f[1];
    if ( $k != $cnt - 1 ){
        echo "\n";
    }
}


<?php
$base = array();
$n = '';
$d = '';

for($i = 0; ; $i++)
{
	$line = fgets(STDIN);
	if($i == 0)
	{
		$first = explode(' ', $line);
		$n = $first[0];
		$d = $first[1];
	} else {
		$line = trim($line);
		$base[] = explode(' ', $line);
	}

	if($i == $n)
		break;
}

//var_dump($base);
$friends = array();
$there_is = false;

for($i = 0; $i < count($base); $i++)
	for($j = 0; $j < count($base); $j++)
		if($base[$i][0] == $base[$j][1] && $base[$i][1] == $base[$j][0] && ((($base[$j][2] - $base[$i][2]) <= $d) && ($base[$j][2] - $base[$i][2]) > 0))
		{
			foreach($friends as $pair)
				if(($pair[0] == $base[$i][0] && $pair[1] == $base[$i][1]) || ($pair[0] == $base[$i][1] && $pair[1] == $base[$i][0]))
					$there_is = true;
			if($there_is != true)
				$friends[] = array($base[$i][1], $base[$i][0]);
		}

echo count($friends) . "\n";
foreach($friends as $string)
	echo implode(' ', $string) . "\n";
?>


<?php
$base = array();
$n = '';
$d = '';

for($i = 0; ; $i++)
{
	$line = fgets(STDIN);
	if($i == 0)
	{
		$first = explode(' ', $line);
		$n = $first[0];
		$d = $first[1];
		if($n < 1 || $n > 1000 || $d < 1 || $d > 1000)
			exit(1);
	} else {
		$line = trim($line);
		$base[] = explode(' ', $line);
		$end = end($base);
		if(iconv_strlen($end[0]) > 20 || iconv_strlen($end[1]) > 20)
			exit(1);
	}

	if($i == $n)
		break;
}

//var_dump($base);
$friends = array();
$there_is = false;

for($i = 0; $i < count($base); $i++)
	for($j = 0; $j < count($base); $j++)
		if($base[$i][0] == $base[$j][1] && $base[$i][1] == $base[$j][0] && ((($base[$j][2] - $base[$i][2]) <= $d) && ($base[$j][2] - $base[$i][2]) > 0) && ($base[$i][0] != $base[$i][1]) && $base[$j][2] <= $n)
		{
			foreach($friends as $pair)
				if(($pair[0] == $base[$i][0] && $pair[1] == $base[$i][1]) || ($pair[0] == $base[$i][1] && $pair[1] == $base[$i][0]))
					$there_is = true;
			if($there_is != true)
				$friends[] = array($base[$i][1], $base[$i][0]);
		}

echo count($friends) . "\n";
foreach($friends as $string)
	echo implode(' ', $string) . "\n";

?>


<?php
$base = array();
$n = '';
$d = '';

for($i = 0; ; $i++)
{
	$line = fgets(STDIN);
	if($i == 0)
	{
		$first = explode(' ', $line);
		$n = $first[0];
		$d = $first[1];
	} else {
		if($i == $n+1)
			break;
		$line = trim($line);
		$base[] = explode(' ', $line);
	}
}

$friends = array();

for($i = 0; $i < count($base); $i++)
	for($j = 0; $j < count($base); $j++)
		if($base[$i][0] == $base[$j][1] && $base[$i][1] == $base[$j][0] && ((($base[$j][2] - $base[$i][2]) <= $d) && ($base[$j][2] - $base[$i][2]) > 0))
			$friends[] = $base[$i][1] . ' ' . $base[$i][0] . "\n";

echo count($friends) . "\n";
foreach($friends as $string)
	echo $string;
?>


<?php

$stdin = fopen("php://stdin", "r");
$line1 = trim(fgets($stdin));
$line1Array = explode(' ', $line1);
$n = (int)$line1Array[0];
$d = (int)$line1Array[1];

$messages = array();
for($i = 0; $i < $n; $i++) {
    $messages[$i] = trim(fgets($stdin));
    $messages[$i] = explode(' ', $messages[$i]);
}

$cnt = 0;
$results = array();

for($i = 0; $i < $n; $i++) {

    $name1 = $messages[$i][0];
    $name2 = $messages[$i][1];
    $time = $messages[$i][2];
    for($j = 0; $j < $n; $j++) {
        if($j == $i)continue;

        $str1 = "$name1 ".$messages[$j][0];
        $str2 = $messages[$j][0]." $name1";
        if($messages[$j][1] == $name1 && $messages[$j][0] == $name2 && !in_array($str1, $results) && !in_array($str2, $results)) {
            //echo $i." ".$j.". ".$messages[$j][0]. " ". $messages[$j][1];
            //echo $i." ".$j." ".$messages[$j][2]." ".$time;
            if(abs($messages[$j][2] - $time) <= $d) {
                $cnt++;
                $results[] = $messages[$j][0]." ".$messages[$j][1];
            }
        }

    }
}

echo $cnt . PHP_EOL;
foreach($results as $r) {
    echo $r . PHP_EOL;
}

/*
16 10000
xxx petya 1
vasya petya 2
anya petya 5
vasya ivan 8
ivan vasya 9
ivan anya 11
petya vasya 13
petya vasya 14
anya ivan 15
ivan anya 18
xxx anya 20
xxx anya 21
anya xxx 24
anya petya 25
anya petya 26
petya anya 28

2 1
a b 1
b a 3

 */

?>


<?php

$stdin = fopen("php://stdin", "r");
$line1 = trim(fgets($stdin));
$line1Array = explode(' ', $line1);
$n = (int)$line1Array[0];
$d = (int)$line1Array[1];

$messages = array();
for($i = 0; $i < $n; $i++) {
    $messages[$i] = trim(fgets($stdin));
    $messages[$i] = explode(' ', $messages[$i]);
}

$cnt = 0;
$results = array();

for($i = 0; $i < $n; $i++) {

    $name1 = $messages[$i][0];
    $name2 = $messages[$i][1];
    $time = $messages[$i][2];
    for($j = 0; $j < $n; $j++) {
        if($j == $i)continue;

        $str1 = "$name1 ".$messages[$j][0];
        $str2 = $messages[$j][0]." $name1";
        if($messages[$j][1] == $name1 && $messages[$j][0] == $name2 && !in_array($str1, $results) && !in_array($str2, $results)) {
            //echo $i." ".$j.". ".$messages[$j][0]. " ". $messages[$j][1];
            //echo $i." ".$j." ".$messages[$j][2]." ".$time;
            if($j > $i)$diffTime = $messages[$j][2] - $time;
            if($i > $j)$diffTime = $time - $messages[$j][2];
            if($diffTime <= $d && $diffTime != 0) {
                $cnt++;
                $results[] = $messages[$j][0]." ".$messages[$j][1];
            }
        }

    }
}

echo $cnt . PHP_EOL;
foreach($results as $r) {
    echo $r . PHP_EOL;
}

/*
16 10000
xxx petya 1
vasya petya 2
anya petya 5
vasya ivan 8
ivan vasya 9
ivan anya 11
petya vasya 13
petya vasya 14
anya ivan 15
ivan anya 18
xxx anya 20
xxx anya 21
anya xxx 24
anya petya 25
anya petya 26
petya anya 28

2 1
a b 1
b a 3

2 1
a b 0
b a 0
 */

?>


<?php
    $line = trim(fgets(STDIN));
    $line_array=explode(" ",$line);
    $n=intval($line_array[0]);
    $d=intval($line_array[1]);

    $friends_array=array();

    $all=array();
    for($i=0;$i<$n;$i++){
        $line = trim(fgets(STDIN));
        $line_array=explode(" ",$line);
        $all[$i]=array();
        $all[$i][0]=$line_array[0];
        $all[$i][1]=$line_array[1];
        $all[$i][2]=$line_array[2];
        $j=$i-1;
        while ($j>=0){
           if ($all[$i][1]==$all[$j][0] && $all[$i][0]==$all[$j][1] && intval($all[$i][2])-intval($all[$j][2])<=$d && intval($all[$i][2])-intval($all[$j][2])>0){
                $flag=0; $z=0;
               while($z<count($friends_array)){
                 if ($friends_array[$z][0]==$all[$i][0] && $friends_array[$z][1]==$all[$i][1]){$flag=1;}
                 if ($friends_array[$z][1]==$all[$i][0] && $friends_array[$z][0]==$all[$i][1]){$flag=1;}
                  $z++;
               }
               if ($flag==0){array_push($friends_array,array($all[$i][0],$all[$i][1]));}
           }
           $j--;
       }
    }
   echo count($friends_array)."\n";
   for ($i=0;$i<count($friends_array);$i++){
       echo $friends_array[$i][0]." ".$friends_array[$i][1]."\n";
   }
?>


<?php
    $line = trim(fgets(STDIN));
    $line_array=explode(" ",$line);
    $n=intval($line_array[0]);
    $d=intval($line_array[1]);

    $friends_array=array();

    $all=array();
    for($i=0;$i<$n;$i++){
        $line = trim(fgets(STDIN));
        $line_array=explode(" ",$line);
        $all[$i]=array();
        $all[$i][0]=$line_array[0];
        $all[$i][1]=$line_array[1];
        $all[$i][2]=$line_array[2];
        $j=$i-1;
        while ($j>=0){
           if ($all[$i][1]==$all[$j][0] && $all[$i][0]==$all[$j][1] && $all[$i][2]-$all[$j][2]<=$d){
                $flag=0; $z=0;
               while($z<count($friends_array)){
                 if ($friends_array[$z][0]==$all[$i][0] && $friends_array[$z][1]==$all[$i][1]){$flag=1;}
                 if ($friends_array[$z][1]==$all[$i][0] && $friends_array[$z][0]==$all[$i][1]){$flag=1;}
                  $z++;
               }
               if ($flag==0){array_push($friends_array,array($all[$i][0],$all[$i][1]));}
           }
           $j--;
       }
    }
   echo count($friends_array)."\n";
   for ($i=0;$i<count($friends_array);$i++){
       echo $friends_array[$i][0]." ".$friends_array[$i][1]."\n";
   }
?>


<?php
list($n, $d) = explode(' ', fgets(STDIN));
$a = array();
$r = array();
for ($i = 0; $i < $n; $i++) {
    $t = explode(' ', fgets(STDIN));
    $a[$t[0]][] = array($t[1], $t[2]);
}
foreach ($a as $sender => $recipients) {
    foreach ($recipients as $recipient) {
        $sendTime = $recipient[1];
        $reciever = $recipient[0];
        if (isset($a[$reciever])) {
            foreach ($a[$reciever] as $hisRecievers) {
                if ($hisRecievers[0] === $sender) {
                    if ((($hisRecievers[1] - $sendTime) > 0) && (($hisRecievers[1] - $sendTime) <= $d)) {
                        if (!isset($r[$reciever . ' ' . $sender]) && !isset($r[$sender . ' ' . $reciever])) {
                            $r[$sender . ' ' . $reciever] = 0;
                        }
                    }
                }
            }
        }
    }
}
echo count($r) . "\n";
foreach ($r as $k => $v) {
    echo $k . "\n";
}


n,d=map(int,input().split())
arr = []
for _ in range(n):
    a,b,c=input().split()
    curr = [a,b,int(c)]
    arr.append(curr)
ans = []
for i in range(n):
    for j in range(i+1,n):
        if abs(arr[i][2]-arr[j][2])<=d and abs(arr[i][2]-arr[j][2])>0:
            if arr[i][0]==arr[j][1] and arr[i][1]==arr[j][0]:
                ans.append((arr[i][1],arr[i][0]))
        elif  abs(arr[i][2]-arr[j][2])>d:
            break
if ans==[]:
    print(0)
else:
    distinct_ans=[]
    for i in ans:
        i=list(i)
        x=i.sort()
        distinct_ans.append(i)
    stack = []
    for i in distinct_ans:
        if i not in stack:
            stack.append(i)
    print(len(stack))
    for i in stack:
        for j in i:
            print(j,end=' ')
        print()


n,d=map(int,input().split())
arr = []
for _ in range(n):
    a,b,c=input().split()
    curr = [a,b,int(c)]
    arr.append(curr)
ans = []
for i in range(n):
    for j in range(i+1,n):
        if abs(arr[i][2]-arr[j][2])<=d and abs(arr[i][2]-arr[j][2])>0:
            if arr[i][0]==arr[j][1] and arr[i][1]==arr[j][0]:
                ans.append((arr[i][1],arr[i][0]))
        elif  abs(arr[i][2]-arr[j][2])>d:
            break
if ans==[]:
    print(0)
else:
    distinct_ans=[]
    for i in ans:
        i=list(i)
        x=i.sort()
        distinct_ans.append(i)
    for i in range(1,len(distinct_ans)):
        if distinct_ans[i][0]==distinct_ans[i-1][0] and  distinct_ans[i][1]==distinct_ans[i-1][1]:
            distinct_ans.remove(distinct_ans[i])
            break
    for i in range(1,len(distinct_ans)):
        if distinct_ans[i][0]==distinct_ans[i-1][0] and  distinct_ans[i][1]==distinct_ans[i-1][1]:
            distinct_ans.remove(distinct_ans[i])
            break
    for i in range(1,len(distinct_ans)):
        if distinct_ans[i][0]==distinct_ans[i-1][0] and  distinct_ans[i][1]==distinct_ans[i-1][1]:
            distinct_ans.remove(distinct_ans[i])
            break
    for i in range(1,len(distinct_ans)):
        if distinct_ans[i][0]==distinct_ans[i-1][0] and  distinct_ans[i][1]==distinct_ans[i-1][1]:
            distinct_ans.remove(distinct_ans[i])
            break
    for i in range(1,len(distinct_ans)):
        if distinct_ans[i][0]==distinct_ans[i-1][0] and  distinct_ans[i][1]==distinct_ans[i-1][1]:
            distinct_ans.remove(distinct_ans[i])
            break
    for i in range(1,len(distinct_ans)):
        if distinct_ans[i][0]==distinct_ans[i-1][0] and  distinct_ans[i][1]==distinct_ans[i-1][1]:
            distinct_ans.remove(distinct_ans[i])
            break
    for i in range(1,len(distinct_ans)):
        if distinct_ans[i][0]==distinct_ans[i-1][0] and  distinct_ans[i][1]==distinct_ans[i-1][1]:
            distinct_ans.remove(distinct_ans[i])
            break
    print(len(distinct_ans))
    for i in distinct_ans:
        for j in i:
            print(j,end=' ')
        print()


n,d=map(int,input().split())
sen=[]
re=[]
ti=[]
for i in range(n):
    ss=input()
    s=ss.split()
    sen.append(s[0])
    re.append(s[1])
    ti.append(s[2])
m=[]
count=0
for i in range(len(sen)):
    if sen[i] in re:
        if 0<int(ti[i])-int(ti[re.index(sen[i])])<=d:
            m.append(str(sen[i]+" "+re[i]))
            count+=1
print(count)
for k in m:
    print(k)


# n=int(input())
#q.sort(key=lambda x:((x[1]-x[0]),-x[0]))
# n,k=map(int,input().split())
# arr=list(map(int,input().split()))
#ls=list(map(int,input().split()))
#for i in range(m):
#from sys import stdin
#n=int(stdin.readline())
#for _ in range(int(input())):
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
##########################################################
import sys
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n) :
    p,q,t=map(str,sys.stdin.readline().split())
    t=int(t)
    l.append([[p,q],t])
#print(l)
p=[]
for i in range(n) :
    for j in range(i+1,n) :
        if l[j][1]-l[i][1] >k :
            break
        if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 and l[i][0]!=l[j][0]:
            #print(l[i][0])
            if l[i][0] not in p and l[i][0][::-1] not in p :
                 p.append(l[i][0])
print(len(p))
if len(p)==0 :
    exit(0)
for i in p:
    print(i[0],i[1])


n,d=map(int,input().split())
sen=[]
re=[]
ti=[]
for i in range(n):
    ss=input()
    s=ss.split()
    sen.append(s[0])
    re.append(s[1])
    ti.append(s[2])
m=[]
count=0
for i in range(len(sen)):
    if sen[i] in re:
        b=re.index(sen[i])
        if 0<int(ti[i])-int(ti[re.index(sen[i])])<=d and re[i]==sen[int(b)]:
            m.append(str(sen[i]+" "+re[i]))
            count+=1
print(count)
for k in m:
    print(k)


n,d=map(int,input().split())
sen=[]
re=[]
ti=[]
for i in range(n):
    ss=input()
    s=ss.split()
    sen.append(s[0])
    re.append(s[1])
    ti.append(s[2])
m=[]
count=0
for i in range(len(sen)):
    if sen[i] in re:
        b=re.index(sen[i])
        if re[i]==sen[int(b)]:
            if  0 < int(ti[i]) - int(ti[re.index(sen[i])]) <= d:
                if str(sen[i]+" "+re[i]) not in m and str(re[i]+" "+sen[i]) not in m:
                    m.append(str(sen[i]+" "+re[i]))
                    count+=1
print(count)
for k in m:
    print(k)


import sys
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n) :
    p,q,t=map(str,sys.stdin.readline().split())
    t=int(t)
    l.append([[p,q],t])
p=[]
for i in range(n) :
    for j in range(i+1,n) :
        if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 :
            #print(l[i][0])
            p.append(l[i][0])
            #print(p)
abc=[]
for i in range(len(p)):
    for j in range(i+1,len(p)):
        if  Counter(p[i])!=Counter(p[j]):
            abc.append(p[i])
abc.append(p[len(p)-1])
print(len(abc))
for i in abc :
    print(i[0],i[1])


import sys
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n) :
    p,q,t=map(str,sys.stdin.readline().split())
    t=int(t)
    l.append([[p,q],t])
p=[]
for i in range(n) :
    for j in range(i+1,n) :
        if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 :
            #print(l[i][0])
            p.append(l[i][0])
            #print(p)
abc=[]
for i in range(len(p)):
    for j in range(i+1,len(p)):
        if  Counter(p[i])!=Counter(p[j]):
            abc.append(p[i])
if p==[] :
    exit(print("0"))
abc.append(p[len(p)-1])
print(len(abc))
for i in abc :
    print(i[0],i[1])


import sys
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n) :
    p,q,t=map(str,sys.stdin.readline().split())
    t=int(t)
    l.append([[p,q],t])
#print(l)
p=[]
for i in range(n) :
    for j in range(i+1,n) :
        if l[j][1]-l[i][1] >k :
            break
        if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 :
            #print(l[i][0])
            if l[i][0] not in p and l[i][0][::-1] not in p :
                 p.append(l[i][0])
print(len(p))
if len(p)==0 :
    exit(0)
for i in p:
    print(i[0],i[1])


import sys
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n) :
    p,q,t=map(str,sys.stdin.readline().split())
    t=int(t)
    l.append([[p,q],t])
total=0
for i in range(n) :
    for j in range(i+1,n) :
        if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 :
            total = total + 1
print(total)


import sys
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n) :
    p,q,t=map(str,sys.stdin.readline().split())
    t=int(t)
    l.append([[p,q],t])
#print(l)
p=[]
for i in range(n) :
    for j in range(i+1,n) :
        if l[j][1]-l[i][1] >k :
            break
        if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 and l[j]!=l[i]:
            #print(l[i][0])
            if l[i][0] not in p and l[i][0][::-1] not in p :
                 p.append(l[i][0])
print(len(p))
if len(p)==0 :
    exit(0)
for i in p:
    print(i[0],i[1])


import sys
from collections import Counter
n,k=map(int,input().split())
l=[]
for i in range(n) :
    p,q,t=map(str,sys.stdin.readline().split())
    t=int(t)
    l.append([[p,q],t])
#print(l)
p=[]
for i in range(n) :
    for j in range(i+1,n) :
        if l[j][1]-l[i][1] >k :
            break
        if Counter(l[i][0]) == Counter(l[j][0]) and l[j][1]-l[i][1]<=k and l[j][1]-l[i][1] >0 and l[i][0]!=l[j][0]:
            #print(l[i][0])
            if l[i][0] not in p and l[i][0][::-1] not in p :
                 p.append(l[i][0])
print(len(p))
if len(p)==0 :
    exit(0)
for i in p:
    print(i[0],i[1])


#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <set>
#include <hash_map>
#include <functional>
#include <sstream>
#include <stack>
using namespace std;

const double eps = 1e-5;

int main()
{
	int n = 0;
	cin >> n;
	int max_h = 0;
	int max_h_i = -1;
	vector<int> heights( n );
	for (int i = 0; i < n; i++) {
		cin >> heights[i];
		if (heights[i] > max_h) {
			max_h = heights[i];
			max_h_i = i;
		}
	}
	vector<int> temp_heights;
	for (int j = max_h_i; j < heights.size(); j++) {
		temp_heights.push_back(heights[j]);
	}
	for (int j = 0; j < max_h_i; j++) {
		temp_heights.push_back(heights[j]);
	}
	heights.swap(temp_heights);
	stack<pair<int, int>> s;
	s.push(make_pair(heights.front(), 1));
	__int64 total = 0;
	int total_max = 0;
	for (int i = 1; i < heights.size(); i++) {
		while (!s.empty() && s.top().first < heights[i])
		{
			total+=s.top().second;
			s.pop();
		}
		if (!s.empty() && s.top().first == heights[i]) {
			total += s.top().second;
			s.top().second++;
			if (s.size() >= 2) {
				total++;
			}
		} else if (!s.empty() && s.top().first > heights[i]) {
			total++;
			s.push(make_pair(heights[i], 1));
		}
	}
	while (!s.empty() && s.top().first != max_h) {
		int count = s.top().second;
		s.pop();
		if (s.top().first != max_h || s.top().second > 1) {
			total += count;
		}
	}
	cout << total;
	return 0;
}


#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include <set>
#include <hash_map>
#include <functional>
#include <sstream>
#include <stack>
using namespace std;

const double eps = 1e-5;

int main()
{
	int n = 0;
	cin >> n;
	int max_h = 0;
	int max_h_i = -1;
	vector<int> heights( n );
	for (int i = 0; i < n; i++) {
		cin >> heights[i];
		if (heights[i] > max_h) {
			max_h = heights[i];
			max_h_i = i;
		}
	}
	vector<int> temp_heights;
	for (int j = max_h_i; j < heights.size(); j++) {
		temp_heights.push_back(heights[j]);
	}
	for (int j = 0; j < max_h_i; j++) {
		temp_heights.push_back(heights[j]);
	}
	heights.swap(temp_heights);

	stack<pair<int, int>> s;
	s.push(make_pair(heights.front(), 1));
	int total = 0;
	int total_max = 0;
	for (int i = 1; i < heights.size(); i++) {
		while (!s.empty() && s.top().first < heights[i])
		{
			total+=s.top().second;
			s.pop();
		}
		if (!s.empty() && s.top().first == heights[i]) {
			total += s.top().second;
			s.top().second++;
			if (s.size() >= 2) {
				total++;
			}
		} else if (!s.empty() && s.top().first > heights[i]) {
			total++;
			s.push(make_pair(heights[i], 1));
		}
	}
	if (s.size() > 2) {
		if (s.top().first != max_h) {
			total += s.top().second;
		}
		else {
			if (s.size() != heights.size()) {
				total++;
			}
		}
	}
	else if (s.size() == 2) {
		int count = s.top().second;
		s.pop();
		if (s.top().second > 1) {
			total += count;
		}
	}
	cout << total;
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:16777216")
using namespace std;

#define bit(n) (1<<(n))
#define inf 1000000000
#define eps 1e-9
#define PI 3.1415926535897932385
#define pb push_back
#define sz size()
#define mp make_pair
#define cl clear()
#define all(a) a.begin(),a.end()
#define fill(ar,val) memset(ar,val,sizeof ar)
#define MIN(a,b) if(a>(b)) a=(b)
#define MAX(a,b) if(a<(b)) a=(b)
#define sqr(x) ((x)*(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

typedef vector<int> VI;
typedef pair<int,int> PII;
typedef __int64 LL;

#define N 1010101

int a[2*N];
int L[2*N];
int R[2*N];
map<int,int> ind;
VI lst[N];

int lst_fnd(int k,int x)
{
	int l=-1;
	int r=lst[k].sz;
	while(l+1<r)
	{
		int m=(l+r)/2;
		if(lst[k][m]<x) l=m; else r=m;
	}
	return r;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d",&n)>0)
	{
		ind.cl;
		int cnt=0;
		int i,j,k;
		for(i=1;i<=n;i++) lst[i].cl;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			a[n+i]=a[i];
			if(!ind[a[i]]) ind[a[i]]=++cnt;
			lst[ind[a[i]]].pb(i);
		}
		for(i=1;i<=cnt;i++)
		{
			int len=lst[i].sz;
			for(j=0;j<len;j++) lst[i].pb(lst[i][j]+n);
		}
		for(i=0;i<2*n;i++)
		{
			for(j=i-1;j>=0 && a[i]>=a[j];j=L[j]);
			L[i]=j;
		}
		for(i=2*n-1;i>=0;i--)
		{
			for(j=i+1;j<2*n && a[i]>=a[j];j=R[j]);
			R[i]=j;
		}
		LL ans1=0;
		LL ans2=0;
		for(i=0;i<n;i++)
			if(R[i]<2*n)
			{
				if(L[n+i]>=n) L[n+i]-=n;
				if(R[i]>=n) R[i]-=n;
				if(L[n+i]==R[i])
				{
					ans1++;
					ans2+=lst[ind[a[i]]].sz/2-1;
				}
				else
				{
					ans1+=2;
					if(L[n+i]>i) R[i]+=n; else
					if(R[i]<i) R[i]+=n;
					int k=ind[a[i]];
					int l=lst_fnd(k,L[n+i]);
					int r=lst_fnd(k,R[i]);
					ans2+=r-l-1;
				}
			}
			else ans2+=lst[ind[a[i]]].sz/2-1;
		printf("%I64d\n",ans1+ans2/2);
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:16777216")
using namespace std;

#define bit(n) (1<<(n))
#define inf 1000000000
#define eps 1e-9
#define PI 3.1415926535897932385
#define pb push_back
#define sz size()
#define mp make_pair
#define cl clear()
#define all(a) a.begin(),a.end()
#define fill(ar,val) memset(ar,val,sizeof ar)
#define MIN(a,b) if(a>(b)) a=(b)
#define MAX(a,b) if(a<(b)) a=(b)
#define sqr(x) ((x)*(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

typedef vector<int> VI;
typedef pair<int,int> PII;
typedef __int64 LL;

#define N 1010101

int a[2*N];
int L[2*N];
int R[2*N];
map<int,int> ind;
VI lst[N];

int lst_fnd(int k,int x)
{
	int l=-1;
	int r=lst[k].sz;
	while(l+1<r)
	{
		int m=(l+r)/2;
		if(lst[k][m]<x) l=m; else r=m;
	}
	return r;
}

int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	while(scanf("%d",&n)>0)
	{
		ind.cl;
		int cnt=0;
		int i,j,k;
		for(i=1;i<=n;i++) lst[i].cl;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			a[n+i]=a[i];
			if(!ind[a[i]]) ind[a[i]]=++cnt;
			lst[ind[a[i]]].pb(i);
		}
		for(i=1;i<=cnt;i++)
		{
			int len=lst[i].sz;
			for(j=0;j<len;j++) lst[i].pb(lst[i][j]+n);
		}
		for(i=0;i<2*n;i++)
		{
			for(j=i-1;j>=0 && a[i]>=a[j];j=L[j]);
			L[i]=j;
		}
		for(i=2*n-1;i>=0;i--)
		{
			for(j=i+1;j<2*n && a[i]>=a[j];j=R[j]);
			R[i]=j;
		}
		LL ans1=0;
		LL ans2=0;
		for(i=0;i<n;i++)
			if(R[i]<2*n)
			{
				if(L[n+i]>=n) L[n+i]-=n;
				if(R[i]>=n) R[i]-=n;
				if(L[n+i]==R[i])
				{
					ans1++;
					ans2+=lst[ind[a[i]]].sz/2-1;
				}
				else
				{
					ans1+=2;
					if(L[n+i]>i) R[i]+=n; else
					if(R[i]<i) R[i]+=n;
					int k=ind[a[i]];
					int l=lst_fnd(k,L[n+i]);
					int r=lst_fnd(k,R[i]);
					ans2+=r-l-1;
				}
			}
		printf("%I64d\n",ans1+ans2/2);
	}
	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <numeric>
#include <algorithm>
#include <string>
#include <memory.h>
#include <memory>
#include <functional>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <time.h>
#include <map>
#include <set>
#include <sstream>
#include <queue>

using namespace std;

const int nmax = 1000001;

int main()
{
    int in[ nmax ] , a[ nmax ] , i , n;
	long long ans = 0ll , mx = 0 , c = 0;
	scanf( "%d" , &n );
	for( i = 0 ; i < n ; i++ )
	{
	    scanf( "%d" , &in[ i ] );
	    if( in[ i ] > mx )
	    mx = (long long)in[ i ] , c = 1;
	    else c += ( in[ i ] == mx );
	}
    i = 0;
    while( in[ i ] != mx ) i++;
    for( int j = 0 ; j < n ; j++ )
    {
        a[ j ] = (long long)in[ i++ ];
        if( i >= n ) i = 0;
    }

    int st[ nmax ] , cou[ nmax ] , top = 0;
    for( i = 1 ; i < n ; i++ )
    {
        int h = a[ i ];
        if( h == mx )
        top = 0;
        else
        {
                bool f = true;
                while( top > 0 && f )
                {
                    if( st[ top - 1 ] < h )
                    {
                        ans += (long long)cou[ top - 1 ];
                        top--;
                    }
                    else f = false;
                }
                if( top > 0 )
                {
                    if( st[ top - 1 ] == h )
                    {
                        ans += (long long)cou[ top - 1 ];
                        cou[ top - 1 ]++;
                        if( top > 1 ) ans++;
                    }
                    else
                    {
                        if( top >= 1 ) ans++;
                        st[ top ] = h;
                        cou[ top++ ] = 1;
                    }
                }
                else
                {
                    st[ top ] = h ;
                    cou[ top++ ] = 1;
                }
        }
    }

    ans += (long long)( c * ( c - 1 ) ) / 2;
    int mm;
    bool used[ nmax ];
    memset( used , false , sizeof used );
    //cout << ans << endl;
    if( c == 1 )
    {
        for( i = 1 , mm = 0 ; i < n ; i++ )
        if( a[ i ] >= mm )
        used[ i ] = true , mm = a[ i ];

        for( i = n - 1 , mm = 0 ; i > 0 ; i-- )
        if( a[ i ] >= mm )
        mm = a[ i ] , used[ i ] = true;

        for( int i = 0 ; i < n ; i++ )
        ans += (long long)used[ i ];
    }
    else
    {
        bool f;
        for( i = 0 ; i < n ; i++ )
        if( a[ i ] == mx )
        {
			if( i == 0 )
			{
				mm = 0; f = true;
				for( int j = 1 ; j < n && f ; j++ )
				if( a[ j ] == mx ) f = false;
				else
					if( a[ j ] >= mm )
					ans += 1ll , mm = a[ j ];

				mm = 0; f = true;
				for( int j = n - 1 ; j > 0 && f ; j-- )
				if( a[ j ] == mx ) f = false;
				else
					if( a[ j ] >= mm )
					ans += 1ll , mm = a[ j ];
			}
			else
			{
				mm = 0; f = true;
				for( int j = i + 1 ; j < n && f ; j++ )
				if( a[ j ] == mx ) f = false;
				else
					if( a[ j ] >= mm )
					ans += 1ll , mm = a[ j ];

				mm = 0; f = true;
				for( int j = i - 1 ; j > 0 && f ; j-- )
				if( a[ j ] == mx ) f = false;
				else
					if( a[ j ] >= mm )
					ans += 1ll , mm = a[ j ];
			}
		}
    }
    cout << ans << endl;
    return 0;
}


n = int(input())
hills = list(map(int, input().strip().split()))
max_height, pos_max_height = max((h, i) for i, h in enumerate(hills))
rotation_hill=(hills[pos_max_height:]+hills[:pos_max_height])[1:]
reference_height = max_height
reference_cost = 0
pairs = 0
reference_hills = []
for current_height in rotation_hill:
    while current_height > reference_height:
        pairs += reference_cost
        reference_height, reference_cost = reference_hills.pop()

    if current_height == reference_height:
        reference_cost += 1
        pairs += reference_cost
    else:
        pairs += 1
        reference_hills.append((reference_height, reference_cost))
        reference_height = current_height
        reference_cost = 1

reference_hills.append((reference_height, reference_cost))
start = 1 if reference_hills[0][1] else 2
pairs += sum(reference_hills[i][1] for i in range(start, len(reference_hills)))

print(pairs)


#!/usr/bin/env python

def main():
    n = int(input())
    hill = tuple(map(int, input().split()))
    pairs = 0

    highest, at = max((h, k) for k, h in enumerate(hill))

    last = highest
    count = 0

    previous = list()
    push = previous.append
    pop = previous.pop

    for at in range(at - 1, at - n, -1):
        current = hill[at]
        while current > last:
            pairs += count
            last, count = pop()
        if current == last:
            count += 1
            pairs += count
        else:
            pairs += 1
            push((last, count))
            last = current
            count = 1

    push((last, count))
    end = len(previous)
    pairs += sum(previous[k][1]
                 for k in range((1 if previous[0][1] else 2), end))

    print(pairs)

if __name__ == '__main__':
    main()


"""
Codeforces
5E - Bindian Signalizing
http://codeforces.com/contest/5/problem/E

Héctor González Belver
../07/2018
"""
import sys
import itertools

def rotation(iterable, n, start):
  for i in range(n):
    yield iterable[(i+start)%n]

def main():
  n = int(sys.stdin.readline().strip())
  hills = list(map(int,sys.stdin.readline().strip().split()))
  max_height, pos_max_height = max((h, i) for i, h in enumerate(hills))

  reference_height = max_height
  reference_cost = 0
  pairs = 0
  reference_hills = []
  for current_height in itertools.islice(rotation(hills, n, pos_max_height+1), n-1):

    while current_height > reference_height:
      pairs += reference_cost
      reference_height, reference_cost = reference_hills.pop()

    if current_height == reference_height:
      reference_cost += 1
      pairs += reference_cost
    else:
      pairs += 1
      reference_hills.append((reference_height, reference_cost))
      reference_height = current_height
      reference_cost = 1

  reference_hills.append((reference_height, reference_cost))
  start = 1 if reference_hills[0][1] else 2
  pairs += sum(previous[i][1] for i in range(start, len(reference_hills)))

  sys.stdout.write(str(pairs) + '\n')

if __name__ == '__main__':
  main()


"""
Codeforces
5E - Bindian Signalizing
http://codeforces.com/contest/5/problem/E

Héctor González Belver
../07/2018
"""
import sys

def rotation(iterable, n, start):
  for i in range(n):
    yield iterable[(i+start)%n]

def main():
  n = int(sys.stdin.readline().strip())
  hills = list(map(int,sys.stdin.readline().strip().split()))
  max_height, pos_max_height = max((h, i) for i, h in enumerate(hills))

  reference_height = max_height
  reference_cost = 0
  pairs = 0
  reference_hills = []
  for current_height in rotation(hills, n, pos_max_height+1):

    while current_height > reference_height:
      pairs += reference_cost
      reference_height, reference_cost = reference_hills.pop()

    if current_height == reference_height:
      reference_cost += 1
      pairs += reference_cost
    else:
      pairs += 1
      reference_hills.append((reference_height, reference_cost))
      reference_height = current_height
      reference_cost = 1

  pairs -= max(2, reference_cost)

  sys.stdout.write(str(pairs) + '\n')

if __name__ == '__main__':
  main()


"""
Codeforces
5E - Bindian Signalizing
http://codeforces.com/contest/5/problem/E

Héctor González Belver
../07/2018
"""
import sys
import itertools

def rotation(iterable, n, start):
  for i in range(n):
    yield iterable[(i+start)%n]

def main():
  n = int(sys.stdin.readline().strip())
  hills = list(map(int,sys.stdin.readline().strip().split()))
  max_height, pos_max_height = max((h, i) for i, h in enumerate(hills))

  reference_height = max_height
  reference_cost = 0
  pairs = 0
  reference_hills = []
  for current_height in itertools.islice(rotation(hills, n, pos_max_height+1), n-1):

    while current_height > reference_height:
      pairs += reference_cost
      reference_height, reference_cost = reference_hills.pop()

    if current_height == reference_height:
      reference_cost += 1
      pairs += reference_cost
    else:
      pairs += 1
      reference_hills.append((reference_height, reference_cost))
      reference_height = current_height
      reference_cost = 1

  reference_hills.append((reference_height, reference_cost))
  start = 1 if reference_hills[0][1] else 2
  pairs += sum(reference_hills[i][1] for i in range(start, len(reference_hills)))

  sys.stdout.write(str(pairs) + '\n')

if __name__ == '__main__':
  main()


__author__ = 'Darren'

def solve():
    n = input()
    heights = map(int, raw_input().split(' '))
    max_height, max_index = -1, -1
    for i in range(n):
        if heights[i] > max_height:
            max_height = heights[i]
            max_index = i
    heights = heights[max_index:] + heights[:max_index]
    heights.append(max_height)

    left = [0] * (n+1)
    center = [0] * (n+1)
    right = [0] * (n+1)
    for i in xrange(1, n):
        left[i] = i - 1
        while left[i] > 0 and heights[i] >= heights[left[i]]:
            left[i] = left[left[i]]
    for i in xrange(n-1, -1, -1):
        right[i] = i + 1
        while right[i] < n and heights[i] > heights[right[i]]:
            right[i] = right[right[i]]
        if right[i] < n and heights[i] == heights[right[i]]:
            center[i] = center[right[i]] + 1
            right[i] = right[right[i]]

    total_pair = 0
    for i in xrange(n):
        total_pair += center[i]
        if heights[i] < max_height:
            if left[i] == 0 and right[i] == n:
                total_pair += 1
            else:
                total_pair += 2

    print(total_pair)

if __name__ == '__main__':
    solve()


__author__ = 'Darren'

def solve():
    n = int(input())
    heights = list(map(int, input().split()))
    max_height, max_index = -1, -1
    for i in range(n):
        if heights[i] > max_height:
            max_height = heights[i]
            max_index = i
    heights = heights[max_index:] + heights[:max_index]
    heights.append(max_height)

    left = [0 for i in range(n+1)]
    center = [0 for i in range(n+1)]
    right = [0 for i in range(n+1)]
    for i in range(1, n):
        left[i] = i - 1
        while left[i] > 0 and heights[i] >= heights[left[i]]:
            left[i] = left[left[i]]
    for i in range(n-1, -1, -1):
        right[i] = i + 1
        while right[i] < n and heights[i] > heights[right[i]]:
            right[i] = right[right[i]]
        if right[i] < n and heights[i] == heights[right[i]]:
            center[i] = center[right[i]] + 1
            right[i] = right[right[i]]

    total_pair = 0
    for i in range(n):
        total_pair += center[i]
        if heights[i] < max_height:
            if left[i] == 0 and right[i] == n:
                total_pair += 1
            else:
                total_pair += 2

    print(total_pair)

if __name__ == '__main__':
    solve()