#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n; cin >> n;
    vector<vector<ll>> v(n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < 5; j++){
            int x; cin >> x;
            v[i].push_back(x);
        }
    }
    auto check = [&](int x){
        set<int> s;
        for(int i = 0; i < n; i++){
            int bit = 0;
            for(int j = 0; j < 5; j++){
                if(v[i][j] >= x){
                    bit = bit | (1LL << j);
                }
            }
            s.insert(bit);
        }
        for(auto &i: s){
            for(auto &j: s){
                for(auto &k: s){
                    if((i | j | k) == 31){
                        return true;
                    }
                }
            }
        }
        return false;
    };
    int l = 0, r = 1e9 + 2;
    while(l + 1 < r){
        int mid = l + ((r-l) >> 1);
        if(check(mid)){
            l = mid;
        } else {
            r = mid;
        }
    }
    cout << l << '\n';
}
#include "bits/stdc++.h"
using namespace std ;
const int mxN =800 ;
int n,k,a[mxN+1][mxN+1],p[mxN+1][mxN+1] ;
// ask if we can have median as atleast x or not ?
bool ok(int x){
  for(int i=1;i<n+1;i++)
    for(int j=1;j<n+1;j++)
      p[i][j]=p[i-1][j]+p[i][j-1]-p[i-1][j-1]+(a[i][j]>x) ;
  for(int i=1;i<n+2-k;i++){
    for(int j=1;j<n+2-k;j++){
      int c=p[i+k-1][j+k-1]+p[i-1][j-1]-p[i-1][j+k-1]-p[i+k-1][j-1] ;
      if(c<(k*k)/2+1)
        return 0  ;
    }
  }
  return 1 ;
}
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);   
  cin >> n >> k ;
  for(int i=1;i<n+1;i++)
    for(int j=1;j<n+1;j++)
      cin >> a[i][j] ;
  int lb=0,rb=1e9 ;
  while(lb<rb){
    int mb=(lb+rb)/2 ;
    if(ok(mb))
      lb=mb+1 ;
    else
      rb=mb  ;
  }
  cout << lb  ;
}