#include "bits/stdc++.h"
using namespace std;
using     ll=long long;
using    vll=vector<   ll>;
using   vvll=vector<  vll>;
using  vvvll=vector< vvll>;
using vvvvll=vector<vvvll>;
using pll=pair<ll,ll>; using tll=tuple<ll,ll,ll>; using qll=tuple<ll,ll,ll,ll>;

constexpr ll INF = 1LL << 60;
struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(numeric_limits<double>::max_digits10); } } fast;
#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, (N)-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, (N)-1)
#define rep(i, S, E)  for (ll i = (S); i <= (E); i++)
#define dep(i, E, S)  for (ll i = (E); i >= (S); i--)
#define each(e, v) for (auto&& e : v)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; }
template<class T> inline T MaxE(vector<T>&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmax(m,v[i]); return m; }
template<class T> inline T MinE(vector<T>&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmin(m,v[i]); return m; }
template<class T> inline T MaxE(vector<T> &v) { return MaxE(v,0,(ll)v.size()-1); }
template<class T> inline T MinE(vector<T> &v) { return MinE(v,0,(ll)v.size()-1); }
template<class T> inline T Sum(vector<T> &v,ll S,ll E){ T s=T(); rep(i,S,E)s+=v[i]; return s; }
template<class T> inline T Sum(vector<T> &v) { return Sum(v,0,v.size()-1); }
template<class T> inline ll sz(T &v){ return (ll)v.size(); }
inline ll CEIL(ll a,ll b){ return (a<0) ? -(-a/b) : (a+b-1)/b; }
inline ll FLOOR(ll a,ll b){ return -CEIL(-a,b); }

//vector用テンプレート
template<class T> inline vector<T>& operator+=(vector<T> &a,const vector<T> &b){ for (ll i=0; i<(ll)a.size(); i++) a[i]+=b[i];  return a; }
template<class T> inline vector<T>& operator-=(vector<T> &a,const vector<T> &b){ for (ll i=0; i<(ll)a.size(); i++) a[i]-=b[i];  return a; }
template<class T> inline vector<T>& operator*=(vector<T> &a,const vector<T> &b){ for (ll i=0; i<(ll)a.size(); i++) a[i]*=b[i];  return a; }
template<class T> inline vector<T>& operator/=(vector<T> &a,const vector<T> &b){ for (ll i=0; i<(ll)a.size(); i++) a[i]/=b[i];  return a; }
template<class T> inline vector<T>& operator%=(vector<T> &a,const vector<T> &b){ for (ll i=0; i<(ll)a.size(); i++) a[i]%=b[i];  return a; }
template<class T,class S> inline vector<T>& operator+=(vector<T> &a,S b){ for (T &e: a) e+=b;  return a; }
template<class T,class S> inline vector<T>& operator-=(vector<T> &a,S b){ for (T &e: a) e-=b;  return a; }
template<class T,class S> inline vector<T>& operator*=(vector<T> &a,S b){ for (T &e: a) e*=b;  return a; }
template<class T,class S> inline vector<T>& operator/=(vector<T> &a,S b){ for (T &e: a) e/=b;  return a; }
template<class T,class S> inline vector<T>& operator%=(vector<T> &a,S b){ for (T &e: a) e%=b;  return a; }
template<class T,class S> inline vector<T> operator+(const vector<T> &a,S b){ vector<T> c=a; return c+=b; }
template<class T,class S> inline vector<T> operator-(const vector<T> &a,S b){ vector<T> c=a; return c-=b; }
template<class T,class S> inline vector<T> operator*(const vector<T> &a,S b){ vector<T> c=a; return c*=b; }
template<class T,class S> inline vector<T> operator/(const vector<T> &a,S b){ vector<T> c=a; return c/=b; }
template<class T,class S> inline vector<T> operator%(const vector<T> &a,S b){ vector<T> c=a; return c%=b; }
template<class T,class S> inline vector<T> operator-(S b,const vector<T> &a){ vector<T> c=-a; return c+=b; }
template<class T> inline vector<T> operator-(const vector<T> &a){ vector<T> c=a; return c*=(-1); }
template<class T> inline ostream &operator<<(ostream &os,const vector<T> &a){ for (ll i=0; i<(ll)a.size(); i++) os<<(i>0?" ":"")<<a[i];  return os; }

