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)

//waz
#include <bits/stdc++.h>
 
using namespace std;
 
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)((x).size()))
 
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long int64;
typedef unsigned int uint;
typedef unsigned long long uint64;
 
#define gi(x) ((x) = F())
#define gii(x, y) (gi(x), gi(y))
#define giii(x, y, z) (gii(x, y), gi(z))
 
int F()
{
	char ch;
	int x, a;
	while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-');
	if (ch == '-') ch = getchar(), a = -1;
	else a = 1;
	x = ch - '0';
	while (ch = getchar(), ch >= '0' && ch <= '9')
		x = (x << 1) + (x << 3) + ch - '0';
	return a * x;
}
 
int H, W;
 
char str[110][110];
 
int cnt[110][110];
 
int sum(int x1, int y1, int x2, int y2)
{
	x1 = max(x1, 1), x1 = min(x1, H);
	x2 = max(x2, 1), x2 = min(x2, H);
	y1 = max(y1, 1), y1 = min(y1, W);
	y2 = max(y2, 1), y2 = min(y2, W);
	if (x1 > x2) return 0;
	if (y1 > y2) return 0;
	//cerr << x1 << ", " << y1 << " and " << x2 << ", " << y2 << endl;
	return cnt[x2][y2] + cnt[x1 - 1][y1 - 1] - cnt[x1 - 1][y2] - cnt[x2][y1 - 1];
}

int f[2][110][110][110];

int main()
{
	gii(H, W);
	for (int i = 1; i <= H; ++i)
		scanf("%s", str[i] + 1);
	int x = 0, y = 0;
	for (int i = 1; i <= H; ++i)
		for (int j = 1; j <= W; ++j)
			if (cnt[i][j] = (str[i][j] == 'o'), str[i][j] == 'E')
				x = i, y = j;
	for (int i = 1; i <= H; ++i)
		for (int j = 1; j <= W; ++j)
			cnt[i][j] += cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1];
	int ans = 0;
	for (int l = 0; x + l <= H; ++l)
		for (int r = 0; x - r > 0; ++r)
			for (int u = 0; y + u <= W; ++u)
				for (int d = 0; y - d > 0; ++d)
				{
					int L = l & 1;
					f[L][r][u][d] = 0;
					int left = x - r, right = x + l;
					int up = y - d, down = y + u;
					left = max(1 + l, left);
					right = min(H - r, right);
					up = max(up, 1 + u);
					down = min(down, W - d);
					if (l && l + r + x <= H) f[L][r][u][d] = max(f[L][r][u][d], f[L ^ 1][r][u][d] + sum(x + l, up, x + l, down));
					if (r && l + r <= x - 1) f[L][r][u][d] = max(f[L][r][u][d], f[L][r - 1][u][d] + sum(x - r, up, x - r, down));
					if (u && u + d + y <= W) f[L][r][u][d] = max(f[L][r][u][d], f[L][r][u - 1][d] + sum(left, y + u, right, y + u));
					if (d && u + d <= y - 1) f[L][r][u][d] = max(f[L][r][u][d], f[L][r][u][d - 1] + sum(left, y - d, right, y - d));
					//cerr << l << ", " << r << ", " << u << ", " << d << " : " << f[l][r][u][d] << endl; 
					ans = max(ans, f[L][r][u][d]);
				}
	printf("%d\n", ans);
	return 0;
}