#include <bits/stdc++.h>
using namespace std;
string mat[61];
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int r, c;
    int caz = 0;
    cin >> r >> c;
    bool semaforAllA = true;
    for (int j = 0; j < r; j++) cin >> mat[j];
    for (int j = 0; j < r && semaforAllA; j++)
      for (int k = 0; k < c && semaforAllA; k++)
        if (mat[j][k] == 'P') semaforAllA = false;
    if (semaforAllA)
      cout << 0 << '\n';
    else {
      bool semaforLin = true;
      for (int j = 0; j < c && semaforLin; j++)
        if (mat[0][j] == 'P') semaforLin = false;
      if (semaforLin)
        cout << 1 << '\n';
      else {
        semaforLin = true;
        for (int j = 0; j < c && semaforLin; j++)
          if (mat[r - 1][j] == 'P') semaforLin = false;
        if (semaforLin)
          cout << 1 << '\n';
        else {
          semaforLin = true;
          for (int j = 0; j < r && semaforLin; j++)
            if (mat[j][0] == 'P') semaforLin = false;
          if (semaforLin == 1)
            cout << 1 << '\n';
          else {
            semaforLin = true;
            for (int j = 0; j < r && semaforLin; j++)
              if (mat[j][c - 1] == 'P') semaforLin = false;
            if (semaforLin) cout << 1 << '\n';
          }
        }
      }
      if (!semaforLin) {
        bool semafor = false;
        if (mat[0][0] == 'A' || mat[0][c - 1] == 'A' || mat[r - 1][0] == 'A' ||
            mat[r - 1][c - 1] == 'A')
          semafor = true;
        if (!semafor) {
          for (int j = 1; j < r - 1 && !semafor; j++) {
            bool semaforLin = 1;
            for (int k = 0; k < c && semaforLin; k++)
              if (mat[j][k] == 'P') semaforLin = false;
            if (semaforLin) {
              cout << 2 << '\n';
              semafor = true;
            }
          }
          for (int j = 1; j < c - 1 && !semafor; j++) {
            bool semaforLin = 1;
            for (int k = 0; k < r && semaforLin; k++)
              if (mat[k][j] == 'P') semaforLin = false;
            if (semaforLin) {
              cout << 2 << '\n';
              semafor = true;
            }
          }
          if (!semafor) {
            for (int j = 1; j < c - 1 && !semafor; j++)
              if (mat[0][j] == 'A' || mat[r - 1][j] == 'A') {
                cout << 3 << '\n';
                semafor = true;
              }
            for (int j = 1; j < r - 1 && !semafor; j++)
              if (mat[j][0] == 'A' || mat[j][c - 1] == 'A') {
                cout << 3 << '\n';
                semafor = true;
              }
            if (!semafor) {
              for (int j = 1; j < r - 1 && !semafor; j++)
                for (int k = 0; k < c && !semafor; k++)
                  if (mat[j][k] == 'A') {
                    cout << 4 << '\n';
                    semafor = true;
                  }
              if (!semafor) cout << "MORTAL\n";
            }
          }
        } else
          cout << 2 << '\n';
      }
    }
  }
  return 0;
}
