#include<bits/stdc++.h>

using namespace std;

const int N = 2e3 + 5;
vector<vector<int>> G(N);
vector<bool> vis(N);

void dfs(int u) {
    vis[u] = true;
    for (int v : G[u]) {
        if (vis[v] == true)
            continue;
        dfs(v);
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n, m; cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        G[x].emplace_back(y);
    }   

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        dfs(i);
        for (int j = 1; j <= n; j++) {
            if (vis[j] == true)
                ans++;
            vis[j] = false;
        }
    }
    cout << ans << "\n";
}
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<utility>
#include<map>
#include<tuple>
#include<deque>
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
const long long LINF = 1001002003004005006;
const double PI = acos(-1);
int dir4row[] = { 1,0,-1,0 };
int dir4col[] = { 0,1,0,-1 };
int dir8row[] = { 1,1,0,-1,-1,-1,0,1 };
int dir8col[] = { 0,1,1,1,0,-1,-1,-1 };

/*-----------------------------------------------*/

int main() {
	int n, k; cin >> n >> k;
	vector<vector<int>> t(n, vector<int>(n));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> t[i][j];
		}
	}
	vector<int> ord(n - 1);
	for (int i = 1; i < n; ++i) {
		ord[i - 1] = i;
	}
	int ans = 0;
	do {
		int now = 0;
		for (int i = 0; i < n - 2; ++i) {
			now += t[ord[i]][ord[i + 1]];
		}
		now += t[0][ord.front()];
		now += t[0][ord.back()];
		if (now == k) {
			ans++;
		}
	} while (next_permutation(ord.begin(), ord.end()));
	cout << ans << endl;
	return 0;
}