#ifndef __MIN_COST_FLOW_SOLVER_H__
#define __MIN_COST_FLOW_SOLVER_H__

/* updated: 2020-07-30 */

#include <cstdint>
#include <vector>
#include <limits>

using CostType = int64_t;
using FlowType = int64_t;
using EdgeID = int;

struct WeightedResidualGraphEdge {
	int from;
	int to;
	CostType cost;
	FlowType capacity;
};

// 外から見たらこれが Edge に見えるように設計する
class WeightedResidualGraphItem {
	using Edge = WeightedResidualGraphEdge;
	using EdgeList = std::vector<Edge>;

	// 本当はポインタ (or イテレータ)にしたいが
	EdgeList& edges_m;
	EdgeID index_m;

public:
	explicit WeightedResidualGraphItem(EdgeList& edges, EdgeID index);
	int from() const;
	int to() const;
	CostType cost() const;
	FlowType capacity() const;
	EdgeID getId() const;
};

class WeightedResidualGraph {
public:
	using Edge = WeightedResidualGraphEdge;
	using Item = WeightedResidualGraphItem;
	using List = std::vector<Item>;

private:
	const int size_m;
	std::vector<Edge> edges_m;
	std::vector<List> graph_m;	// to の情報をここに乗せるか？

public:
	explicit WeightedResidualGraph(int size);
	int size() const;

	void add(int from, int to, CostType cost, FlowType capacity);
	List& row(int from);
	const List& row(int from) const;
	Item get(EdgeID index);
	FlowType flow(EdgeID index, FlowType quantity);
};

/* 負辺なし最小費用流問題を解く(計算量 O(FE log V), F は流量) */
class MinimumCostFlowSolver {
	// INF の扱いに注意
	static const CostType InfCost = std::numeric_limits<CostType>::max();

	WeightedResidualGraph graph_m;
	std::vector<CostType> distance_m;
	std::vector<CostType> potential_m;

public:
	explicit MinimumCostFlowSolver(int size);
	void add(int from, int to, CostType cost, FlowType capacity);

private:
	std::pair<CostType, FlowType> flow(int start, int goal, FlowType limit);

public:
	CostType solve(int from, int to, FlowType quantity);
};

#endif

#ifdef ONLY_MY_ENVIR
#include "MinCostFlowSolver.h"
#endif

#include <tuple>
#include <queue>
#include <functional>
#include <limits>

/*-------------------------------------*/
/*---   WeightedResidualGraphItem   ---*/
/*-------------------------------------*/

WeightedResidualGraphItem::WeightedResidualGraphItem(EdgeList& edges, EdgeID index)
	: edges_m(edges), index_m(index) {
}

int WeightedResidualGraphItem::from() const {
	return edges_m[index_m].from;
}

int WeightedResidualGraphItem::to() const {
	return edges_m[index_m].to;
}

CostType WeightedResidualGraphItem::cost() const {
	return edges_m[index_m].cost;
}

FlowType WeightedResidualGraphItem::capacity() const {
	return edges_m[index_m].capacity;
}

EdgeID WeightedResidualGraphItem::getId() const {
	return index_m;
}

/*-------------------------------------*/
/*-----   WeightedResidualGraph   -----*/
/*-------------------------------------*/

WeightedResidualGraph::WeightedResidualGraph(int size)
	: size_m(size), graph_m(size) {
}

int WeightedResidualGraph::size() const {
	return size_m;
}

void WeightedResidualGraph::add(int from, int to, CostType cost, FlowType capacity) {
	Edge forward{ from, to, cost, capacity };
	Edge backward{ to, from, -cost, 0 };
	edges_m.push_back(forward);
	edges_m.push_back(backward);

	int edge_count = edges_m.size();
	graph_m[from].push_back(Item(edges_m, edge_count - 2));
	graph_m[to].push_back(Item(edges_m, edge_count - 1));
}

WeightedResidualGraph::List& WeightedResidualGraph::row(int from) {
	return graph_m[from];
}

const WeightedResidualGraph::List& WeightedResidualGraph::row(int from) const {
	return graph_m[from];
}

WeightedResidualGraph::Item WeightedResidualGraph::get(EdgeID index) {
	return Item(edges_m, index);
}

