Problem:
Score : 1400 points

Problem Statement

We have a grid with H rows and W columns.
The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:

. : This cell is empty.

o : This cell contains a robot.

E : This cell contains the exit. E occurs exactly once in the whole grid.

Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:

Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.

Find the maximum number of robots that can be salvaged.

Constraints

2≤H,W≤100

a_{ij} is ., o or E.

E occurs exactly once in the whole grid.

Input

The input is given from Standard Input in the following format:

H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}

Output

Print the maximum number of robots that can be salvaged.

Sample Input 1

3 3
o.o
.Eo
ooo

Sample Output 1

3

For example, select left, up, right.

Sample Input 2

2 2
E.
..

Sample Output 2

0

Sample Input 3

3 4
o...
o...
oooE

Sample Output 3

5

Select right, right, right, down, down.

Sample Input 4

5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo

Sample Output 4

12

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#define DEB
#include<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;


#ifdef DEB
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
#define dumpR(x) cerr<<"\x1b[31m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpY(x) cerr<<"\x1b[33m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
#define dumpG(x) cerr<<"\x1b[32m"<<#x<<" = " <<(x)<<"\x1b[39m"<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define dumpR(x) ;
#define dumpY(x) ;
#define dumpG(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

template<class T> void chmin(T& a,const T& b) { if(a>b) a=b; }
template<class T> void chmax(T& a,const T& b) { if(a<b) a=b; }

typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fr<<','<<a.sc<<')';
    return out;
  }
}

//const int INF=5e8;
int h,w;
char buf[105][105];
int dp[5505][5505];

pi itop[5505];
int ptoi[105][105];

int rsum[105][105];
int csum[105][105];
int getr(int y,int r,int l){
  if(l>=r) return 0;
  return rsum[y][r]-rsum[y][l];
}
int getc(int x,int b,int t){
  if(t>=b) return 0;
  return csum[x][b]-csum[x][t];
}
void renew(int y1,int y2,int x1,int x2,int a){
  chmax(dp[ptoi[y1][y2]][ptoi[x1][x2]],a);
}
int main(){
  int cnt=0;
  REP(i,100){
    REPN(j,101,i+1){
      itop[cnt]=mp(i,j);
      ptoi[i][j]=cnt;
      ++cnt;
    }
  }

  cin>>h>>w;
  int gx,gy;
  REP(i,h){
    scanf("%s",buf[i]);
    REP(j,w) if(buf[i][j]=='E'){
      gx=j;
      gy=i;
    }
  }
  REP(i,h) REP(j,w) rsum[i][j+1]=rsum[i][j]+(buf[i][j]=='o'?1:0);
  REP(i,w) REP(j,h) csum[i][j+1]=csum[i][j]+(buf[j][i]=='o'?1:0);

  memset(dp,-1,sizeof(dp));
  renew(gy,gy+1,gx,gx+1,0);

  int res=0;
  for(int len1=1;len1<=h;++len1) for(int len2=1;len2<=w;len2++){
    REP(i1,h+1-len1) REP(j1,w+1-len2){
      int i2=i1+len1,j2=j1+len2;
      
      int idx1=ptoi[i1][i2],idx2=ptoi[j1][j2];
      int val=dp[idx1][idx2];
      if(~val){
        chmax(res,val);
        int top=i2-(gy+1),bot=h-(gy-i1),left=j2-(gx+1),right=w-(gx-j1);//[left...right)
        if(i2<bot){
          renew(i1,i2+1,j1,j2,val+getr(i2,min(right,j2),max(left,j1)));
        }
        if(i1>top){
          renew(i1-1,i2,j1,j2,val+getr(i1-1,min(right,j2),max(left,j1)));
        }
        if(j2<right){
          renew(i1,i2,j1,j2+1,val+getc(j2,min(bot,i2),max(top,i1)));
        }
        if(j1>left){
          renew(i1,i2,j1-1,j2,val+getc(j1-1,min(bot,i2),max(top,i1)));
        }
      }
    }
  }
  cout<<res<<endl;

  return 0;
}