#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <functional>
#include <map>
#include <queue>

#define ll long long
#define ld long double
#define mp make_pair
#define TASKNAME "robots"

using namespace std;

const ll inf = 1e9;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
map<char, int> d;

int n, m;
vector<vector<char>> table;
vector<vector<char>> used;
vector<vector<int>> dist;
vector<vector<int>> tin;
vector<vector<int>> c;
vector<int> cycles;
int len = 0;
int s = 0;
int timer = 0;
int cid = 0;

void dfs(int x, int y) {
	tin[x][y] = timer++;
	used[x][y] = 1;
	int dd = d[table[x][y]];
	int nx = x + dy[dd];
	int ny = y + dx[dd];
	if (used[nx][ny] == 0) {
		dist[nx][ny] = dist[x][y] + 1;
		dfs(nx, ny);
	}
	else if (used[nx][ny] == 1) {
		len = dist[x][y] + 1 - dist[nx][ny];
		cid = cycles.size();
		cycles.push_back(len);
		s = tin[nx][ny];
	}
	else if (used[nx][ny] == 2)
		cid = c[nx][ny];
	c[x][y] = cid;
	used[x][y] = 2;
	if (tin[x][y] >= s)
		used[x][y] = 3;
}

void solve() {
	d['E'] = 0;
	d['e'] = 0;
	d['S'] = 1;
	d['s'] = 1;
	d['W'] = 2;
	d['w'] = 2;
	d['N'] = 3;
	d['n'] = 3;
	cin >> n >> m;
	table.resize(n, vector<char>(m, '0'));
	used.resize(n, vector<char>(m, 0));
	dist.resize(n, vector<int>(m, 0));
	tin.resize(n, vector<int>(m, 0));
	c.resize(n, vector<int>(m, 0));
	cin >> table[0][0];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++)
			cin >> table[i][j];
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			len = 0;
			cid = 0;
			timer = 0;
			if (used[i][j] == 0)
				dfs(i, j);
		}
	}
	vector<int> t(cycles.size());
	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (table[i][j] < 'Z') {
				if (t[c[i][j]] < cycles[c[i][j]])
					ans++;
				t[c[i][j]]++;
			}
		}
	}
	cout << ans;
}

int main() {
#ifdef __DEBUG__
	freopen("input.txt", "r", stdin);
	clock_t start = clock();
#else
	freopen(TASKNAME".in", "r", stdin);
	freopen(TASKNAME".out", "w", stdout);
#endif 

	solve();

#ifdef __DEBUG__
	cerr << "\nTime: " << (ld)(clock() - start) / (ld)CLOCKS_PER_SEC << endl;
#endif
}