On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man. 

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point. 


You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

最大流可以解决一些简单的匹配问题

而费用流在最大流的基础上又增加了选择最小费用的操作

这就使得费用流算法在日常生活着有着很多的应用

(如果 自己能督促一下自己 早点学习 费用流 那么省赛可能就金了)

代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;

int cnt1=0,cnt2=0;
char mp[105][105];
pair<int,int > man[105];
pair<int, int > house[105];

const int maxn = 205;
const int maxm = 100500;
const int inf = 0x3f3f3f3f;
struct fuck//从u到v
{
    int v, ne, cap, flow, cost, u;
} ed[maxm];
int head[maxn], cnt;//前n个点 n+1_2n
int pre[maxn], dis[maxn];
bool vis[maxn];
int n, m;
void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}
void add(int u, int v, int cap, int cost)//填边函数
{
    ed[cnt].v = v;
    ed[cnt].cap = cap;
    ed[cnt].flow = 0;
    ed[cnt].u = u;
    ed[cnt].cost = cost;
    ed[cnt].ne = head[u];
    head[u] = cnt++;

    ed[cnt].v = u;
    ed[cnt].cap = 0;
    ed[cnt].flow = 0;
    ed[cnt].u = v;
    ed[cnt].cost =-cost;
    ed[cnt].ne = head[v];
    head[v] = cnt++;
}
bool spfa(int st, int en)
{
    queue<int>q;
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    dis[st] = 0;//将初始点带入
    vis[st] = 1;
    q.push(st);
    while (q.size())//spfa
    {
        //cout<<"*"<<endl;
        int u = q.front();
        //cout<<u<<endl;
        q.pop();
        vis[u] = 0;
        for (int s = head[u]; s!=-1; s = ed[s].ne)
        {
            //cout<<s<<endl;
            int v = ed[s].v;
            if (ed[s].cap > ed[s].flow&&dis[v] > dis[u] + ed[s].cost)
            {
               // cout<<ed[s].cap<<ed[s].flow<<endl;
                dis[v] = dis[u] + ed[s].cost;
                //cout<<dis[v]<<endl;
                pre[v] = s;
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    //cout<<"/"<<endl;
    if (pre[en] == -1)return 0;
    return 1;
}
int MinMax(int st, int en, long long &cost)
{
    int flow = 0;
    cost = 0;
    while (spfa(st, en))
    {
        //cout<<"d"<<endl;
        int Min = inf;
        for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v])
        {
            if (Min > ed[s].cap - ed[s].flow)
                Min = ed[s].cap - ed[s].flow;
        }

        for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v])
        {
            //cout<<"2";
            ed[s].flow += Min;
            ed[s ^ 1].flow -= Min;
            cost += ed[s].cost*Min;
        }
        flow += Min;
    }
    return flow;
}


int dist(pair<int,int> p,pair<int,int> q)
{
    return abs(p.first-q.first)+abs(p.second-q.second);
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) return 0;
        init();
        cnt1=0;
        cnt2=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%s",mp[i]+1);
            for(int j=1; j<=m; j++)
            {
                if(mp[i][j]=='m') cnt1++,man[cnt1].first=i,man[cnt1].second=j;
                if(mp[i][j]=='H') cnt2++,house[cnt2].first=i,house[cnt2].second=j;
            }
        }
        for(int i=1; i<=cnt1; i++)
        {
            add(0,i,1,0);
            add(i+cnt1,2*cnt1+1,1,0);
        }
        for(int i=1; i<=cnt1; i++)
        {
            for(int j=1; j<=cnt1; j++)
            {
                add(i,j+cnt1,1,dist(man[i],house[j]));
            }
        }
        long long ans=0;
        MinMax(0,2*cnt1+1,ans);
        printf("%lld\n",ans);
    }
}