#include <bits/stdc++.h>
using namespace std;
char gr[65][65];
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long r, c;
    cin >> r >> c;
    bool yes = true, existeA = false;
    long long ans = LLONG_MAX;
    for (long long i = 0; i < r; i++) {
      for (long long j = 0; j < c; j++) {
        cin >> gr[i][j];
        if (gr[i][j] == 'P')
          yes = false;
        else
          existeA = true;
      }
    }
    if (yes) ans = 0;
    if (existeA) ans = min(ans, 4ll);
    yes = true;
    for (int i = 0; i < r; i++) {
      if (gr[i][0] == 'P') yes = false;
    }
    if (yes) ans = min(ans, 1ll);
    yes = true;
    for (int i = 0; i < r; i++) {
      if (gr[i][c - 1] == 'P') yes = false;
    }
    if (yes) ans = min(ans, 1ll);
    yes = true;
    for (int i = 0; i < c; i++) {
      if (gr[0][i] == 'P') yes = false;
    }
    if (yes) ans = min(ans, 1ll);
    yes = true;
    for (int i = 0; i < c; i++) {
      if (gr[r - 1][i] == 'P') yes = false;
    }
    if (yes) ans = min(ans, 1ll);
    if (gr[0][0] == 'A' || gr[0][c - 1] == 'A' || gr[r - 1][0] == 'A' ||
        gr[r - 1][c - 1] == 'A')
      ans = min(ans, 2ll);
    for (int i = 0; i < r; i++) {
      yes = true;
      for (int j = 0; j < c; j++) {
        if (gr[i][j] == 'P') yes = false;
      }
      if (yes) ans = min(ans, 2ll);
    }
    for (int i = 0; i < c; i++) {
      yes = true;
      for (int j = 0; j < r; j++) {
        if (gr[j][i] == 'P') yes = false;
      }
      if (yes) ans = min(ans, 2ll);
    }
    for (int i = 0; i < r; i++) {
      if (gr[i][0] == 'A' || gr[i][c - 1] == 'A') ans = min(ans, 3ll);
    }
    for (int i = 0; i < c; i++) {
      if (gr[0][i] == 'A' || gr[r - 1][i] == 'A') ans = min(ans, 3ll);
    }
    if (ans == LLONG_MAX)
      cout << "MORTAL\n";
    else
      cout << ans << "\n";
  }
  return 0;
}
