#include <bits/stdc++.h>
using namespace std;
const int N = 65;
int rt[N], ct[N], r, c;
char g[N][N];
int solve() {
  memset(rt, 0, sizeof(rt));
  memset(ct, 0, sizeof(ct));
  int sum = 0;
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      if (g[i][j] == 'A') sum++, ct[j]++, rt[i]++;
    }
  }
  if (sum == 0) return -1;
  if (sum == r * c) return 0;
  if (rt[1] == c || rt[r] == c || ct[1] == r || ct[c] == r) return 1;
  if (g[1][1] == 'A' || g[1][c] == 'A' || g[r][1] == 'A' || g[r][c] == 'A')
    return 2;
  for (int i = 2; i <= r; i++)
    if (rt[i] == c) return 2;
  for (int i = 2; i <= c; i++)
    if (ct[i] == r) return 2;
  if (rt[1] || rt[r] || ct[1] || ct[c]) return 3;
  return 4;
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    cin >> r >> c;
    for (int i = 1; i <= r; i++) cin >> g[i] + 1;
    int ans = solve();
    if (ans == -1)
      cout << "MORTAL\n";
    else
      cout << ans << endl;
  }
  return 0;
}
