#include <bits/stdc++.h>
using namespace std;
int solve(vector<string>& in) {
  int n = in.size();
  int m = in[0].size();
  vector<pair<int, int> > flast(m, {-1, -1});
  vector<int> cnt(m);
  vector<int> col_res(m, -1);
  int undone = 0;
  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n; i++) {
      if (in[i][j] == 'A') {
        cnt[j]++;
        flast[j].second = i;
        if (flast[j].first == -1) flast[j].first = i;
      }
    }
    if (flast[j].first == 0 || flast[j].second == n - 1) {
      if (cnt[j] == n)
        col_res[j] = 0;
      else
        col_res[j] = 1;
    } else {
      if (flast[j].first != -1) col_res[j] = 2;
    }
    if (cnt[j] < n) undone++;
  }
  int res = -1;
  int left_cnt = 0;
  for (int j = 0; j < m; j++) {
    if (col_res[j] != -1) {
      int tem = col_res[j];
      int processed = 0;
      if (cnt[j] != n) {
        processed++;
      }
      if (left_cnt > 0) {
        tem++;
      }
      if (processed + left_cnt < undone) tem++;
      if (res == -1) res = tem;
      res = min(res, tem);
    }
    if (cnt[j] != n) {
      left_cnt++;
      ;
    }
  }
  return res;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<string> one(n);
  for (auto& it : one) {
    cin >> it;
  }
  vector<string> two(m);
  for (int j = 0; j < m; j++) {
    string cur;
    for (int i = 0; i < n; i++) {
      cur += one[i][j];
    }
    two[j] = cur;
  }
  int o = solve(one);
  int t = solve(two);
  vector<int> res = {o, t};
  sort(res.begin(), res.end());
  for (int i : res) {
    if (i != -1) {
      cout << i << endl;
      return;
    }
  }
  cout << "MORTAL" << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
