#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<long long> Vll;
ll mm=1000000000;ll MM=mm+7;
#define rep(i, n) for(int i=0;i<n;i++)
#define PI 3.141592653589793
 

int main(){
    ll n,k;
    cin >> n >> k;
    vector<Vll> t(n,Vll(n));
    rep(i,n){
        rep(j,n){
            cin >> t[i][j];
        }
    }
    ll ans=0;
    vector<int> v(n-1);
    for(int i=0;i<n-1;i++)v[i]=i+1;
    do{
        int cnt=0;
       rep(i,n-1){
           if(i==n-2){
               cnt+=t[v[i-1]][v[i]];
               cnt+=t[v[i]][0];
           }
           else if(i==0){
               cnt+=t[0][v[i]];
           }
           else{
               cnt+=t[v[i-1]][v[i]];
           }
            if(cnt==k)ans++;
       }
    }while(next_permutation(v.begin(),v.end()));  
    
    cout << ans << endl;
}


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

int N, ft[200005];
set< pair<int,int> > s;

void update (int x, int v) {
    x++;
    while (x <= N) ft[x] += v, x += x & -x;
}

int query (int x) {
    x++;
    int ans = 0;
    while (x) ans += ft[x], x -= x & -x;
    return ans;
}

int main() {
    scanf("%d", &N);
    memset(ft,0,sizeof(ft));
    for (int i = 1; i < N; ++i) update(i,1);
    for (int i = 0; i < N; ++i) {
        int x; scanf("%d", &x);
        s.emplace(x+i,i);
    }
    long long ans = 0;
    for (int i = 0; i < N; ++i) {
        int x; scanf("%d", &x);
        x += i;
        auto it = s.lower_bound(make_pair(x,-1));
        if (it == s.end()) {
            printf("-1");
            return 0;
        }
        int n = (*it).second;
        ans += query(n);
        update(n,-1);
        s.erase(it);
    }
    printf("%lld", ans);

    return 0;
}
