#include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cctype>
#include <algorithm>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template<class T>inline T next_num(){
	T i=0;char c;
	while(!isdigit(c=getchar())&&c!='-');
	bool flag=c=='-';
	flag?(c=getchar()):0;
	while(i=i*10-'0'+c,isdigit(c=getchar()));
	return flag?-i:i;
}
template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;}
const int N=101;
char mat[N][N];
short f[N][N][N][N],sum[N][N];
inline int asksum(int x1,int x2,int y1,int y2){
	return x1<=x2&&y1<=y2?sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]:0;
}
int main(){
	int n=ni,m=ni,ex,ey;
	memset(sum,0,sizeof(sum));
	for(int i=1;i<=n;i++){
		scanf("%s",mat[i]+1);
		for(int j=1;j<=m;j++){
			sum[i][j]=(mat[i][j]=='o')+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
			if(mat[i][j]=='E'){
				ex=i,ey=j;
			}
		}
	}
	for(int i=ex;i>=1;i--){
		for(int j=ex;j<=n;j++){
			for(int k=ey;k>=1;k--){
				for(int l=ey;l<=m;l++){
					short &F=f[i][j][k][l]=0;
					int xs=max(1+(j-ex),i),xt=min(n-(ex-i),j);
					int ys=max(1+(l-ey),k),yt=min(m-(ey-k),l);
					if(i<ex)apmax(F,f[i+1][j][k][l]+asksum(xs,i,ys,yt));
					if(j>ex)apmax(F,f[i][j-1][k][l]+asksum(j,xt,ys,yt));
					if(k<ey)apmax(F,f[i][j][k+1][l]+asksum(xs,xt,ys,k));
					if(l>ey)apmax(F,f[i][j][k][l-1]+asksum(xs,xt,l,yt));
				}
			}
		}
	}
	printf("%d\n",(int)f[1][n][1][m]);
	return 0;
}
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 1000010

int dp[SIZE][2];

int main() {
  char S[SIZE];

  scanf("%s", S);
  int N = strlen(S);

  for (int i = 0; i <= N; i++)
    dp[i][0] = dp[i][1] = INF;

  dp[0][0] = 0;
  dp[0][1] = 1;

  for (int i = 0; i < N; i++) {
    int p = S[i] - '0';

    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + p);
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 10 - p);

    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + p + 1);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 10 - p - 1);
  }

  cout << dp[N][0] << endl;

  return 0;
}
