#include <bits/stdc++.h>

#include <atcoder/all>

using namespace std;
using namespace atcoder;

using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U>
void chmin(T& t, const U& u) {
    if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
    if (t < u) t = u;
}

template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << "(" << p.first << "," << p.second << ")";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    rep(i, v.size()) {
        if (i) os << ",";
        os << v[i];
    }
    os << "}";
    return os;
}

#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

using Mint = modint998244353;

bool vis[60][60];

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    V<string> st(N);
    V<pii> ps;

    rep(i, N) {
        cin >> st[i];
        rep(j, M) {
            if (st[i][j] == 'o') {
                ps.eb(i, j);
                st[i][j] = '.';
            }
        }
    }

    int sz = ps.size();
    int nodes = sz + N * M + 2;
    int s = nodes - 2, t = nodes - 1;

    mcf_graph<int, int> g(nodes);
    V<pair<pii, pii>> edges;

    rep(i, sz) {
        queue<pii> que;
        memset(vis, 0, sizeof(vis));
        int r, c;
        tie(r, c) = ps[i];
        vis[r][c] = 1;

        edges.eb(mp(s, i), mp(1, 0));

        rep(a, N) {
            rep(b, M) {
                if (vis[a][b]) {
                    if (a + 1 < N && st[a + 1][b] == '.') {
                        vis[a + 1][b] = 1;
                    }
                    if (b + 1 < M && st[a][b + 1] == '.') {
                        vis[a][b + 1] = 1;
                    }
                    edges.eb(mp(i, sz + a * M + b), mp(1, a - r + b - c));
                }
            }
        }
    }

    rep(a, N) rep(b, M) if (st[a][b] == '.') {
        edges.eb(mp(sz + a * M + b, t), mp(1, 0));
    }
    ll mx = 0;
    for (auto e : edges) {
        chmax(mx, e.se.se);
    }

    for (auto e : edges) {
        if (e.fi.fi == s || e.fi.se == t) {
            g.add_edge(e.fi.fi, e.fi.se, e.se.fi, e.se.se);
        } else {
            g.add_edge(e.fi.fi, e.fi.se, e.se.fi, mx - e.se.se);
        }
    }

    auto ans = g.flow(s, t, sz);
    debug(sz, ans, mx * sz);
    cout << mx * sz - ans.se << endl;

    return 0;
}

Measured memory_bytes: 8548