#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
int mod = 1000000007;
int INF = 1e9;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int r, c;
    cin >> r >> c;
    string a[r];
    int i;
    for (i = 0; i < r; i++) {
      cin >> a[i];
    }
    int j;
    bool flag2 = false;
    for (i = 0; i < r; i++) {
      for (j = 0; j < c; j++) {
        if (a[i][j] == 'A') {
          flag2 = true;
          break;
        }
      }
    }
    if (!flag2) {
      cout << "MORTAL"
           << "\n";
      continue;
    }
    int left[r][c], right[r][c], up[r][c], down[r][c];
    bool rowup[r];
    bool rowdown[r];
    bool colleft[c], colright[c];
    for (i = 0; i < r; i++) {
      bool flag = true;
      for (j = 0; j < c; j++) {
        if (a[i][j] != 'A') {
          flag = false;
          break;
        }
      }
      rowup[i] = (i == 0) ? flag : rowup[i - 1] && flag;
    }
    for (i = r - 1; i >= 0; i--) {
      bool flag = true;
      for (j = c - 1; j >= 0; j--) {
        if (a[i][j] != 'A') {
          flag = false;
          break;
        }
      }
      rowdown[i] = (i == r - 1) ? flag : rowdown[i + 1] && flag;
    }
    for (i = 0; i < c; i++) {
      bool flag = true;
      for (j = 0; j < r; j++) {
        if (a[j][i] != 'A') {
          flag = false;
          break;
        }
      }
      colleft[i] = (i == 0) ? flag : colleft[i - 1] && flag;
    }
    for (i = c - 1; i >= 0; i--) {
      bool flag = true;
      for (j = r - 1; j >= 0; j--) {
        if (a[j][i] != 'A') {
          flag = false;
          break;
        }
      }
      colright[i] = (i == c - 1) ? flag : colright[i + 1] && flag;
    }
    for (i = 0; i < r; i++) {
      for (j = 0; j < c; j++) {
        if (j == 0) {
          left[i][j] = (a[i][j] == 'A');
        } else {
          left[i][j] = left[i][j - 1] + (a[i][j] == 'A');
        }
      }
    }
    for (i = 0; i < r; i++) {
      for (j = c - 1; j >= 0; j--) {
        if (j == c - 1) {
          right[i][j] = (a[i][j] == 'A');
        } else {
          right[i][j] = right[i][j + 1] + (a[i][j] == 'A');
        }
      }
    }
    for (i = 0; i < c; i++) {
      for (j = 0; j < r; j++) {
        if (j == 0) {
          up[j][i] = (a[j][i] == 'A');
        } else {
          up[j][i] = up[j - 1][i] + (a[j][i] == 'A');
        }
      }
    }
    for (i = 0; i < c; i++) {
      for (j = r - 1; j >= 0; j--) {
        if (j == r - 1) {
          down[j][i] = (a[j][i] == 'A');
        } else {
          down[j][i] = down[j + 1][i] + (a[j][i] == 'A');
        }
      }
    }
    int mn = INT_MAX;
    for (i = 0; i < r; i++) {
      for (j = 0; j < c; j++) {
        if (a[i][j] == 'A') {
          int cnt1 = 0;
          int cnt2 = 0;
          if (j - 1 >= 0 && left[i][j - 1] != j) cnt1++;
          if (j + 1 < c && right[i][j + 1] != c - 1 - (j + 1) + 1) cnt1++;
          if (i - 1 >= 0 && !rowup[i - 1]) cnt1++;
          if (i + 1 < r && !rowdown[i + 1]) cnt1++;
          if (i - 1 >= 0 && up[i - 1][j] != i) cnt2++;
          if (i + 1 < r && down[i + 1][j] != r - 1 - (i + 1) + 1) cnt2++;
          if (j - 1 >= 0 && !colleft[j - 1]) cnt2++;
          if (j + 1 < c && !colright[j + 1]) cnt2++;
          mn = min(mn, min(cnt1, cnt2));
        }
      }
    }
    if (mn == INT_MAX)
      cout << "MORTAL"
           << "\n";
    else
      cout << mn << "\n";
  }
}
