#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll; typedef vector<ll> vll; typedef map<ll,ll> mll;
mt19937_64 rng(chrono::high_resolution_clock().now().time_since_epoch().count());
const ll maxn = 8e2 + 6, inf = 1e18 + 7, mod = 1e9 + 7;
ll n, k, ans = -1, a[maxn][maxn], b[maxn][maxn], dir[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}};
map<char,ll> m = {{'G', 0}, {'T', 1}, {'M', 2}, {'D', 3}, {'H', 4}};
pll posm, posd;
bool check(ll x, ll y, ll a[maxn][maxn]) {
	if (x < 1 || x > n || y < 1 || y > n) return 0;
	if (a[x][y] == 1 || a[x][y] == 4) return 0;
	return 1;
}
queue<pll> q;
ll dist[maxn][maxn];
void multisource_bfs() {
	fill(&dist[0][0], &dist[maxn][0], inf);
	for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) {
		if (a[i][j] == 4) q.push({i,j}), dist[i][j] = 0;
	}
	while (!q.empty()) {
		auto [x,y] = q.front();
		q.pop();
		for (int d = 0; d < 4; ++d) {
			ll nx = x + dir[d][0], ny = y + dir[d][1];
			if (!check(nx,ny,a) || a[nx][ny] == 3) continue;
			if (dist[nx][ny] == inf) {
				dist[nx][ny] = dist[x][y] + 1;
				q.push({nx,ny});
			}
		}
		
	}
}
void build(ll mid) {
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			if (!a[i][j] && dist[i][j] <= mid) b[i][j] = 4;
			else b[i][j] = a[i][j];
		}
	}
}
ll distm[maxn][maxn];
void bfs(ll dist[maxn][maxn]) {
	while (!q.empty()) q.pop();
	fill(&dist[0][0], &dist[maxn][0], inf);
	q.push(posm);
	dist[posm.first][posm.second] = 0;
	while (!q.empty()) {
		auto [x,y] = q.front();
		q.pop();
		for (int d = 0; d < 4; ++d) {
			ll nx = x + dir[d][0], ny = y + dir[d][1];
			if (!check(nx,ny,b)) continue;
			if (dist[nx][ny] == inf) {
				dist[nx][ny] = dist[x][y] + 1;
				q.push({nx,ny});
			}
		}
	}
}
bool checkbs(ll mid) {
	auto [dx,dy] = posd;
	for (int d = 0; d < 4; ++d) {
		ll x = dx + dir[d][0], y = dy + dir[d][1];
		if (!check(x,y,a)) continue;
		ll time_for_mecho = distm[x][y] / k, time_for_bees = dist[x][y] - mid;
		if (time_for_mecho < time_for_bees) return 1; 
		if (time_for_mecho == time_for_bees && dist[x][y] % k != 0) return 1;
	}
	return 0;
}
void solve() {
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		string s;
		cin >> s;
		s = " " + s;
		for (int j = 1; j <= n; ++j) {
			a[i][j] = m[s[j]];
			if (a[i][j] == 2) posm = {i,j};
			if (a[i][j] == 3) posd = {i,j};
		}
	}
	multisource_bfs();
	ll l = 0, r = inf;
	while (l <= r) {
		ll mid = (l+r) / 2;
		build(mid);
		bfs(distm);
		if (checkbs(mid)) {
			ans = mid;
			l = mid+1;
		} else r = mid-1;
	}
	cout << ans;
	return;
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	if (fopen(".INP", "r")) {
		freopen(".INP", "r", stdin);
		freopen(".OUT", "w", stdout);
	}
	ll testCase = 1;
//	cin >> testCase;
	while (testCase--) {
		solve();
	}
	return 0;
}
Compilation message (stderr)
mecho.cpp: In function 'int main()':
mecho.cpp:111:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |                 freopen(".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~
mecho.cpp:112:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |                 freopen(".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |