#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <vector>
#include <array>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <limits>
#include <numeric>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define chmin(x, y) x = std::min(x, y)
#define chmax(x, y) x = std::max(x, y)
#define all(v) v.begin(), v.end()
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)
#define deg_to_rad(deg) (((deg)/360)*2*M_PI)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

template <typename T>
void dump(const vector<T>& v) {
	for (const T& n : v) {
		cout << n << " ";
	}
	cout << endl;
}

template <typename T>
void dump(const vector<vector<T>>& v) {
	for (const vector<T>& ns : v) {
		for (const T& n : ns) {
			cout << n << " ";
		}
		cout << endl;
	}
}

const int INF = (numeric_limits<int>::max)() / 2;
const ll INF_LL = (numeric_limits<ll>::max)() / 2;
const ll MOD = 1000000007;

void solve() {
	int n, m;
	cin >> n >> m;

	vector<P> a(n);
	rep(i, n) {
		cin >> a[i].first;
		a[i].second = i;
	}

	vector<vector<ll>> ls(n);
	rep(i, m) {
		ll x, y;
		cin >> x >> y;
		x--;
		y--;
		ls[y].push_back(x);
	}

	vector<ll> dp(n, INF_LL);

	for (int i = 1; i < n; i++) {
		for(ll j : ls[i]) {
			dp[i] = min({ dp[i], dp[j], a[j].first });
		}
	}

	ll ans = -INF_LL;
	rep(i, n) {
		chmax(ans, a[i].first - dp[i]);
	}
	cout << ans << endl;
}

int main() {
	int t = 1;
	while (t--) {
		solve();
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll=long long;

vector<vector<int>> t;
int ans=0;
int k;
int n;

void cal(int s,int cnt,vector<bool>ok,int sum){
  for(int i=1;i<n;++i){
    if(ok[i]==true || s==i)continue;
    //int tmp=sum;
    ok[i]=true;
    sum+=t[s][i];
    cal(i,cnt+1,ok,sum);
    ok[i]=false;
    sum-=t[s][i];
  }
  //cout << s << endl;
  if(cnt==n){
    //cout << s << endl;
    sum+=t[s][0];
    //cout << sum << endl;
    if(sum==k)ans++;
  }
  return;
}

int main(){
  //int n,k;
  cin >> n >> k;
  t=vector<vector<int>>(n,vector<int>(n));
  for(int i=0;i<n;++i){
    for(int j=0;j<n;++j){
      cin >> t[i][j];
    }
  }

  vector<bool> ok(n,false);

  ok[0]=true;

  int sum=0;

  cal(0,1,ok,sum);

  cout << ans << endl;
  return 0;
}
