#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<typename F, typename C>
struct PrimalDual {
    struct edge {
        int to; F cap; C cost; int rev;
        edge() = default;
        edge(int to, F cap, C cost, int rev):to(to), cap(cap), cost(cost), rev(rev) {};
    };
    vector<vector<edge>> G;
    vector<C> potential, min_cost;
    vector<int> prevv, preve;

    explicit PrimalDual(int n) : G(n), potential(n), min_cost(n), prevv(n), preve(n) {}

    void add_edge(int u, int v, F cap, C cost){
        G[u].emplace_back(v, cap, cost, G[v].size());
        G[v].emplace_back(u, 0, -cost, G[u].size()-1);
    }

    struct P{
        C first; int second;
        P(C first,int second):first(first),second(second){}
        bool operator<(const P&a) const{return a.first<first;}
    };
    void dijkstra(int s){

        priority_queue<P> Q;
        fill(min_cost.begin(),min_cost.end(), INF<C>);
        min_cost[s] = 0;
        Q.emplace(0, s);
        while(!Q.empty()){
            P p = Q.top(); Q.pop();
            int v = p.second;
            if(min_cost[v] < p.first) continue;
            for(int i = 0; i < G[v].size(); ++i){
                edge &e=G[v][i];
                if(e.cap==0) continue;
                if(min_cost[v]+e.cost+potential[v]-potential[e.to] < min_cost[e.to]){
                    min_cost[e.to] = min_cost[v]+e.cost+potential[v]-potential[e.to];
                    prevv[e.to] = v;
                    preve[e.to] = i;
                    Q.emplace(min_cost[e.to], e.to);
                }
            }
        }
    }

    C flow(int s, int t, F fl, int &ok){
        C res = 0;
        fill(potential.begin(),potential.end(), 0);
        while(fl > 0){
            dijkstra(s);
            if(min_cost[t] == INF<C>){
                ok = 0;
                return res;
            }
            for (int i = 0; i < potential.size(); ++i) {
                if(min_cost[i] < INF<C>) potential[i] += min_cost[i];
            }

            F d = fl;
            for(int v = t; v != s; v = prevv[v]){
                d = min(d, G[prevv[v]][preve[v]].cap);
            }
            fl -= d;
            res += potential[t]*d;
            for(int v = t; v != s; v = prevv[v]){
                G[prevv[v]][preve[v]].cap -= d;
                G[v][G[prevv[v]][preve[v]].rev].cap += d;
            }
        }
        ok = 1;
        return res;
    }
};
template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int n, m;
    cin >> n >> m;
    vector<string> v(n);
    for (auto &&i : v) cin >> i;
    vector<int> ys, xs;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if(v[i][j] == 'o') {
                ys.emplace_back(i);
                xs.emplace_back(j);
            }
        }
    }
    int k = ys.size();
    PrimalDual<int, int> G(2 + k + n*m);
    auto d = make_v(n, m, INF<int>);
    auto get = [&](int sy, int sx){
        for (auto &&i : d) for (auto &&j : i) j = INF<int>;
        d[sy][sx] = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if(i+1 != n && v[i+1][j] != '#') chmin(d[i+1][j], d[i][j]+1);
                if(j+1 != m && v[i][j+1] != '#') chmin(d[i][j+1], d[i][j]+1);
            }
        }
    };
    for (int i = 0; i < k; ++i) {
        G.add_edge(0, i+1, 1, 0);
    }
    for (int i = 0; i < n * m; ++i) {
        G.add_edge(1+k+i, 1+k+n*m, 1, 0);
    }
    for (int i = 0; i < k; ++i) {
        get(ys[i], xs[i]);
        for (int y = 0; y < n; ++y) {
            for (int x = 0; x < m; ++x) {
                if(d[y][x] != INF<int>){
                    G.add_edge(i+1, 1+k+y*m+x, 1, -d[y][x]);
                }
            }
        }
    }
    int ok;
    cout << -G.flow(0, 1+k+n*m, k, ok) << "\n";
    return 0;
}

Measured memory_bytes: 7036