#include <bits/stdc++.h>
const int MX = 10010896;
const int lmt = 3164;
const int N = 10000001;
int flag[MX >> 6];
long long int seg_tree[2 * N];
int n = 5;
using namespace std;
using namespace std::chrono;
long long int extgcd(long long int a, long long int b, long long int& x,
                     long long int& y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    int g = extgcd(b, a % b, y, x);
    y -= a / b * x;
    return g;
  }
}
void sieve() {
  int i, j, k;
  for (i = 3; i < lmt; i += 2)
    if (!(flag[i >> 6] & (1 << ((i >> 1) & 31))))
      for (j = i * i, k = i << 1; j < MX; j += k)
        (flag[j >> 6] |= (1 << ((j >> 1) & 31)));
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, m;
    cin >> n >> m;
    char a[n][m];
    int b1 = 6;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) cin >> a[i][j];
    }
    bool x = 0;
    for (int i = 0; i < n; i++) {
      bool s = 0;
      for (int j = 0; j < m; j++) {
        if (a[i][j] == 'A') {
          if (i == 0 && j == 0 || (i == n - 1 && j == m - 1) ||
              (i == n - 1 && j == 0) || (i == 0 && j == m - 1)) {
            b1 = (2 < b1) ? 2 : b1;
          } else if (i == 0 || i == n - 1)
            b1 = (3 < b1) ? 3 : b1;
          else
            b1 = (4 < b1) ? 4 : b1;
        } else
          s = 1;
      }
      if (!s) {
        if (i == 0 || i == n - 1)
          b1 = (1 < b1) ? 1 : b1;
        else
          b1 = (2 < b1) ? 2 : b1;
      } else
        x = 1;
    }
    if (!x) b1 = 0;
    for (int i = 0; i < m; i++) {
      bool s = 0;
      for (int j = 0; j < n; j++) {
        if (a[j][i] == 'A') {
          if (i == 0 && j == 0 || (i == m - 1 && j == n - 1) ||
              (i == m - 1 && j == 0) || (i == 0 && j == n - 1)) {
            b1 = (2 < b1) ? 2 : b1;
          } else if (i == 0 || i == m - 1)
            b1 = (3 < b1) ? 3 : b1;
          else
            b1 = (4 < b1) ? 4 : b1;
        } else
          s = 1;
      }
      if (!s) {
        if (i == 0 || i == m - 1)
          b1 = (1 < b1) ? 1 : b1;
        else
          b1 = (2 < b1) ? 2 : b1;
      }
    }
    if (b1 == 6)
      cout << "MORTAL\n";
    else
      cout << b1 << "\n";
  }
}
