#include <bits/stdc++.h>
using namespace std;
struct __timestamper {};
const int INF = 1e9;
int subsolve(vector<string> a) {
  int n = a.size();
  int m = a[0].size();
  bool anyA = 0;
  bool anyP = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      anyA |= a[i][j] == 'A';
      anyP |= a[i][j] == 'P';
    }
  }
  if (!anyA) return INF;
  if (!anyP) return 0;
  if (a[0] == string(m, 'A')) return 1;
  if (a[0][0] == 'A' || a[0][m - 1] == 'A') return 2;
  for (int i = 0; i < n; i++) {
    if (a[i] == string(m, 'A')) return 2;
  }
  if (a[0] != string(m, 'P')) return 3;
  return 4;
}
int solve(vector<string> a) {
  int res = INF;
  for (int it = 0; it < 4; it++) {
    res = min(res, subsolve(a));
    int n = a.size();
    int m = a[0].size();
    vector<string> b(m, string(n, '.'));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        b[m - 1 - j][i] = a[i][j];
      }
    }
    a = b;
  }
  return res;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, m;
    scanf("%d%d", &n, &m);
    vector<string> a(n);
    for (int i = 0; i < n; i++) {
      static char buf[(int)1.01e5];
      scanf("%s", buf);
      a[i] = buf;
    }
    int res = solve(a);
    if (res == INF)
      printf("MORTAL\n");
    else
      printf("%d\n", res);
  }
  return 0;
}