FlowType WeightedResidualGraph::flow(EdgeID index, FlowType quantity) {
	FlowType capacity = edges_m[index].capacity;
	FlowType real_flow = std::min(capacity, quantity);

	Edge& forward = edges_m[index];
	Edge& backward = edges_m[index ^ 0x01];
	forward.capacity -= real_flow;
	backward.capacity += real_flow;

	return real_flow;
}

/*-------------------------------------*/
/*-----   MinimumCostFlowSolver   -----*/
/*-------------------------------------*/

MinimumCostFlowSolver::MinimumCostFlowSolver(int size)
	: graph_m(size), distance_m(size), potential_m(size) {
}

void MinimumCostFlowSolver::add(int from, int to, CostType cost, FlowType capacity) {
	graph_m.add(from, to, cost, capacity);
}

// (単位フローあたりの費用, その条件で流せるフロー) の組を返す
std::pair<CostType, FlowType> MinimumCostFlowSolver::flow(int start, int goal, FlowType limit) {
	std::fill(distance_m.begin(), distance_m.end(), InfCost);
	std::vector<EdgeID> ids(graph_m.size());

	using State = std::tuple<CostType, int, EdgeID>;	// from はいらない気もする
	std::priority_queue<State, std::vector<State>, std::greater<State>> queue;
	queue.push(State(0, start, -1));

	while (!queue.empty()) {
		State state = queue.top(); queue.pop();
		CostType cost = std::get<0>(state);
		int from = std::get<1>(state);
		EdgeID id = std::get<2>(state);

		if (distance_m[from] < InfCost) continue;
		distance_m[from] = cost;
		ids[from] = id;

		const auto& row = graph_m.row(from);
		for (const auto& edge : graph_m.row(from)) {
			if (edge.capacity() == 0) continue;

			int to = edge.to();
			CostType next_cost = cost + edge.cost() + potential_m[from] - potential_m[to];
			queue.push(State(next_cost, to, edge.getId()));
		}
	}

	if (distance_m[goal] == InfCost) return std::make_pair(InfCost, 0);
	
	// 最大で流せる量を確定
	FlowType max_flow = limit;
	{
		int curr = goal;
		while (curr != start) {
			EdgeID id = ids[curr];

			max_flow = std::min(max_flow, graph_m.get(id).capacity());
			curr = graph_m.get(id).from();
		}
	}

	// 実際に流す
	{
		int curr = goal;
		while (curr != start) {
			EdgeID id = ids[curr];

			graph_m.flow(id, max_flow);
			curr = graph_m.get(id).from();
		}
	}

	CostType min_cost = distance_m[goal] + potential_m[goal];
	return std::make_pair(min_cost, max_flow);
}

CostType MinimumCostFlowSolver::solve(int from, int to, FlowType quantity) {
	FlowType remain = quantity;
	CostType sum = 0;

	while (remain > 0) {
		auto result = flow(from, to, remain);
		CostType cost = result.first;
		FlowType flow = result.second;

		if (flow == 0) return InfCost;
		remain -= flow;
		sum += cost * flow;

		for (int i = 0; i < graph_m.size(); ++i) {
			potential_m[i] += distance_m[i];
		}
	}

	return sum;
}

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h&& j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "Interpolation.h"
#include "IntMod.h"
#include "KDTree.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "List.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RollingHash.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

#include <cctype>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int di[] = { 0, 1 };
const int dj[] = { 1, 0 };
int h, w;
string s[50];

int ver(int i, int j) {
	return i * w + j;
}

int main() {
	cin >> h >> w;
	REP(i, 0, h) cin >> s[i];
	
	MinimumCostFlowSolver fs(h * w + 2);
	int sv = h * w;
	int tv = sv + 1;

	int cnt = 0;
	REP(i, 0, h) {
		REP(j, 0, w) {
			if (s[i][j] == '#') continue;
			REP(k, 0, 2) {
				int ni = i + di[k];
				int nj = j + dj[k];
				if (!Inside(ni, nj, h, w)) continue;
				if (s[ni][nj] == '#') continue;
				fs.add(ver(i, j), ver(ni, nj), 0, INF);
			}

			if (s[i][j] == 'o') {
				fs.add(sv, ver(i, j), i + j, 1);
				++cnt;
			}

			fs.add(ver(i, j), tv, (h + w) - (i + j), 1);
		}
	}

	int ans = fs.solve(sv, tv, cnt);
	ans -= (h + w) * cnt;
	cout << -ans << endl;
	return 0;
}


Measured memory_bytes: 4248