#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const int maxn = 100;
const long long linf = 1e18;
const int inf = 0x3f3f3f3f;
long long qpow(long long a, long long b) {
  return b ? ((b & 1) ? a * qpow(a * a % MOD, b >> 1) % MOD
                      : qpow(a * a % MOD, b >> 1)) %
                 MOD
           : 1;
}
long long qpow(long long a, long long b, long long c) {
  return b ? ((b & 1) ? a * qpow(a * a % MOD, b >> 1) % c
                      : qpow(a * a % MOD, b >> 1)) %
                 c
           : 1;
}
inline int ird() {
  register int x = 0, f = 1;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * f;
}
inline long long lrd() {
  register long long x = 0, f = 1;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * f;
}
void puts1() { cout << "-1" << endl; }
void puts2() { cout << "YES" << endl; }
void puts3() { cout << "NO" << endl; }
int gcd(int a, int b) { return (b > 0) ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
long long lgcd(long long a, long long b) { return (b > 0) ? gcd(b, a % b) : a; }
long long llcm(long long a, long long b) { return a * b / lgcd(a, b); }
char s[maxn][maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n = ird(), m = ird();
    int ans = 8, sum = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> s[i][j];
        if (s[i][j] == 'A') sum++;
      }
    }
    int f = 0, co = 0, t;
    for (int i = 1; i <= n; i++) {
      f = 0;
      co = 0;
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == 'A') {
          co++;
          if (j == 1 || j == m) f = 1;
        }
      }
      if (co == 0) continue;
      if (co == m)
        t = 0;
      else {
        if (f == 1)
          t = 1;
        else
          t = 2;
      }
      if (n * m - sum == m - co) {
        ans = min(ans, t);
        continue;
      }
      if (i == 1 || i == n)
        t += 1;
      else
        t += 2;
      ans = min(ans, t);
    }
    for (int j = 1; j <= m; j++) {
      f = 0;
      co = 0;
      for (int i = 1; i <= n; i++) {
        if (s[i][j] == 'A') {
          co++;
          if (i == 1 || i == n) f = 1;
        }
      }
      if (co == 0) continue;
      if (co == n)
        t = 0;
      else {
        if (f == 1)
          t = 1;
        else
          t = 2;
      }
      if (n * m - sum == n - co) {
        ans = min(ans, t);
        continue;
      }
      if (j == 1 || j == m)
        t += 1;
      else
        t += 2;
      ans = min(ans, t);
    }
    if (ans != 8)
      cout << ans << endl;
    else
      cout << "MORTAL" << endl;
  }
  return 0;
}
