#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define DBG(a) cout<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define eps 1e-8
#define eq(x,y) (fabs((x)-(y)) < eps)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int>pii;
const int mod = 998244353;
long double pi = acosl(-1);
const ll infl = 1e17+100;
const int inf = 1e9+100;
const int nmax = 1e6+5;
const int MAXLG = log2(nmax)+1;
//mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//typedef tree< ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ost;



int main(){
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);


    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int>a(n+1);
    for(int &x : a) cin>>x;

    int kom = inf;
    for(int i=0; i<n; i++){
        kom = min(kom, abs(a[i]-a[i+1]));
    }

    vector< vector<int> >ans(kom, vector<int>(n+1) );
    for(int i=0; i<=n; i++){
        int z = a[i]/kom;
        int extra = a[i]%kom;
        for(int j=0; j<kom; j++){
            ans[j][i] = z;
            if(j<extra) ans[j][i]++;
        }
    }

    cout<<kom<<"\n";
    for(auto z : ans){
        for(auto x : z){
            cout<<x<<" ";
        }
        cout<<"\n";
    }
}
/*
*/

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>

using namespace std;

typedef long long LL;
const int N = 810, inf = 1e9;

int n, m;
int a[N][N];
int s[N][N];
int vis;

bool check(int x){
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (a[i][j] > x);
            
    int res = inf;
    for(int i = m; i <= n; i ++)
        for(int j = m; j <= n; j ++)
            res = min(res, s[i][j] - s[i - m][j] - s[i][j - m] + s[i - m][j - m]);
    return res <= m * m - vis;
}

int main(){
    scanf("%d%d", &n, &m);
    
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            scanf("%d", &a[i][j]);
            
    vis = m * m / 2;
    if(m & 1) vis ++;
    int l = 0, r = 1e9;
    while(l < r){
        int mid = l + r >> 1;
        if(check(mid)) r = mid;
        else l = mid + 1;
    }
    printf("%d\n", r);
    return 0;
}