#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <time.h>
#include <atcoder/mincostflow>

#pragma warning(disable:4996) 

typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define LINF3 1000000000000
#define INF 2140000000
#define INF2 100000000
//const long long MOD = 1000000007;
long long MOD = 998244353;

using namespace std;
using namespace atcoder;

typedef pair<ll, int> P;
vector<ll> dijkstra(int s, const vector<vector<pair<int, int> > >& G) {
    priority_queue< P, vector<P>, greater<P> > que;
    vector<ll> d(G.size(), LINF);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
        int curr = que.top().second;
        ll  dcurr = que.top().first;
        que.pop();
        if (d[curr] < dcurr) continue;
        int i;
        for (i = 0; i < (int)G[curr].size(); i++) {
            int next = G[curr][i].first;
            ll  dist = G[curr][i].second;
            if (d[next] > d[curr] + dist) {
                d[next] = d[curr] + dist;
                que.push(P(d[next], next));
            }
        }
    }
    return d;
}

char str[55][55];

void solve()
{
    int n, m;
    scanf("%d%d", &n, &m);
    int i, j;
    for (i = 0; i < n; i++) {
        scanf("%s", str[i]);
    }
#define POSI(i,j) ((i)*m+(j))

    int num = n * m;

    vector<vector<pair<int, int> > > gg(num);
    vector<int> v;
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) {
            if (str[i][j] == '#') continue;
            if (str[i][j] == 'o') v.push_back(POSI(i, j));

            {
                int i2 = i + 1, j2 = j;
                if (i2 < n && str[i2][j2]!='#') {
                    gg[POSI(i, j)].push_back(make_pair(POSI(i2, j2),1));
                }
            }
            {
                int i2 = i, j2 = j+1;
                if (j2 < m && str[i2][j2] != '#') {
                    gg[POSI(i, j)].push_back(make_pair(POSI(i2, j2), 1));
                }
            }
        }
    }

    int sizv = v.size();
    vector<vector<ll> > dist(sizv);
    for (i = 0; i < sizv; i++) {
         dist[i] = dijkstra(v[i], gg);
    }
    
    /**
     * generate (s -> o -> all -> t) graph
     * i-th o    correspond to vertex i
     * i-th posi correspond to vertex sizv + i
     **/
    mcf_graph<int, long long> g(sizv + num + 2);
    int s = sizv+num, t = s+1;
    const long long BIG = 1000000000;

    // we can "waste" the flow
    //g.add_edge(s, t, sizv, BIG);

    int k;
    for (k = 0; k < sizv; k++) {
        g.add_edge(s, k, 1, 0);
    }
    for (i = 0; i < num; i++) {
        g.add_edge(sizv+i, t, 1, 0);
    }
    for (k = 0; k < sizv; k++) {
        for (i = 0; i < num; i++) {
            if (dist[k][i] != LINF) {
                g.add_edge(k, sizv + i, 1, BIG-dist[k][i]);
            }
        }
    }
    auto result = g.flow(s, t, sizv);
    printf("%lld\n", sizv*BIG-result.second);

    return;
}

int main()
{
#if 1
    solve();
#else
    pre();

    int T;
    scanf("%d", &T);
    int t;
    for (t = 0; t < T; t++) {
        //printf("Case #%d: ", t + 1);
        solve();
    }
#endif
    return 0;
}



Measured memory_bytes: 10496