#include <bits/stdc++.h>
using namespace std;
int T;
int R, C;
char MAT[100][100];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> T;
  for (long long t = 0; t < T; t++) {
    cin >> R >> C;
    for (long long i = 0; i < R; i++) {
      for (long long j = 0; j < C; j++) {
        cin >> MAT[i][j];
      }
    }
    bool flag = false;
    for (long long i = 0; i < R; i++) {
      if (flag) break;
      for (long long j = 0; j < C; j++) {
        if (MAT[i][j] == 'A') {
          flag = true;
        }
      }
    }
    if (flag == false) {
      cout << "MORTAL\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < R; i++) {
      if (flag) break;
      for (long long j = 0; j < C; j++) {
        if (MAT[i][j] == 'P') {
          flag = true;
        }
      }
    }
    if (flag == false) {
      cout << "0\n";
      continue;
    }
    if (R == 1 || C == 1) {
      if (MAT[0][0] == 'P' && MAT[R - 1][C - 1] == 'P') {
        cout << "2\n";
        continue;
      } else {
        cout << "1\n";
        continue;
      }
    }
    flag = false;
    for (long long i = 0; i < R; i++) {
      if (MAT[i][0] == 'P') {
        flag = true;
        break;
      }
    }
    if (flag == false) {
      cout << "1\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < R; i++) {
      if (MAT[i][C - 1] == 'P') {
        flag = true;
        break;
      }
    }
    if (flag == false) {
      cout << "1\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < C; i++) {
      if (MAT[0][i] == 'P') {
        flag = true;
        break;
      }
    }
    if (flag == false) {
      cout << "1\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < C; i++) {
      if (MAT[R - 1][i] == 'P') {
        flag = true;
        break;
      }
    }
    if (flag == false) {
      cout << "1\n";
      continue;
    }
    if (MAT[0][0] == 'A') {
      cout << "2\n";
      continue;
    }
    if (MAT[R - 1][0] == 'A') {
      cout << "2\n";
      continue;
    }
    if (MAT[0][C - 1] == 'A') {
      cout << "2\n";
      continue;
    }
    if (MAT[R - 1][C - 1] == 'A') {
      cout << "2\n";
      continue;
    }
    bool aux = false;
    for (long long i = 0; i < R; i++) {
      flag = false;
      for (long long j = 0; j < C; j++) {
        if (MAT[i][j] == 'P') {
          flag = true;
          break;
        }
      }
      if (flag == false) {
        aux = true;
        break;
      }
    }
    for (long long j = 0; j < C; j++) {
      flag = false;
      for (long long i = 0; i < R; i++) {
        if (MAT[i][j] == 'P') {
          flag = true;
          break;
        }
      }
      if (flag == false) {
        aux = true;
        break;
      }
    }
    if (aux == true) {
      cout << "2\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < R; i++) {
      if (MAT[i][0] == 'A') {
        flag = true;
        break;
      }
    }
    if (flag == true) {
      cout << "3\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < R; i++) {
      if (MAT[i][C - 1] == 'A') {
        flag = true;
        break;
      }
    }
    if (flag == true) {
      cout << "3\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < C; i++) {
      if (MAT[0][i] == 'A') {
        flag = true;
        break;
      }
    }
    if (flag == true) {
      cout << "3\n";
      continue;
    }
    flag = false;
    for (long long i = 0; i < C; i++) {
      if (MAT[R - 1][i] == 'A') {
        flag = true;
        break;
      }
    }
    if (flag == true) {
      cout << "3\n";
      continue;
    }
    cout << "4\n";
  }
  return 0;
}
