#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
#define li long long
#define itn int

using namespace std;

inline int nxt(){
	int n;
	scanf("%d", &n);
	return n;
}

const int N = 111 * 111 / 3;
int dp[N][N];

int main(){

	int n = nxt(), m = nxt();
	vector<string> a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];

	int posX = 0, posY = 0;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			if (a[i][j] == 'E'){
				posX = i;
				posY = j;
			}
		}
	}

	vector<int> i1, i2, j1, j2;
	for (int i = 0; i <= posX; i++){
		for (int j = posX; j < n; j++){
			i1.push_back(i);
			i2.push_back(j);
		}
	}
	for (int i = 0; i <= posY; i++){
		for (int j = posY; j < m; j++){
			j1.push_back(i);
			j2.push_back(j);
		}
	}

	vector<vector<int>> prefX(n, vector<int>(m + 1, 0)), prefY(n + 1, vector<int>(m, 0));
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			prefX[i][j + 1] = prefX[i][j] + (a[i][j] == 'o' ? 1 : 0);
		}
	}
	for (int i = 0; i < n; i++){
		for (int j = 0; j < m; j++){
			prefY[i + 1][j] = prefY[i][j] + (a[i][j] == 'o' ? 1 : 0);
		}
	}

	int q = i1.size(), w = j1.size();
	vector<vector<int>> pos1(posX + 1, vector<int>(n - posX));
	for (int i = 0; i < q; i++)
		pos1[i1[i]][i2[i] - posX] = i;
	vector<vector<int>> pos2(posY + 1, vector<int>(m - posY));
	for (int i = 0; i < w; i++)
		pos2[j1[i]][j2[i] - posY] = i;

	dp[pos1[posX][0]][pos2[posY][0]] = 0;
	for (int i = posX; i >= 0; i--){
		for (int ii = 0; ii < n - posX; ii++){
			for (int j = posY; j >= 0; j--){
				for (int jj = 0; jj < m - posY; jj++){
					int l = j, r = jj + posY, u = i, d = ii + posX;
					int actl = jj, actr = m - 1 - (posY - j), actu = ii, actd = n - 1 - (posX - i);
					// if (l == posY && r == posY && u == posX && d == posX)
					// 	continue;
					int& res = dp[pos1[i][ii]][pos2[j][jj]];
					if (r > posY)
						res = max(res, dp[pos1[i][ii]][pos2[j][jj - 1]] + (r > actr ? 0 : prefY[min(d, actd) + 1][r] - prefY[max(u, actu)][r]));
					if (l < posY)
						res = max(res, dp[pos1[i][ii]][pos2[j + 1][jj]] + (l < actl ? 0 : prefY[min(d, actd) + 1][l] - prefY[max(u, actu)][l]));
					if (d > posX)
						res = max(res, dp[pos1[i][ii - 1]][pos2[j][jj]] + (d > actd ? 0 : prefX[d][min(r, actr) + 1] - prefX[d][max(l, actl)]));
					if (u < posX)
						res = max(res, dp[pos1[i + 1][ii]][pos2[j][jj]] + (u < actu ? 0 : prefX[u][min(r, actr) + 1] - prefX[u][max(l, actl)]));
				}
			}
		}
	}

	int ans = 0;
	for (int i = posX; i >= 0; i--){
		for (int ii = 0; ii < n - posX; ii++){
			for (int j = posY; j >= 0; j--){
				for (int jj = 0; jj < m - posY; jj++){
					// cerr << i << " " << ii + posX << " " << j << " " << jj + posY << " " << dp[pos1[i][ii]][pos2[j][jj]] << "\n";
					ans = max(ans, dp[pos1[i][ii]][pos2[j][jj]]);
				}
			}
		}
	}

	cout << ans << "\n";

	return 0;
}

#include <algorithm>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
/* clang-format on */

template <class I>
class SegmentTree {
 public:
  using Op = function<I(const I&, const I&)>;

 private:
  int n;
  vector<I> dat;
  Op op;
  I e;

 public:
  SegmentTree(int n_, const Op& op, const I& e) : op(op), e(e) {
    n = 1;
    while (n < n_) n *= 2;
    dat.resize(2 * n - 1, e);
  }
  SegmentTree(const vector<I>& init, const Op& op, const I& e)
      : SegmentTree(init.size(), op, e) {
    for (int i = 0; i < init.size(); i++) {
      dat[i + n - 1] = init[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
  }
  const I& operator[](int k) const { return dat[k + n - 1]; }
  void update(int k, const I& a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  // [a, b)
  I query(int a, int b) const {
    a += n - 1;
    b += n - 1;
    I vl = e;
    I vr = e;
    while (a < b) {
      if ((a & 1) == 0) vl = op(vl, dat[a]);
      if ((b & 1) == 0) vr = op(dat[b - 1], vr);
      a = a / 2;
      b = (b - 1) / 2;
    }
    return op(vl, vr);
  }
  // query(0, i) >= x となる最小のi
  int kth(const I& x) const {
    int k = 0;
    I v = e;
    while (k < n - 1) {
      I t = op(v, dat[k * 2 + 1]);
      if (x <= t) {
        k = k * 2 + 1;
      } else {
        v = t;
        k = k * 2 + 2;
      }
    }
    return k - (n - 1) + 1;
  }
};

int main() {
#ifdef LOCAL
  ifstream cin("in.txt");
#endif
  int N, Q;
  while (cin >> N >> Q) {
    int M = Q + 2;
    vector<ll> x(M);
    for (int i = 0; i < M; i++) cin >> x[i], --x[i];
    vector<ll> dsum(M);
    dsum[0] = 0;
    for (int i = 1; i < M; i++) {
      dsum[i] = dsum[i - 1] + abs(x[i] - x[i - 1]);
    }
    auto op = [](ll a, ll b) { return min(a, b); };
    ll inf = numeric_limits<ll>::max();
    vector<ll> dp(M);
    dp[M - 1] = 0;
    SegmentTree<ll> addSeg(N, op, inf), subSeg(N, op, inf);
    addSeg.update(x[M - 1], dp[M - 1] + dsum[M - 2] + x[M - 1]);
    subSeg.update(x[M - 1], dp[M - 1] + dsum[M - 2] - x[M - 1]);
    for (int i = M - 2; i >= 1; i--) {
      dp[i] = min({dsum[M - 1] - dsum[i],  // 先頭を最後まで持っていく
                   addSeg.query(x[i - 1], N) - dsum[i] - x[i - 1],
                   subSeg.query(0, x[i - 1] + 1) - dsum[i] + x[i - 1]});
      addSeg.update(x[i], min(addSeg[x[i]], dp[i] + dsum[i - 1] + x[i]));
      subSeg.update(x[i], min(subSeg[x[i]], dp[i] + dsum[i - 1] - x[i]));
    }
    cout << dp[1] << endl;
  }
  return 0;
}
