#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
#define el endl
#define int int64_t
#define fi first
#define se second
#define file(x) freopen(x".inp", "r", stdin); freopen(x".out", "w", stdout)
#define pb push_back
#define gd() ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
#define rizzler signed main()
#define tc() int t; cin >> t; while(t--)
void tle() {
double time_taken = 1000.0 * clock() / CLOCKS_PER_SEC;
if (time_taken < 1000.0) {
std::cerr << "Time: ";
std::cerr << time_taken << "ms" << std::string(27, '\t');
} else {
std::cerr << "TLE Warning!\n";
std::cerr << "Time: ";
std::cerr << (time_taken / 1000.0) << "s" << std::string(27, '\n');
}
}
struct chess{
int i1, j1, i2, j2;
};
int v[105][105], m[105][105], xv, yv, xm, ym, res, n;
char a[105][105];
int dxv[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int dyv[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int dxm[] = {-2, -1, 1, 2, 2, 1, -1, -2, 0};
int dym[] = {-1, -2, -2, -1, 1, 2, 2, 1, 0};
bool valid(int i, int j){
return i >= 1 && i <= n && j >= 1 && j <= n && a[i][j] != '#';
}
void bfs(){
v[xv][yv] = 0;
m[xm][ym] = 0;
queue<chess> q;
q.push({xv,yv,xm,ym});
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
int i1 = xv + dxv[i];
int j1 = yv + dyv[i];
int i2 = xm + dxm[j];
int j2 = ym + dym[j];
if (valid(i1,j1) && valid(i2,j2) && v[i1][j1] == 0 && m[i2][j2] == 0){
q.push({i1,j1,i2,j2});
if (i1 != xv && j1 != yv) v[i1][j1] = v[xv][yv] + 1;
if (i2 != xm && j2 != ym) m[i2][j2] = m[xm][ym] + 1;
}
}
}
while(!q.empty()){
auto top = q.front(); q.pop();
int i1 = top.i1, j1 = top.j1, i2 = top.i2, j2 = top.j2;
if (i1 == i2 && j1 == j2) res = min(res, max(v[i1][j1], m[i2][j2]));
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
int ni1 = i1 + dxv[i];
int nj1 = j1 + dyv[i];
int ni2 = i2 + dxm[j];
int nj2 = j2 + dym[j];
if (!valid(ni1,nj1) || !valid(ni2,nj2)) continue;
if (v[ni1][nj1] != 0 && m[ni2][nj2] != 0) continue;
if (v[ni1][nj1] == 0) v[ni1][nj1] = v[i1][j1] + 1;
if (m[ni2][nj2] == 0) m[ni2][nj2] = m[i2][j2] + 1;
q.push({ni1,nj1,ni2,nj2});
if (ni1 == ni2 && nj1 == nj2)
res = min(res, max(v[ni1][nj1], m[ni2][nj2]));
}
}
}
}
rizzler{
gd();
cin >> n;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cin >> a[i][j];
if (a[i][j] == 'T'){
xv = i, yv = j;
} else if(a[i][j] == 'M'){
xm = i, ym = j;
}
}
}
res = 1e9;
bfs();
cout << res << "\n";
tle();
}
I3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk8yIikKI3ByYWdtYSBHQ0MgdGFyZ2V0KCJhdngsYXZ4MixmbWEiKQojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbGwgPSBsb25nIGxvbmc7CmNvbnN0IGludCBtb2QgPSAxZTkgKyA3OwojZGVmaW5lIGVsIGVuZGwKI2RlZmluZSBpbnQgaW50NjRfdAojZGVmaW5lIGZpIGZpcnN0CiNkZWZpbmUgc2Ugc2Vjb25kCiNkZWZpbmUgZmlsZSh4KSBmcmVvcGVuKHgiLmlucCIsICJyIiwgc3RkaW4pOyBmcmVvcGVuKHgiLm91dCIsICJ3Iiwgc3Rkb3V0KQojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGdkKCkgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7IGNpbi50aWUobnVsbHB0cik7IGNvdXQudGllKG51bG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YmIGFbaV1bal0gIT0gJyMnOwp9Cgp2b2lkIGJmcygpewogICAgdlt4dl1beXZdID0gMDsKICAgIG1beG1dW3ltXSA9IDA7CiAgICBxdWV1ZTxjaGVzcz4gcTsKICAgIHEucHVzaCh7eHYseXYseG0seW19KTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgOTsgaSsrKXsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IDk7IGorKyl7CiAgICAgICAgaW50IGkxID0geHYgKyBkeHZbaV07CiAgICAgICAgaW50IGoxID0geXYgKyBkeXZbaV07CiAgICAgICAgaW50IGkyID0geG0gKyBkeG1bal07CiAgICAgICAgaW50IGoyID0geW0gKyBkeW1bal07CiAgICAgICAgaWYgKHZhbGlkKGkxLGoxKSAmJiB2YWxpZChpMixqMikgJiYgdltpMV1bajFdID09IDAgJiYgbVtpMl1bajJdID09IDApewogICAgICAgICAgICBxLnB1c2goe2kxLGoxLGkyLGoyfSk7CiAgICAgICAgICAgIGlmIChpMSAhPSB4diAmJiBqMSAhPSB5dikgdltpMV1bajFdID0gdlt4dl1beXZdICsgMTsKICAgICAgICAgICAgaWYgKGkyICE9IHhtICYmIGoyICE9IHltKSBtW2kyXVtqMl0gPSBtW3htXVt5bV0gKyAxOwogICAgICAgIH0KICAgIH0KfQogICAgd2hpbGUoIXEuZW1wdHkoKSl7CiAgICAgICAgYXV0byB0b3AgPSBxLmZyb250KCk7IHEucG9wKCk7CiAgICAgICAgaW50IGkxID0gdG9wLmkxLCBqMSA9IHRvcC5qMSwgaTIgPSB0b3AuaTIsIGoyID0gdG9wLmoyOwogICAgICAgIGlmIChpMSA9PSBpMiAmJiBqMSA9PSBqMikgcmVzID0gbWluKHJlcywgbWF4KHZbaTFdW2oxXSwgbVtpMl1bajJdKSk7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCA5OyBpKyspeyAgCiAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgOTsgaisrKXsgIAogICAgICAgICAgICAgICAgaW50IG5pMSA9IGkxICsgZHh2W2ldOwogICAgICAgICAgICAgICAgaW50IG5qMSA9IGoxICsgZHl2W2ldOwogICAgICAgICAgICAgICAgaW50IG5pMiA9IGkyICsgZHhtW2pdOwogICAgICAgICAgICAgICAgaW50IG5qMiA9IGoyICsgZHltW2pdOwogICAgICAgICAgICAgICAgaWYgKCF2YWxpZChuaTEsbmoxKSB8fCAhdmFsaWQobmkyLG5qMikpIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgaWYgKHZbbmkxXVtuajFdICE9IDAgJiYgbVtuaTJdW25qMl0gIT0gMCkgY29udGludWU7CiAgICAgICAgICAgICAgICBpZiAodltuaTFdW25qMV0gPT0gMCkgdltuaTFdW25qMV0gPSB2W2kxXVtqMV0gKyAxOwogICAgICAgICAgICAgICAgaWYgKG1bbmkyXVtuajJdID09IDApIG1bbmkyXVtuajJdID0gbVtpMl1bajJdICsgMTsKICAgICAgICAgICAgICAgIHEucHVzaCh7bmkxLG5qMSxuaTIsbmoyfSk7CiAgICAgICAgICAgICAgICBpZiAobmkxID09IG5pMiAmJiBuajEgPT0gbmoyKQogICAgICAgICAgICAgICAgICAgIHJlcyA9IG1pbihyZXMsIG1heCh2W25pMV1bbmoxXSwgbVtuaTJdW25qMl0pKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKcml6emxlcnsKICAgIGdkKCk7CiAgICAgICAgY2luID4+IG47CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKXsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbjsgaisrKXsKICAgICAgICAgICAgICAgIGNpbiA+PiBhW2ldW2pdOwogICAgICAgICAgICAgICAgaWYgKGFbaV1bal0gPT0gJ1QnKXsKICAgICAgICAgICAgICAgICAgICB4diA9IGksIHl2ID0gajsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZihhW2ldW2pdID09ICdNJyl7CiAgICAgICAgICAgICAgICAgICAgeG0gPSBpLCB5bSA9IGo7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmVzID0gMWU5OwogICAgICAgIGJmcygpOwogICAgICAgIGNvdXQgPDwgcmVzIDw8ICJcbiI7CiAgICAKCgogICAgdGxlKCk7CiAgICAKfQ==