#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> S) {
  os << "{ ";
  for (auto s : S) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.F << ":" << m.S << ")";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const int R = 65;
char G[R][R];
int solve() {
  int n, m;
  cin >> n >> m;
  bool all = true, none = true;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> G[i][j];
      if (G[i][j] == 'A')
        none = false;
      else
        all = false;
    }
  }
  if (all) return 0;
  if (none) return -1;
  bool row = true;
  for (int i = 1; i <= m; ++i)
    if (G[1][i] == 'P') row = false;
  if (row) return 1;
  row = true;
  for (int i = 1; i <= m; ++i)
    if (G[n][i] == 'P') row = false;
  if (row) return 1;
  row = true;
  for (int i = 1; i <= n; ++i)
    if (G[i][1] == 'P') row = false;
  if (row) return 1;
  row = true;
  for (int i = 1; i <= n; ++i)
    if (G[i][m] == 'P') row = false;
  if (row) return 1;
  if (G[1][1] == 'A') return 2;
  if (G[1][m] == 'A') return 2;
  if (G[n][1] == 'A') return 2;
  if (G[n][m] == 'A') return 2;
  for (int i = 2; i <= n - 1; ++i) {
    bool eq = true;
    for (int j = 1; j <= m; ++j) {
      if (G[i][j] == 'P') eq = false;
    }
    if (eq) return 2;
  }
  for (int i = 2; i <= m - 1; ++i) {
    bool eq = true;
    for (int j = 1; j <= n; ++j) {
      if (G[j][i] == 'P') eq = false;
    }
    if (eq) return 2;
  }
  bool any = false;
  for (int i = 2; i <= n - 1; ++i) {
    if (G[i][1] == 'A' || G[i][m] == 'A') any = true;
  }
  for (int i = 2; i <= m - 1; ++i) {
    if (G[1][i] == 'A' || G[n][i] == 'A') any = true;
  }
  if (any) return 3;
  return 4;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  int t;
  cin >> t;
  while (t--) {
    int ans = solve();
    if (ans != -1)
      cout << ans << "\n";
    else
      cout << "MORTAL"
           << "\n";
  }
  return 0;
}
