#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
void amax(long long& a, long long b) { a = max(a, b) % 1000000007; }
void amin(long long& a, long long b) { a = min(a, b) % 1000000007; }
void inc(long long& a, long long b) { a = (a + b) % 1000000007; }
void dec(long long& a, long long b) { a = (a - b + 1000000007) % 1000000007; }
long long solve() {
  long long n, m;
  cin >> n >> m;
  string a[n];
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long ans = -1;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (a[i][j] == 'A') ans = 4;
    }
  }
  for (long long i = 0; i < n; i++)
    if (a[i][0] == 'A') ans = 3;
  for (long long i = 0; i < m; i++)
    if (a[0][i] == 'A') ans = 3;
  for (long long i = 0; i < n; i++)
    if (a[i][m - 1] == 'A') ans = 3;
  for (long long i = 0; i < m; i++)
    if (a[n - 1][i] == 'A') ans = 3;
  for (long long i = 0; i < n; i++) {
    long long count = 0;
    for (long long j = 0; j < m; j++)
      if (a[i][j] == 'A') count++;
    if (count == m) ans = 2;
  }
  for (long long i = 0; i < m; i++) {
    long long count = 0;
    for (long long j = 0; j < n; j++)
      if (a[j][i] == 'A') count++;
    if (count == n) ans = 2;
  }
  long long st = -1;
  for (long long i = 0; i < 1; i++) {
    long long cur = 0;
    for (long long j = 0; j < m; j++) {
      if (a[i][j] == 'A') {
        cur = j;
      } else
        break;
    }
    st = max(cur, st);
  }
  for (long long i = n - 1; i < n; i++) {
    long long cur = 0;
    for (long long j = 0; j < m; j++) {
      if (a[i][j] == 'A') {
        cur = j;
      } else
        break;
    }
    st = max(cur, st);
  }
  long long end = 1e9;
  for (long long i = 0; i < 1; i++) {
    long long cur = 1e9;
    for (long long j = (long long)(m - 1); j >= (long long)(0); --j) {
      if (a[i][j] == 'A') {
        cur = j;
      } else
        break;
    }
    end = min(end, cur);
  }
  for (long long i = n - 1; i < n; i++) {
    long long cur = 1e9;
    for (long long j = (long long)(m - 1); j >= (long long)(0); --j) {
      if (a[i][j] == 'A') {
        cur = j;
      } else
        break;
    }
    end = min(end, cur);
  }
  if (end < st) ans = 2;
  st = -1;
  for (long long i = 0; i < 1; i++) {
    long long cur = 0;
    for (long long j = 0; j < n; j++) {
      if (a[j][i] == 'A') {
        cur = j;
      } else
        break;
    }
    st = max(cur, st);
  }
  for (long long i = m - 1; i < m; i++) {
    long long cur = 0;
    for (long long j = 0; j < n; j++) {
      if (a[j][i] == 'A') {
        cur = j;
      } else
        break;
    }
    st = max(cur, st);
  }
  end = 1e9;
  for (long long i = 0; i < 1; i++) {
    long long cur = 1e9;
    for (long long j = (long long)(n - 1); j >= (long long)(0); --j) {
      if (a[j][i] == 'A') {
        cur = j;
      } else
        break;
    }
    end = min(end, cur);
  }
  for (long long i = m - 1; i < m; i++) {
    long long cur = 1e9;
    for (long long j = (long long)(n - 1); j >= (long long)(0); --j) {
      if (a[j][i] == 'A') {
        cur = j;
      } else
        break;
    }
    end = min(end, cur);
  }
  if (end < st) ans = 2;
  if (a[0][0] == 'A' || a[n - 1][0] == 'A' || a[0][m - 1] == 'A' ||
      a[n - 1][m - 1] == 'A')
    ans = 2;
  long long count = 0;
  for (long long i = 0; i < n; i++)
    if (a[i][0] == 'A') count++;
  if (count == n) ans = 1;
  count = 0;
  for (long long i = 0; i < n; i++)
    if (a[i][m - 1] == 'A') count++;
  if (count == n) ans = 1;
  count = 0;
  for (long long i = 0; i < m; i++) {
    if (a[0][i] == 'A') count++;
  }
  if (count == m) ans = 1;
  count = 0;
  for (long long i = 0; i < m; i++) {
    if (a[n - 1][i] == 'A') count++;
  }
  if (count == m) ans = 1;
  count = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++)
      if (a[i][j] == 'A') count++;
  }
  if (count == n * m) ans = 0;
  if (ans == -1)
    cout << "MORTAL\n";
  else
    cout << ans << '\n';
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
