/*************************************************
Author :supermaker
Created Time :2016/4/4 20:01:51
File Location :C:\Users\abcd\Desktop\TheEternalPoet
**************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <set>
#include <bitset>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <complex>
#include <numeric>
#include <functional>
#include <cmath>
#include <ctime>
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
using namespace std;
#define CFF freopen ("aaa.txt", "r", stdin)
#define CPPFF ifstream cin ("aaa.txt")
#define DB(ccc) cout << #ccc << " = " << ccc << endl
#define PB push_back
#define MP(A, B) make_pair(A, B)
typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
typedef pair <int, int> PII;
typedef pair <int, bool> PIB;
const int INF = 0x7F7F7F7F;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = 2 * acos (0.0);
const int maxn = 200 + 66;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
struct Node
{
int to, nxt, c, f, cost;
Node () {}
Node (int tt, int nn, int cc, int ff, int cco) : to (tt), nxt (nn), c (cc), f (ff), cost (cco) {}
};
Node edge[maxn*maxn];
int head[maxn], edgelen;
void Init ()
{
memset (head, -1, sizeof (head));
memset (edge, -1, sizeof (edge));
edgelen = 0;
}
void AddEdge (int cost, int u, int v, int w, int rw = 0)
{
edge[edgelen].to = v, edge[edgelen].c = w, edge[edgelen].f = 0;
edge[edgelen].cost = cost, edge[edgelen].nxt = head[u], head[u] = edgelen++;
edge[edgelen].to = u, edge[edgelen].c = rw, edge[edgelen].f = 0;
edge[edgelen].cost = -cost, edge[edgelen].nxt = head[v], head[v] = edgelen++;
}
int dis[maxn], cnt[maxn], inq[maxn], par[maxn];
int N;
bool spfa (int s, int e)
{
for (int i = 0; i <= N; i++)
{
dis[i] = INF;
inq[i] = cnt[i] = 0;
par[i] = -1;
}
dis[s] = 0;
queue <int> qu;
qu.push (s);
inq[s]++, cnt[s]++;
while (!qu.empty ())
{
int u = qu.front (); qu.pop (); inq[u]--;
if (cnt[u] > N) return false;
for (int i = head[u]; i != -1; i = edge[i].nxt)
{
int v = edge[i].to;
if (edge[i].c > edge[i].f && dis[u] < INF && dis[v] > dis[u] + edge[i].cost)
{
dis[v] = dis[u] + edge[i].cost;
par[v] = i;
if (!inq[v])
{
qu.push (v);
inq[v]++, cnt[v]++;
}
}
}
}
if (par[e] == -1) return false;
return true;
}
int allcost;
int MinCostMaxFlow (int s, int e)
{
int res = 0;
allcost = 0;
while (spfa (s, e))
{
int minval = INF;
for (int i = par[e]; i != -1; i = par[edge[i^1].to])
minval = min (minval, edge[i].c - edge[i].f);
for (int i = par[e]; i != -1; i = par[edge[i^1].to])
{
edge[i].f += minval;
edge[i^1].f -= minval;
allcost += edge[i].cost * minval;
}
res += minval;
}
return res;
}
char g[maxn][maxn];
int id[maxn][maxn], len, row, col;
bool vis[maxn][maxn];
inline bool InLine (int x, int y)
{
if (x >= 1 && x <= row && y >= 1 && y <= col) return true;
return false;
}
void bfs (int x, int y)
{
memset (vis, false, sizeof (vis));
vis[x][y] = true;
queue <Node> qu;
qu.push (Node (x, y, 0, 0, 0));
while (!qu.empty ())
{
Node tt = qu.front (); qu.pop ();
if (g[tt.to][tt.nxt] == 'H')
{
AddEdge (tt.c, id[x][y], id[tt.to][tt.nxt], 1);
//cout << id[x][y] << " " << id[tt.to][tt.nxt] << " " << tt.c << endl;
}
for (int i = 0; i < 4; i++)
{
int xx = tt.to + dx[i], yy = tt.nxt + dy[i];
if (InLine (xx, yy) && !vis[xx][yy])
{
vis[xx][yy] = true;
qu.push (Node (xx, yy, tt.c + 1, 0, 0));
}
}
}
}
int main()
{
//CFF;
//CPPFF;
while (scanf ("%d%d", &row, &col) != EOF)
{
if (row == 0 && col == 0) break;
memset (id, -1, sizeof (id));
len = 1;
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++)
{
scanf (" %c", &g[i][j]);
if (g[i][j] != '.')
id[i][j] = len++;
}
Init ();
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++)
if (g[i][j] == 'm')
bfs (i, j);
len--;
N = len + 2;
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++)
{
if (g[i][j] == 'm')
AddEdge (0, 0, id[i][j], 1);
else if (g[i][j] == 'H')
AddEdge (0, id[i][j], len + 1, 1);
}
MinCostMaxFlow (0, len + 1);
printf ("%d\n", allcost);
}
return 0;
}