//pair用テンプレート
using  vpll=vector< pll>;
using vvpll=vector<vpll>;
template<class T,class S> inline pair<T,S>& operator+=(pair<T,S> &a,const pair<T,S> &b){ a.first+=b.first; a.second+=b.second; return a; }
template<class T,class S> inline pair<T,S>& operator-=(pair<T,S> &a,const pair<T,S> &b){ a.first-=b.first; a.second-=b.second; return a; }
template<class T,class S> inline pair<T,S>& operator*=(pair<T,S> &a,const pair<T,S> &b){ a.first*=b.first; a.second*=b.second; return a; }
template<class T,class S> inline pair<T,S>& operator/=(pair<T,S> &a,const pair<T,S> &b){ a.first/=b.first; a.second/=b.second; return a; }
template<class T,class S> inline pair<T,S>& operator%=(pair<T,S> &a,const pair<T,S> &b){ a.first%=b.first; a.second%=b.second; return a; }
template<class T,class S,class R> inline pair<T,S>& operator+=(pair<T,S> &a,R b){ a.first+=b; a.second+=b; return a; }
template<class T,class S,class R> inline pair<T,S>& operator-=(pair<T,S> &a,R b){ a.first-=b; a.second-=b; return a; }
template<class T,class S,class R> inline pair<T,S>& operator*=(pair<T,S> &a,R b){ a.first*=b; a.second*=b; return a; }
template<class T,class S,class R> inline pair<T,S>& operator/=(pair<T,S> &a,R b){ a.first/=b; a.second/=b; return a; }
template<class T,class S,class R> inline pair<T,S>& operator%=(pair<T,S> &a,R b){ a.first%=b; a.second%=b; return a; }
template<class T,class S,class R> inline pair<T,S> operator+(const pair<T,S> &a,R b){ pair<T,S> c=a; return c+=b; }
template<class T,class S,class R> inline pair<T,S> operator-(const pair<T,S> &a,R b){ pair<T,S> c=a; return c-=b; }
template<class T,class S,class R> inline pair<T,S> operator*(const pair<T,S> &a,R b){ pair<T,S> c=a; return c*=b; }
template<class T,class S,class R> inline pair<T,S> operator/(const pair<T,S> &a,R b){ pair<T,S> c=a; return c/=b; }
template<class T,class S,class R> inline pair<T,S> operator%(const pair<T,S> &a,R b){ pair<T,S> c=a; return c%=b; }
template<class T,class S,class R> inline pair<T,S> operator-(R b,const pair<T,S> &a){ pair<T,S> c=-a; return c+=b; }
template<class T,class S> inline pair<T,S> operator-(const pair<T,S> &a){ pair<T,S> c=a; return c*=(-1); }
template<class T,class S> inline ostream &operator<<(ostream &os,const pair<T,S> &a){ return os << a.first << ' ' << a.second; }

#if 1
#include <atcoder/all>
using namespace atcoder;
#endif

struct Mcf_graph{
	mcf_graph<int,ll> mcfg;
	Mcf_graph(){}
	Mcf_graph(ll n): mcfg(int(n)) {}
	ll add_edge(ll from,ll to,ll cap,ll cost) {
		return (ll)mcfg.add_edge(int(from),int(to),int(cap),cost);
	}
	mcf_graph<int,ll>::edge get_edge(ll i){ return mcfg.get_edge(int(i)); }
	vector<mcf_graph<int,ll>::edge> edges() { return mcfg.edges(); }
	pair<ll,ll> flow(ll s,ll t) {
		pair<int,ll> ret=mcfg.flow(int(s),int(t));
		return pair<ll,ll>{ret.first,ret.second};
	}
	pair<ll,ll> flow(ll s,ll t,ll flow_limit) {
		pair<int,ll> ret=mcfg.flow(int(s),int(t),int(flow_limit));
		return pair<ll,ll>{ret.first,ret.second};
	}
	vector<pair<int,ll>> slope(ll s,ll t) { return mcfg.slope(int(s),int(t)); }
	vector<pair<int,ll>> slope(ll s,ll t,ll flow_limit) {
		return mcfg.slope(int(s),int(t),int(flow_limit));
	}
};

vvll bfs2d(vector<string> &mz, pll st){
	ll h=mz.size(), w=mz[0].size();
	ll di[2]={0,1};
	ll dj[2]={1,0};
	vvll dist(h, vll(w, INF));
	queue<pll> Q;
	dist[st.first][st.second]=0;
	Q.push(st);
	while (!Q.empty()){
		ll i, j;  tie(i, j) = Q.front(); Q.pop();
		rep(k, 0, 1){
			ll ii=i+di[k], jj=j+dj[k];
			if (ii<0 or h<=ii or jj<0 or w<=jj) continue;

			if (mz[ii][jj]=='#') continue; //壁ならskip

			if (chmin(dist[ii][jj], dist[i][j]+1)) Q.push({ii,jj});
		}
	}
	return dist;
}

void solve()
{
	const ll BIG=1000000000;
	ll n,m;  cin >> n >> m;
	vector<string> S(n);  rep(i,0,n-1){ string S_; cin>>S_; S[i]=S_; }

	ll km=0;//コマ数
	rep(i,0,n-1){
		rep(j,0,m-1){
			if (S[i][j]=='o')km++;
		}
	}

	Mcf_graph graph(n*m+km+2); //n:頂点数 s,tも含む
	ll s=n*m+km;
	ll t=n*m+km+1;

	ll kmidx=0;
	rep(i,0,n-1){
		rep(j,0,m-1){
			if (S[i][j]!='o')continue;

			vvll dist=bfs2d(S,{i,j});

			ll kmv=n*m+kmidx;

			rep(ii,0,n-1){
				rep(jj,0,m-1){
					if (dist[ii][jj]==INF)continue;

					ll v=ii*m+jj;
					graph.add_edge(kmv,v,1,BIG-dist[ii][jj]);
				}
			}
			graph.add_edge(s,kmv,1,0);

			kmidx++;
		}
	}
	rep(i,0,n-1){
		rep(j,0,m-1){
			ll v=i*m+j;
			graph.add_edge(v,t,1,0);
		}
	}

	ll f,c; tie(f,c)=graph.flow(s,t,km); //戻り値 f:流せた量、c:最小コスト
	ll ans=BIG*km-c;
	cout << ans << '\n';
}


int main(){
#if 1
	solve();
#else
	ll t;  cin >> t;
	rep(i, 0, t-1){
		solve();
	}
#endif
	return 0;
}


Measured memory_bytes: 8808