#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	int H,W,count;
	char a;
	count = 0;
	cin >> H >> W;
	vector<vector<char>> S(H, vector<char>(W));
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> S[i][j];
		}
	}
	for (int i = 0; i <= H-2; i++) {
		for (int j = 0; j <= W-2; j++) {
			a = S[i][j];
			if (a == '.') {
				if (a == S[i][j + 1]) {
					count += 1;
				}
				if (a == S[i + 1][j]) {
					count += 1;
				}
			}
		}
	}
	for (int i = 0; i <= H - 2; i++) {
		a = S[i][W-1];
		if (a == '.') {
			if (a == S[i + 1][W - 1]) {
				count += 1;
			}
		}
	}
	for (int i = 0; i <= W - 2; i++) {
		a = S[H-1][i];
		if (a == '.') {
			if (a == S[H - 1][i + 1]) {
				count += 1;
			}
		}
	}
	cout << count;
	return 0;
}
#include<iostream>
#include<string>
#include<algorithm>    
#include<cmath>
#include<ctime>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<bitset>
#include<functional>
#include <fstream>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
#define puts(x) cout << (x) << endl;
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 10000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p % i == 0) return false;
	}
	return true;
}
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (a % b == 0)return b;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll keta(ll n) {
	ll res = 0;
	while (n >= 1) {
		res += n % 10; n /= 10;
	}
	return res;
}
ll modpow(ll x, ll y) {
	ll res = 1;
	while (y) {
		if (y % 2) { res *= x; res %= mod; }
		x = x * x % mod; y /= 2;
	}
	return res;
}
ll nCk(ll n, ll k) {
	ll a = 1, b = 1;
	for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
	for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
	return a * modpow(b, mod - 2) % mod;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
struct edge { ll to, cost; };
struct status {
	ll cost;
	ll bango;

	bool operator<(const status& rhs) const { return cost < rhs.cost; };
	bool operator>(const status& rhs) const { return cost > rhs.cost; };
};


signed main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	//cout << fixed << setprecision(15);

	//input
	ll n, m; cin >> n >> m;

	vector<P> a(n); vector<ll> mi(n + 1);
	rep(i, n) { cin >> a[i].first; a[i].second = i + 1; mi[i + 1] = a[i].first; }
	sort(all(a)); reverse(all(a));

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

	vector<ll> ma(n + 2, 0 - INF);
	vector<bool> visit(n + 2);
	rep(i, a.size()) {
		queue<ll> que; ll cnt = a[i].first;
		if (!visit[a[i].second]) {
			que.push(a[i].second);
		}
		while (que.size()) {
			ll q = que.front(); que.pop();
			for (int i = 0; i < G[q].size(); i++) {
				if (!visit[G[q][i]]) {
					que.push(G[q][i]); visit[G[q][i]] = true;
					ma[G[q][i]] = cnt;
				}
			}
		}
	}
	ll ans = 0 - INF;
	rep2(i, n) {
		ans = max(ans, ma[i] - mi[i]);
	}
	cout << ans << endl;
	return 0;
}