/**
 *    author:  otera    
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int n, m;
vector<vector<int>> a;

void solve() {
	cin >> n >> m;
    a.assign(n, vector<int>(m));
    rep(i, n) {
        rep(j, m) {
            cin >> a[i][j];
            -- a[i][j];
        }
    }
    vector<int> used(n, 0), check(m, 1);
    int ans = n;
    int cnt = 0;
    while(true) {
        map<int, int> mp;
        rep(i, n) {
            while(used[i] < m && !check[a[i][used[i]]]) {
                used[i] ++;
            }
            mp[a[i][used[i]]] ++;
        }
        set<int> se; int ma = 0;
        for(auto p: mp) {
            if(ma == p.sc) {
                se.insert(p.fr);
            } else if(ma < p.sc) {
                se.clear();
                ma = p.sc;
                se.insert(p.fr);
            }
        }
        chmin(ans, ma);
        for(auto j: se) {
            check[j] = 0;
            ++ cnt;
        }
        if(cnt == m) break;
    }
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int res,ll,rr,uu,dd,ad,R,L,D,U,sx,sy,h,w,pr[105][105],pc[105][105],dp[105][105][105];
char s[105][105];
int main()
{
    scanf("%d%d",&h,&w);
    for (int i=1;i<=h;++i)
        scanf("%s",s[i]+1);
    for (int i=1;i<=h;++i)
        for (int j=1;j<=w;++j) {
            pr[i][j]=pr[i-1][j]+(s[i][j]=='o');
            pc[i][j]=pc[i][j-1]+(s[i][j]=='o');
            if (s[i][j]=='E')
                sx=i,sy=j;
        }
    memset(dp,-1,sizeof dp);
    dp[0][0][0]=0;
    L=sy-1,R=w-sy,U=sx-1,D=h-sx;
    for (int l=0;l<=L;++l)
        for (int r=0;r<=R;++r)
            for (int u=0;u<=U;++u)
                for (int d=0;d<=D;++d) {
                    if (dp[r][u][d]==-1)
                        continue;
                    rr=min(sy+r,w-l);
                    ll=max(sy-l,r+1);
                    uu=max(sx-u,d+1);
                    dd=min(sx+d,h-u);
                    res=max(res,dp[r][u][d]);
                    if (l+r<R) {
                        ad=pr[dd][sy+r+1]-pr[uu-1][sy+r+1];
                        dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+ad);
                    }
                    if (u+d<U) {
                        ad=pc[sx-u-1][rr]-pc[sx-u-1][ll-1];
                        dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+ad);
                    }
                    if (u+d<D) {
                        ad=pc[sx+d+1][rr]-pc[sx+d+1][ll-1];
                        dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+ad);
                    }
                    if (l+r<L) {
                        ad=pr[dd][sy-l-1]-pr[uu-1][sy-l-1];
                        dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+ad);
                    }
                }
    printf("%d\n",res);
    return 0;
}