#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
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;}

template< typename flow_t, typename cost_t >
struct MinCostFlow {
  const cost_t INF;

  struct edge {
    int to;
    flow_t cap;
    cost_t cost;
    int rev;
    bool isrev;
  };
  vector<vector<edge>> graph;
  vector<cost_t> potential,min_cost;
  vector<int> prevv,preve;

  MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {}

  void add_edge(int from,int to,flow_t cap,cost_t cost){
    graph[from].emplace_back((edge){to,cap,cost,(int)graph[to].size(),false});
    graph[to].emplace_back((edge){from,0,-cost,(int)graph[from].size()-1,true});
  }

  cost_t min_cost_flow(int s,int t,flow_t f){
    int V=(int)graph.size();
    cost_t ret=0;
    using Pi = pair<cost_t,int>;
    priority_queue<Pi,vector<Pi>,greater<Pi>> PQ;
    potential.assign(V,0);
    preve.assign(V,-1);
    prevv.assign(V,-1);

    while(f>0){
      min_cost.assign(V,INF);
      PQ.emplace(0,s);
      min_cost[s]=0;
      while(!PQ.empty()){
        cost_t d;
        int now;
        tie(d,now)=PQ.top();
        PQ.pop();
        if(min_cost[now]<d) continue;
        
        for(int i=0;i<graph[now].size();i++){
          edge &e=graph[now][i];
          cost_t nextCost=min_cost[now]+(e.cost+potential[now]-potential[e.to]);
          if(e.cap>0&&min_cost[e.to]>nextCost){
            min_cost[e.to]=nextCost;
            prevv[e.to]=now;
            preve[e.to]=i;
            PQ.emplace(min_cost[e.to],e.to);
          }
        }
      }

      if(min_cost[t]==INF) return -1;

      for(int v=0;v<V;v++) potential[v]+=min_cost[v];
      flow_t addflow=f;
      for(int v=t;v!=s;v=prevv[v]){
        addflow=min(addflow,graph[prevv[v]][preve[v]].cap);
      }
      f-=addflow;
      ret+=addflow*potential[t];
      
      for(int v=t;v!=s;v=prevv[v]){
        edge &e=graph[prevv[v]][preve[v]];
        e.cap-=addflow;
        graph[v][e.rev].cap+=addflow;
      }
    }
    return ret;
  }
};

const int INF=1e9;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,M;
  cin>>N>>M;
  vector<string> S(N);
  rep(i,N) cin>>S[i];

  int ofs=N*M;
  MinCostFlow<int,int> mcf(ofs+2);
  int s=ofs,t=s+1;
  auto conv = [&](int a,int b){
    return a*M+b;
  };

  ll B=100000;

  ll defo=0;
  int num=0;
  for(int i=0;i<N;i++) for(int j=0;j<M;j++){
    if(S[i][j]=='#') continue;
    if(S[i][j]=='o'){
        defo+=i+j;
        num++;
        mcf.add_edge(s,conv(i,j),1,0);
    }
    mcf.add_edge(conv(i,j),t,1,B-(i+j));
    if(i+1<N&&S[i+1][j]!='#') mcf.add_edge(conv(i,j),conv(i+1,j),ofs,0);
    if(j+1<M&&S[i][j+1]!='#') mcf.add_edge(conv(i,j),conv(i,j+1),ofs,0);
  }

  int ans=mcf.min_cost_flow(s,t,num);
  ans-=B*num;
  ans=abs(ans);
  ans-=defo;
  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 4204