#include <bits/stdc++.h>
using namespace std;
vector<string> split(const string& s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\n';
  err(++it, args...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int Test;
  cin >> Test;
  while (Test--) {
    long long int n, m;
    cin >> n >> m;
    vector<string> b(n);
    for (int i = 0; i < n; i++) cin >> b[i];
    int c = 0, l = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) c += (b[i][j] == 'A');
    }
    if (c == 0) {
      cout << "MORTAL" << endl;
      continue;
    } else if (c == (n * m)) {
      cout << 0 << endl;
      continue;
    }
    int t = 0;
    long long int complete = 0;
    for (int i = 0; i < m; i++) {
      if (b[0][i] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == m);
    t = 0;
    for (int i = 0; i < n; i++) {
      if (b[i][0] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == n);
    t = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (b[i][0] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == n);
    t = 0;
    for (int i = 0; i < m; i++) {
      if (b[n - 1][i] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == m);
    t = 0;
    for (int i = m - 1; i >= 0; i--) {
      if (b[0][i] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == m);
    t = 0;
    for (int i = 0; i < n; i++) {
      if (b[i][m - 1] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == n);
    t = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (b[i][m - 1] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == n);
    t = 0;
    for (int i = m - 1; i >= 0; i--) {
      if (b[n - 1][i] == 'P') break;
      t++;
    }
    l = max(l, t);
    complete |= (t == m);
    if (l > 0) {
      if (complete)
        cout << 1 << endl;
      else
        cout << 2 << endl;
      continue;
    }
    long long int f = 0, com = 0;
    for (int i = 1; i < n - 1; i++) {
      long long int len = 0;
      for (int j = 0; j < m; j++) {
        if (b[i][j] == 'P') break;
        len++;
      }
      f = max(len, f);
      com |= (len == m);
    }
    for (int i = 1; i < n - 1; i++) {
      long long int len = 0;
      for (int j = m - 1; j >= 0; j--) {
        if (b[i][j] == 'P') break;
        len++;
      }
      f = max(len, f);
      com |= (len == m);
    }
    for (int i = 1; i < m - 1; i++) {
      long long int len = 0;
      for (int j = n - 1; j >= 0; j--) {
        if (b[j][i] == 'P') break;
        len++;
      }
      f = max(len, f);
      com |= (len == n);
    }
    for (int i = 1; i < m - 1; i++) {
      long long int len = 0;
      for (int j = 0; j < n; j++) {
        if (b[j][i] == 'P') break;
        len++;
      }
      f = max(len, f);
      com |= (len == n);
    }
    if (com)
      cout << 2 << endl;
    else if (f)
      cout << 3 << endl;
    else
      cout << 4 << endl;
  }
  return 0;
}
