#include <bits/stdc++.h>
using namespace std;
int N;
int S;
vector<pair<int,int>> trans = {{1,0}, {-1,0},{0,1},{0,-1}};
char clst[810][810];
int s1,s2;
int f1,f2;
queue<pair<int,int>> q[2];
queue<pair<int,int>> m[2];
bool visited[810][810];
bool vt[810][810];
int cq,cm;
bool check(int i, int j){
if(i < 0) return false;
if(i >= N) return false;
if(j < 0) return false;
if(j >= N) return false;
return true;
}
void spreadhoney(int it){
while(!q[it].empty()){
pair<int,int> ii = q[it].front();
q[it].pop();
int i = ii.first;
int j = ii.second;
visited[i][j] = true;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = ii.first + i;
int nj = ii.second + j;
if(!check(ni,nj)) continue;
if(visited[ni][nj]) continue;
q[1 - it].push({ni,nj});
}
}
}
int main(){
scanf(" %d",&N);
scanf(" %d",&S);
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
scanf(" %c",&clst[i][j]);
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
visited[i][j] = false;
vt[i][j] = false;
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(clst[i][j] == 'T'){
visited[i][j] = true;
vt[i][j] = true;
}
else if(clst[i][j] == 'H'){
q[1].push({i,j});
}
else if(clst[i][j] == 'M'){
s1 = i;
s2 = j;
}
else if(clst[i][j] == 'D'){
f1 = i;
f2 = j;
visited[i][j] = true;
}
}
}
cq = 0;
cm = 0;
spreadhoney(1);
vt[s1][s2] = true;
for(pair<int,int> ii : trans){
int ni = s1 + ii.first;
int nj = s2 + ii.second;
if(!check(ni,nj)) continue;
m[0].push({ni,nj});
}
bool flag;
for(int i = 0; i < N * N; i++){
q[0].push_back({i,i});
}
return 0;
while(true){
int it = cm % 2;
bool done = false;
for(int i = 0; i < S; i++, cm++){
it = cm % 2;
while(!m[it].empty()){
pair<int,int> ii = m[it].front();
m[it].pop();
int i = ii.first;
int j = ii.second;
if(i == f1 && j == f2){
done = true;
flag = true;
break;
}
if(vt[i][j]) continue;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = i + ii.first;
int nj = j + ii.second;
if(!check(ni,nj)) continue;
if(vt[ni][nj]) continue;
m[1 - it].push({ni,nj});
}
}
if(done) break;
if(m[1 - it].empty()){
done = true;
flag = false;
break;
}
}
if(done) break;
it = cq % 2;
spreadhoney(it);
cq++;
}
if(!flag || true){
printf("-1");
return 0;
}
int s = 0;
int e = 800*800;
int m1;
while(s != e){
m1 = (s + e + 1)/2;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
visited[i][j] = false;
vt[i][j] = false;
}
}
while(!m[0].empty()) m[0].pop();
while(!m[1].empty()) m[1].pop();
while(!q[0].empty()) q[0].pop();
while(!q[1].empty()) q[1].pop();
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(clst[i][j] == 'T'){
visited[i][j] = true;
vt[i][j] = true;
}
else if(clst[i][j] == 'H'){
q[1].push({i,j});
}
else if(clst[i][j] == 'D'){
visited[i][j] = true;
}
}
}
cq = 0;
cm = 0;
spreadhoney(1);
vt[s1][s2] = true;
for(pair<int,int> ii : trans){
int ni = s1 + ii.first;
int nj = s2 + ii.second;
if(!check(ni,nj)) continue;
m[0].push({ni,nj});
}
for(int i = 0; i < m1; i++, cq++){
int it = cq % 2;
spreadhoney(it);
}
if(visited[s1][s2]){
e = m1 - 1;
continue;
}
/*for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%d ",visited[i][j]);
}
printf("\n");
}
printf("\n");*/
bool flag;
while(true){
int it = cm % 2;
bool done = false;
for(int i = 0; i < S; i++, cm++){
it = cm % 2;
while(!m[it].empty()){
pair<int,int> ii = m[it].front();
m[it].pop();
int i = ii.first;
int j = ii.second;
//printf("%d %d\n",i,j);
if(i == f1 && j == f2){
done = true;
flag = true;
break;
}
if(vt[i][j]) continue;
vt[i][j] = true;
for(pair<int,int> ii : trans){
int ni = i + ii.first;
int nj = j + ii.second;
if(!check(ni,nj)) continue;
m[1 - it].push({ni,nj});
}
}
//printf("\n");
if(done) break;
if(m[1 - it].empty()){
done = true;
flag = false;
break;
}
}
if(done) break;
it = cq % 2;
spreadhoney(it);
cq++;
}
if(flag){
s = m1;
}
else{
e = m1 - 1;
}
}
printf("%d",s);
}
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGG
MGGGGGD
TGGTTTT
TGGGGGT
TTTTTHT
*/
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:102:14: error: 'class std::queue<std::pair<int, int> >' has no member named 'push_back'
102 | q[0].push_back({i,i});
| ^~~~~~~~~
mecho.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
mecho.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf(" %d",&S);
| ~~~~~^~~~~~~~~~
mecho.cpp:52:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf(" %c",&clst[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~~