#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
using namespace std;
template <class T, class U>
inline void checkmin(T &x, U y) {
  if (y < x) x = y;
}
template <class T, class U>
inline void checkmax(T &x, U y) {
  if (y > x) x = y;
}
template <class T, class U>
inline bool ifmax(T &x, U y) {
  if (y > x) return x = y, true;
  return false;
}
template <class T, class U>
inline bool ifmin(T &x, U y) {
  if (y < x) return x = y, true;
  return false;
}
template <class T>
inline void sort(T &a) {
  sort(a.begin(), a.end());
}
template <class T>
inline void rsort(T &a) {
  sort(a.rbegin(), a.rend());
}
template <class T>
inline void reverse(T &a) {
  reverse(a.begin(), a.end());
}
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
  return str >> p.first >> p.second;
}
template <class T>
inline istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) str >> i;
  return str;
}
template <class T>
inline T sorted(T a) {
  sort(a);
  return a;
}
struct zet {
  int val;
  zet(long long x = 0) {
    val = x % 1000000007;
    if (val < 0) val += 1000000007;
  }
  zet(long long a, long long b) {
    *this += a;
    *this /= b;
  }
  zet &operator+=(zet const &b) {
    val += b.val;
    if (val >= 1000000007) val -= 1000000007;
    return *this;
  }
  zet &operator-=(zet const &b) {
    val -= b.val;
    if (val < 0) val += 1000000007;
    return *this;
  }
  zet &operator*=(zet const &b) {
    val = (val * (long long)b.val) % 1000000007;
    return *this;
  }
  friend zet mypow(zet a, long long n) {
    zet res = 1;
    while (n) {
      if (n & 1) res *= a;
      a *= a;
      n >>= 1;
    }
    return res;
  }
  friend zet inv(zet a) { return mypow(a, 1000000007 - 2); }
  zet &operator/=(zet const &b) { return *this *= inv(b); }
  friend zet operator+(zet a, const zet &b) { return a += b; }
  friend zet operator-(zet a, const zet &b) { return a -= b; }
  friend zet operator-(zet a) { return 0 - a; }
  friend zet operator*(zet a, const zet &b) { return a *= b; }
  friend zet operator/(zet a, const zet &b) { return a /= b; }
  friend istream &operator>>(istream &stream, zet &a) {
    return stream >> a.val;
  }
  friend ostream &operator<<(ostream &stream, const zet &a) {
    return stream << a.val;
  }
  friend bool operator==(zet const &a, zet const &b) { return a.val == b.val; }
  friend bool operator!=(zet const &a, zet const &b) { return a.val != b.val; }
  friend bool operator<(zet const &a, zet const &b) { return a.val < b.val; }
};
struct zethash {
  size_t operator()(const zet &z) const { return z.val; }
};
void rotate(vector<vector<char>> &a) {
  vector<vector<char>> ans(signed(a[0].size()), vector<char>(signed(a.size())));
  for (int i = 0; i < signed(a.size()); ++i)
    for (int j = 0; j < signed(a[i].size()); ++j)
      ans[j][i] = a[signed(a.size()) - i - 1][j];
  a = ans;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<char>> a(n, vector<char>(m));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) cin >> a[i][j];
  bool flag = false;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (a[i][j] == 'A') flag = true;
  if (!flag) {
    cout << "MORTAL\n";
    return;
  }
  flag = false;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (a[i][j] == 'P') flag = true;
  if (!flag) {
    cout << 0 << '\n';
    return;
  }
  for (int t = 0; t < 4; ++t) {
    bool flag = true;
    for (int i = 0; i < signed(a.size()); ++i)
      if (a[i][0] == 'P') flag = false;
    if (flag) {
      cout << 1 << '\n';
      return;
    }
    rotate(a);
  }
  for (int t = 0; t < 4; ++t) {
    for (int i = 0; i < signed(a.size()); ++i) {
      bool flag = true;
      for (int j = 0; j < signed(a[i].size()); ++j)
        if (a[i][j] != 'A') flag = false;
      if (flag) {
        cout << 2 << '\n';
        return;
      }
    }
    if (a[0][0] == 'A') {
      cout << 2 << '\n';
      return;
    }
    rotate(a);
  }
  for (int t = 0; t < 4; ++t) {
    bool flag = false;
    for (int i = 0; i < signed(a.size()); ++i)
      if (a[i][0] == 'A') flag = true;
    if (flag) {
      cout << 3 << '\n';
      return;
    }
    rotate(a);
  }
  cout << 4 << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  srand(94385);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
