#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, r, c;
  char grid[65][65];
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &r, &c);
    int ans = 4;
    int horizontalA[65] = {0};
    int verticalA[65] = {0};
    int horizontalP[65] = {0};
    int verticalP[65] = {0};
    for (int i = 0; i < r; i++) scanf("%s", &grid[i]);
    bool p = 0, a = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        if (grid[i][j] == 'P') {
          p = 1;
        } else
          a = 1;
      }
    }
    if (!a) {
      printf("MORTAL\n");
      continue;
    }
    for (int i = 0; i < r; i++) {
      bool P_present = 0;
      for (int j = 0; j < c; j++)
        if (grid[i][j] == 'P') P_present = 1;
      if (i > 0) verticalP[i] = verticalP[i - 1];
      verticalP[i] += (int)P_present;
    }
    for (int j = 0; j < c; j++) {
      bool P_present = 0;
      for (int i = 0; i < r; i++)
        if (grid[i][j] == 'P') P_present = 1;
      if (j > 0) horizontalP[j] = horizontalP[j - 1];
      horizontalP[j] += (int)P_present;
    }
    for (int i = 0; i < r; i++) {
      bool a_found = 0, allA = 1;
      for (int j = 0; j < c; j++) {
        if (grid[i][j] == 'A') {
          bool up, right, left, down;
          up = right = left = down = 0;
          if (horizontalP[c - 1] - horizontalP[j] > 0) right = 1;
          if (j > 0 && horizontalP[j - 1] > 0) left = 1;
          if (verticalP[r - 1] - verticalP[i] > 0) down = 1;
          if (i > 0 && verticalP[i - 1] > 0) up = 1;
          int tmp = up + right + left + down;
          ans = min(ans, tmp);
        } else
          allA = 0;
      }
      int tmpans = 0;
      if (allA) {
        if (verticalP[r - 1] - verticalP[i] > 0) tmpans++;
        if (i > 0 && verticalP[i - 1] > 0) tmpans++;
        ans = min(ans, tmpans);
      }
    }
    for (int j = 0; j < c; j++) {
      bool allA = 1;
      for (int i = 0; i < r; i++) {
        if (grid[i][j] != 'A') {
          allA = 0;
          break;
        }
      }
      int tmpans = 0;
      if (allA) {
        if (horizontalP[c - 1] - horizontalP[j] > 0) tmpans++;
        if (j > 0 && horizontalP[j - 1] > 0) tmpans++;
        ans = min(ans, tmpans);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
