using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Numerics;

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    int[] Hungarian(int[,] a, int n, int m)
    {
        const int INF = int.MaxValue / 2;
        var u = new int[n + 1];
        var v = new int[m + 1];
        var p = new int[m + 1];
        var way = new int[m + 1];
        for (int i = 1; i <= n; ++i)
        {
            p[0] = i;
            int j0 = 0;
            var minv = Enumerable.Repeat(INF, m + 1).ToArray();
            var used = new bool[m + 1];
            do
            {
                used[j0] = true;
                int i0 = p[j0], delta = INF, j1 = 0;
                for (int j = 1; j <= m; ++j)
                    if (!used[j])
                    {
                        int cur = a[i0, j] - u[i0] - v[j];
                        if (cur < minv[j])
                        {
                            minv[j] = cur;
                            way[j] = j0;
                        }
                        if (minv[j] < delta)
                        {
                            delta = minv[j];
                            j1 = j;
                        }
                    }
                for (int j = 0; j <= m; ++j)
                    if (used[j])
                    {
                        u[p[j]] += delta;
                        v[j] -= delta;
                    }
                    else
                    {
                        minv[j] -= delta;
                        j0 = j1;
                    }
            } while (p[j0] != 0);
            do
            {
                int j1 = way[j0];
                p[j0] = p[j1];
                j0 = j1;
            } while (j0 != 0);
        }

        Write(v[0]);

        return p;
    }

    public void Solve()
    {
        int n = ReadInt();
        int m = ReadInt();
        var a = ReadLines(n);

        var id = new int[n, m];
        int cnt = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (a[i][j] != '#')
                {
                    id[i, j] = cnt++;
                }

        var g = new List<List<(int, int)>>();
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (a[i][j] == 'o')
                {
                    var f = new bool[n, m];
                    var list = new List<(int, int)>();
                    var q = new Queue<(int, int)>();
                    q.Enqueue((i, j));
                    f[i, j] = true;
                    int step = 0;
                    while (q.Count > 0)
                    {
                        var nq = new Queue<(int, int)>();
                        while (q.Count > 0)
                        {
                            var t = q.Dequeue();
                            list.Add((id[t.Item1, t.Item2], step));
                            if (t.Item1 < n - 1 && a[t.Item1 + 1][t.Item2] != '#' && !f[t.Item1 + 1, t.Item2])
                            {
                                f[t.Item1 + 1, t.Item2] = true;
                                nq.Enqueue((t.Item1 + 1, t.Item2));
                            }
                            if (t.Item2 < m - 1 && a[t.Item1][t.Item2 + 1] != '#' && !f[t.Item1, t.Item2 + 1])
                            {
                                f[t.Item1, t.Item2 + 1] = true;
                                nq.Enqueue((t.Item1, t.Item2 + 1));
                            }
                        }
                        q = nq;
                        step++;
                    }
                    g.Add(list);
                }

        int no = g.Count;
        var g2 = new int[no + 1, cnt + 1];
        for (int i = 1; i <= no; i++)
            for (int j = 1; j <= cnt; j++)
                g2[i, j] = int.MaxValue / 2;
        for (int i = 0; i < no; i++)
            foreach (var e in g[i])
                g2[i + 1, e.Item1 + 1] = -e.Item2;

        Hungarian(g2, no, cnt);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            new Solver().Solve();
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.WriteLine(ex);
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0) currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++) ret[i][j] = matrix[j][i]; }
        return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++) lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array) writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++) ret[i] = new T(); return ret; }
    #endregion
}

Measured memory_bytes: 25304