#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int bin(long int a[], long int low, long int high, long d, long day[]) {
  if (low > high) return -1;
  long mid = (low + high) / 2;
  if (a[mid] > (a[low] + d) && (a[mid - 1] <= (a[low] + d) || day[mid - 1]) &&
      day[mid] == 0)
    return mid;
  else if (a[mid] > (a[low] + d) && day[mid] == 0)
    return bin(a, low, mid - 1, d, day);
  else
    return bin(a, mid + 1, high, d - a[mid + 1] + a[low], day);
}
long long power(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2 == 0) return (power((a * a), b / 2));
  return ((a) * (power((a * a), b / 2)));
}
bool cmp(int a, int b) { return a > b; }
int main() {
  int t = 1;
  cin >> t;
  for (int X = 1; X <= t; X++) {
    int r, c, check = 0, i, j, p = 0, ans = 4, k;
    cin >> r >> c;
    int a[r][c];
    string s;
    for (long long int i = 0; i < (r); i++) {
      cin >> s;
      for (j = 0; j < c; j++) {
        if (s[j] == 'A') {
          a[i][j] = 1;
          check = 1;
        } else {
          a[i][j] = 0;
          p = 1;
        }
      }
    }
    if (!check) {
      cout << "MORTAL\n";
      continue;
    }
    if (!p) {
      cout << "0\n";
      continue;
    }
    i = 0;
    j = 0;
    if (a[i][j]) {
      check = 1;
      for (k = 0; k < c; k++) {
        if (!a[i][k]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      check = 1;
      for (k = 0; k < r; k++) {
        if (!a[k][j]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      ans = 2;
    }
    i = 0;
    j = c - 1;
    if (a[i][j] && i == 0 && j == c - 1) {
      check = 1;
      for (k = 0; k < c; k++) {
        if (!a[i][k]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      check = 1;
      for (k = 0; k < r; k++) {
        if (!a[k][j]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      ans = 2;
    }
    i = r - 1;
    j = 0;
    if (a[i][j] && i == r - 1 && j == 0) {
      check = 1;
      for (k = 0; k < c; k++) {
        if (!a[i][k]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      check = 1;
      for (k = 0; k < r; k++) {
        if (!a[k][j]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      ans = 2;
    }
    i = r - 1;
    j = c - 1;
    if (a[i][j] && i == r - 1 && j == c - 1) {
      check = 1;
      for (k = 0; k < c; k++) {
        if (!a[i][k]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      check = 1;
      for (k = 0; k < r; k++) {
        if (!a[k][j]) {
          check = 0;
          break;
        }
      }
      if (check) {
        cout << "1\n";
        continue;
      }
      ans = 2;
    }
    if (ans == 2) {
      cout << "2\n";
      continue;
    }
    p = 1;
    for (i = 1; i < r - 1; i++) {
      p = 1;
      for (j = 0; j < c; j++) {
        p = p & a[i][j];
      }
      if (p) ans = 2;
    }
    for (j = 1; j < c - 1; j++) {
      p = 1;
      for (i = 0; i < r; i++) {
        p = p & a[i][j];
      }
      if (p) ans = 2;
    }
    if (ans == 2) {
      cout << ans << "\n";
      continue;
    }
    p = 0;
    for (i = 1; i < r - 1; i++) {
      p = p | a[i][0];
      p = p | a[i][c - 1];
    }
    for (j = 1; j < c - 1; j++) {
      p = p | a[0][j];
      p = p | a[r - 1][j];
    }
    if (p)
      cout << "3\n";
    else
      cout << "4\n";
  }
  return 0;
}
