//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <functional>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <iterator>
#include <bitset>
#include <complex>
#include <limits>
#include <random>
#include<fstream>
#include<array>


using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;

const double EPS = 1e-9;

struct edge
{
	int to, cap, cost, rev;
	edge(int to, int cap, int cost, int rev) :to(to), cap(cap), cost(cost), rev(rev) {}
	edge() :to(-1), cap(-1), cost(-1), rev(-1) {}
};

vector<vector<edge>> G;
vector<int> h, dist, prevv, preve;

int min_cost_flow(int s, int t, int f)
{
	int res = 0;
	fill(all(h), 0);
	while (f > 0)
	{
		priority_queue<pii, vector<pii>, greater<pii>> q;
		fill(all(dist), INF);
		dist[s] = 0;
		q.push(pii(0, s));
		while (!q.empty())
		{
			pii p = q.top(); q.pop();
			int v = p.second;
			if (dist[v] < p.first)continue;
			rep(i, 0, SZ(G[v]))
			{
				edge& e = G[v][i];
				if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to])
				{
					dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
					prevv[e.to] = v;
					preve[e.to] = i;
					q.push(pii(dist[e.to], e.to));
				}
			}
		}
		if (dist[t] == INF)
		{
			return -1;
		}
		rep(v, 0, SZ(h))h[v] += dist[v];

		int d = f;
		for (int v = t; v != s; v = prevv[v])
		{
			chmin(d, G[prevv[v]][preve[v]].cap);
		}
		f -= d;
		res += d * h[t];
		for (int v = t; v != s; v = prevv[v])
		{
			edge& e = G[prevv[v]][preve[v]];
			e.cap -= d;
			G[v][e.rev].cap += d;
		}
	}
	return res;
}

void add_edge(int from, int to, int cap, int cost)
{
	G[from].push_back(edge(to, cap, cost, SZ(G[to])));
	G[to].push_back(edge(from, 0, -cost, SZ(G[from]) - 1));
}

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

	static const int dx[] = { 1,0 };
	static const int dy[] = { 0,1 };
	int H, W;
	cin >> H >> W;

	vector<string> smap(H);
	int V = 0;
	rep(i, 0, H)
	{
		cin >> smap[i];
		rep(j, 0, W)if (smap[i][j] == 'o')V++;
	}

	G = vector<vector<edge>>(V + H * W + 2);
	h = vector<int>(V + H * W + 2, 0);
	dist = vector<int>(V + H * W + 2, 0);
	prevv = vector<int>(V + H * W + 2, 0);
	preve = vector<int>(V + H * W + 2, 0);

	int NAX = (int)1e10;

	int v = 0;
	rep(i, 0, H)rep(j, 0, W)
	{
		if (smap[i][j] == 'o')
		{
			vector<vector<bool>> checked(H, vector<bool>(W, false));

			function<void(int, int)> dfs = [&](int x, int y)
			{
				add_edge(v, V+y*W + x, 1, NAX - (abs(x - j) + abs(y - i)));
				
				rep(k, 0, 2)
				{
					int nx = x + dx[k], ny = y + dy[k];
					if (0 <= nx && nx < W && 0 <= ny && ny < H&&smap[ny][nx] != '#'&&!checked[ny][nx])
					{
						checked[ny][nx] = true;
						dfs(nx, ny);
					}
				}
			};

			dfs(j, i);
			v++;
		}
	}

	rep(i, 0, V)
	{
		add_edge(V + H * W, i, 1, 0);
	}
	rep(i, 0, H*W)
	{
		add_edge(V + i, V + H * W + 1, 1, 0);
	}

	int res = V * NAX - min_cost_flow(V + H * W, V + H * W + 1, V);
	cout << res << endl;

	return 0;
}


Measured memory_bytes: 9172