#include <bits/stdc++.h>
using namespace std;
inline long long Getint() {
  char ch = getchar();
  long long x = 0, fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    (x *= 10) += ch ^ 48;
    ch = getchar();
  }
  return x * fh;
}
const int inf = 1 << 20;
char s[105][105];
int r, c;
inline int Solver(int id) {
  bool isp = 1, isa = 1;
  for (int i = 1; i <= c; i++)
    if (s[id][i] == 'A') isp = 0;
  for (int i = 1; i <= c; i++)
    if (s[id][i] == 'P') isa = 0;
  if (isp) return inf;
  if (isa) {
    if (id == 1 || id == r) return 1;
    return 2;
  }
  if (s[id][1] == 'A' || s[id][c] == 'A') return 2 + (1 < id && id < r);
  return 3 + (1 < id && id < r);
}
inline int Solvel(int id) {
  bool isp = 1, isa = 1;
  for (int i = 1; i <= r; i++)
    if (s[i][id] == 'A') isp = 0;
  for (int i = 1; i <= r; i++)
    if (s[i][id] == 'P') isa = 0;
  if (isp) return inf;
  if (isa) {
    if (id == 1 || id == c) return 1;
    return 2;
  }
  if (s[1][id] == 'A' || s[r][id] == 'A') return 2 + (1 < id && id < c);
  return 3 + (1 < id && id < c);
}
int main() {
  int T = Getint();
  while (T--) {
    r = Getint();
    c = Getint();
    for (int i = 1; i <= r; i++) scanf("%s", s[i] + 1);
    bool isa = 1;
    for (int i = 1; i <= r; i++)
      for (int j = 1; j <= c; j++)
        if (s[i][j] == 'P') isa = 0;
    if (isa) {
      puts("0");
      continue;
    }
    int Ans = inf;
    for (int i = 1; i <= r; i++) Ans = min(Ans, Solver(i));
    for (int i = 1; i <= c; i++) Ans = min(Ans, Solvel(i));
    if (Ans == inf)
      puts("MORTAL");
    else
      cout << Ans << '\n';
  }
  return 0;
}
