#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
//C:flow_type,D:cost_type,E:edge_class
template<typename C, typename D, class E>
struct MinCostFlow{
    const D INF = numeric_limits<D>::max();
    vector<vector<E>> g;
    int n;
    int s, t;
    vector<int> pv, pe;
    vector<D> h;
    MinCostFlow(vector<vector<E>> _g, int _s, int _t, bool negative):g(_g),n((int)g.size()), s(_s), t(_t),pv(n),pe(n),h(n){
        assert(s != t);
        if(negative){
            vector<D> dist(n, INF);
            dist[s] = 0;
            for(int c = 0; c < n; c++){
                for(int i = 0; i < n; i++){
                    for(auto &e : g[i]){
                        if(e.cap > 0 and dist[i] < INF){
                            dist[e.to] = min(dist[e.to], dist[i] + e.cost);
                        }
                    }
                }
            }
            for(int i = 0; i < n; i++){
                h[i] += dist[i];
            }
        }
    }
    D solve(C f){
        D res = 0;
        while(f > 0){
            vector<D> dist(n, INF);
            using P = pair<D, int>;
            priority_queue<P, vector<P>, greater<P>>pq;
            dist[s] = 0;
            pq.emplace(dist[s], s);
            while(!pq.empty()){
                auto x = pq.top();
                pq.pop();
                int v = x.second;
                if(dist[v] < x.first)continue;
                for(int i = 0; i < (int) g[v].size(); i++){
                    const auto &e = g[v][i];
                    if(e.cap <= 0)continue;
                    D temp = dist[v] + e.cost + h[v] - h[e.to];
                    if(dist[e.to] > temp){
                        dist[e.to] = temp;
                        pv[e.to] = v;
                        pe[e.to] = i;
                        pq.emplace(dist[e.to], e.to);
                    }
                }
            }
            if(dist[t] == INF){
                return D(-1);
            }
            for(int i = 0; i < n; i++){
                h[i] += dist[i];
            }
            C d = f;
            for(int v = t; v != s; v = pv[v]){
                d = min(d, g[pv[v]][pe[v]].cap);
            }
            f -= d;
            res += h[t] * d;
            for(int v = t; v != s; v = pv[v]){
                auto &e = g[pv[v]][pe[v]];
                e.cap -= d;
                g[v][e.rev].cap += d;
            }
        }
        return res;
    }
};
template<class C, class D, class E>
MinCostFlow<C, D, E> get_mcf(const vector<vector<E>>& g, int s, int t, bool negative = false){
    return MinCostFlow<C, D, E>(g, s, t, negative);
}

int main(){
    struct E{
        int to, rev, cap, cost;
    };
    int n, m;
    cin >> n >> m;
    vector<vector<E>> g(n * m + 2);
    auto add_edge = [&](int from, int to, int cap, int dist) {
        g[from].push_back(E{to, int(g[to].size()), cap, dist});
        g[to].push_back(E{from, int(g[from].size())-1, 0, -dist});
    };
    const int s = n * m;
    const int t = s + 1;
    vector<string> a(n);
    rep(i,n){
        cin >> a[i];
        rep(j,m){
            if(a[i][j] == 'o'){
                add_edge(s, i * m + j, 1, 0);
            }
        }
    }
    rep(i,n){
        rep(j,m-1){
            if(a[i][j] != '#' and a[i][j+1] != '#'){
                add_edge(i * m + j, i * m + j + 1, 100, 0);
            }
        }
        if(i < n - 1)rep(j,m){
            if(a[i][j] != '#' and a[i + 1][j] != '#'){
                add_edge(i * m + j, (i + 1) * m + j, 100, 0);
            }
        }
    }
    int ans = 0;
    int num = 0;
    const int gap = 10000;
    rep(i,n)rep(j,m){
        if(a[i][j] != '#'){
            add_edge(i * m + j, t, 1, gap - i - j);
        }
        if(a[i][j] == 'o'){
            show(i, j);
            ans -= i + j;
            num++;
        }
    }
    show(num);
    show(ans);
    show(get_mcf<int, int>(g, s, t).solve(num));
    ans -= get_mcf<int, int>(g, s, t).solve(num);
    ans += gap * num;
    cout << ans << endl;
}


Measured memory_bytes: 4292