#include <bits/stdc++.h>

#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define reset(a,v) memset((a), v, sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

const int MOD = 998244353;

const int N = 55;

int n, z;
int dat[N][N];
ll fact[N];

struct DisjointSet {

	int total;
	int par[N], subsize[N];

	void init(int n) {
		total = n;
		for (int i = 0; i < n; i++) {
			par[i] = i;
			subsize[i] = 1;
		}
	}

	int findSet(int x) {
		return par[x] == x ? x : par[x] = findSet(par[x]);
	}

	void uni(int a, int b) {
		int x = findSet(a), y = findSet(b);
		if (x != y) {
			par[y] = x;
			subsize[x] += subsize[y];
		}
	}
};

DisjointSet dsR, dsC;

int main() {
	for (int i = 0; i < N; i++) {
		if (i == 0) fact[i] = 1;
		else fact[i] = (1ll*i*fact[i-1]) % MOD;
	}

	cin >> n >> z;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> dat[i][j];
		}
	}
	dsR.init(n);
	dsC.init(n);
	for (int i = 0; i < n-1; i++) {
		for (int j = i+1; j < n; j++) {
			bool ok = true;
			for (int k = 0; k < n; k++) {
				if (dat[k][i] + dat[k][j] > z) ok = false;
			}
			if (ok) {
				dsC.uni(i,j);
			}

			ok = true;
			for (int k = 0; k < n; k++) {
				if (dat[i][k] + dat[j][k] > z) ok = false;
			}
			if (ok) {
				dsR.uni(i,j);
			}
		}
	}
	
	ll ans = 1;
	for (int i = 0; i < n; i++) {
		if (dsR.findSet(i) == i) {
			ans = (ans * fact[dsR.subsize[i]]) % MOD;
			// printf("%d %d\n", i, dsR.subsize[i]);
		}
		if (dsC.findSet(i) == i) {
			ans = (ans * fact[dsC.subsize[i]]) % MOD;
			// printf("%d %d\n", i, dsC.subsize[i]);
		}
	}
	cout << ans << "\n";
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	// cin>>t;
	while(t--){
		int n,k;
		cin>>n>>k;
		int a[n];
		int count[n]={0};
		for(int i=0;i<n;i++){
			cin>>a[i];
			count[a[i]]++;
		}
		priority_queue<int, vector<int>, greater<int>> p;
		int ans=0;
		int m=1e9;
		int filled=0;
		for(int i=0;i<n-1;i++){
			m=min(m,count[i]);
			count[i]=min(m,count[i]);
			count[i+1]=min(m,count[i+1]);
			if(count[i]>count[i+1]){
				int x=count[i]-count[i+1];
				for(int j=0;j<x;j++){
					if(filled<k){
						filled++;
						p.push(i+1);
						ans+=i+1;
					}
					else{
						if(p.top()<i+1){
							ans+=i+1-p.top();
							p.pop();
							p.push(i+1);
						}
					}
				}

			}
		}
		cout<<ans;
		cout<<endl;
	}
}