#include<bits/stdc++.h>
typedef int LL;
typedef double dl;
#define opt operator
#define pb push_back
const LL maxn=1e6+9,mod=998244353,inf=0x3f3f3f3f;
LL Read(){
	LL x(0),f(1); char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-') f=-1; c=getchar();
	}
	while(c>='0' && c<='9'){
		x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
	}return x*f;
}
void Chkmin(LL &x,LL y){
	if(y<x) x=y;
}
void Chkmax(LL &x,LL y){
	if(y>x) x=y;
}
LL add(LL x,LL y){
	return x+=y,x>=mod?x-mod:x;
}
LL dec(LL x,LL y){
	return x-=y,x<0?x+mod:x;
}
LL mul(LL x,LL y){
	return 1ll*x*y%mod;
}
LL Pow(LL base,LL b){
	LL ret(1); while(b){
		if(b&1) ret=mul(ret,base); base=mul(base,base); b>>=1;
	}return ret;
}
LL n,k;
LL fa[maxn],size[maxn],fac[maxn];
LL a[100][100];
LL Find(LL x){
	return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Merge(LL x,LL y){
	LL fx(Find(x)),fy(Find(y));
	if(fx==fy) return;
	size[fx]+=size[fy]; fa[fy]=fx;
}
void Init(){
	for(LL i=1;i<=n;++i) fa[i]=i,size[i]=1;
}
int main(){
	n=Read(); k=Read();
	fac[0]=1; for(LL i=1;i<=n;++i) fac[i]=mul(fac[i-1],i);
	for(LL i=1;i<=n;++i){
		for(LL j=1;j<=n;++j) a[i][j]=Read();
	}
	Init();
	for(LL x=1;x<=n;++x){
		for(LL y=x+1;y<=n;++y){
			LL flag(1);
			for(LL i=1;i<=n;++i) flag&=(a[i][x]+a[i][y]<=k);
			if(flag){
				Merge(x,y);
			}
		}
	}
	LL ret(1);
	for(LL i=1;i<=n;++i) if(Find(i)==i){
		ret=1ll*ret*fac[size[i]]%mod;
	}
	Init();
	for(LL x=1;x<=n;++x){
		for(LL y=x+1;y<=n;++y){
			LL flag(1);
			for(LL i=1;i<=n;++i) flag&=(a[x][i]+a[y][i]<=k);
			if(flag){
				Merge(x,y);
			}
		}
	}
	for(LL i=1;i<=n;++i) if(Find(i)==i){
		ret=1ll*ret*fac[size[i]]%mod;
	}
	printf("%d\n",ret);
	return 0;
}



#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define endl '\n'
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;
uniform_int_distribution<int> rng(0, 1e9);

//DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define deb(x...)  _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END


const int N = 1e3 + 5;
int a[N][N];
int x[N][N];
int y[N][N];




void solve(){
    
    int n, k;
    cin >> n >> k;
    
    fr(i,1,n){
        fr(j,1,n){
            cin >> a[i][j];
        }
    }

    int l = 0, h = 1e9;

    while(l < h){

        int mid = (l + h) / 2;

        fr(i,1,n){
            fr(j,1,n){
                x[i][j] = (a[i][j] <= mid) ? -1 : 1;
            }
        }


        fr(i,1,n){
            fr(j,1,n){
                y[i][j] = y[i-1][j] + y[i][j-1] - y[i-1][j-1] + x[i][j]; 
            }
        }

        bool ok = 0;
        fr(i,k,n){
            fr(j,k,n){
                int z = y[i][j] - y[i-k][j] - y[i][j-k] + y[i-k][j-k];
                if(z <= 0){
                    ok = 1;
                }
            }
        }

        if(ok)
            h = mid;
        else
            l = mid + 1;
    }

    cout << l << endl;


}




signed main() {

    #ifdef LOCAL
        (void)!freopen("input.txt","r",stdin);
        (void)!freopen("output.txt","w",stdout);
    #endif
    
    ios_base::sync_with_stdio(0), cin.tie(0);
    cout << fixed << setprecision(15);
    int t = 1;
    //cin >> t;

    while (t--)
        solve();
    return 0;
}

int powm(int a, int b){
    int res = 1;
    while (b) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

int divide(int a, int b) {
    return (a % mod) * powm(b % mod, mod - 2) % mod;
}

