#include <bits/stdc++.h>
using namespace std;
template <class T>
void _Print(T &&x) {
  cerr << x << ")"
       << "\n";
}
template <class T, class... S>
void _Print(T &&x, S &&...y) {
  cerr << x << ", ";
  _Print(y...);
}
template <class T>
class compare {
 public:
  bool operator()(pair<T, T> &x, pair<T, T> &y) {
    if (x.first == y.first) {
      return x.second > y.second;
    }
    return x.first > y.first;
  }
};
template <class T>
ostream &operator<<(ostream &os, const pair<T, T> &a) {
  return os << a.first << " " << a.second;
}
long long power(long long a, int b) {
  long long po = 1;
  while (b) {
    if (b & 1) po *= a;
    a *= a;
    b >>= 1;
  }
  return po;
}
template <class T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first + b.first, a.second + b.second};
}
template <class T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first - b.first, a.second - b.second};
}
template <class T>
pair<T, T> operator*(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first * b.first, a.second * b.second};
}
template <class T>
pair<T, T> operator%(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first % b.first, a.second % b.second};
}
template <class T, class U>
pair<T, T> operator+(const pair<T, T> &a, const U &b) {
  return {a.first + b, a.second + b};
}
template <class T, class U>
pair<T, T> operator*(const pair<T, T> &a, const U &b) {
  return {a.first * b, a.second * b};
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int r, c;
    cin >> r >> c;
    int cntp = 0;
    bool corner = false, line = false, totline = false, midline = false;
    string mat[r];
    for (int i = 0; i < r; i++) {
      cin >> mat[i];
    }
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        if (mat[i][j] == 'P')
          cntp++;
        else {
          if (i == 0 or i == r - 1 or j == 0 or j == c - 1) {
            line = true;
            if (i == 0) {
              if (j == 0 or j == c - 1) corner = true;
            } else if (i == r - 1) {
              if (j == 0 or j == c - 1) corner = true;
            }
          }
        }
      }
    }
    for (int i = 0; i < r; i++) {
      int cnt = 0;
      for (int j = 0; j < c; j++) {
        if (mat[i][j] == 'A') cnt++;
      }
      if (cnt == c) {
        midline = true;
        if (i == 0 or i == r - 1) totline = true;
      }
    }
    for (int j = 0; j < c; j++) {
      int cnt = 0;
      for (int i = 0; i < r; i++) {
        if (mat[i][j] == 'A') cnt++;
      }
      if (cnt == r) {
        if (j == 0 or j == c - 1) totline = true;
        midline = true;
      }
    }
    if (cntp == 0) {
      cout << 0 << "\n";
    } else if (cntp == r * c) {
      cout << "MORTAL"
           << "\n";
    } else if (totline) {
      cout << 1 << "\n";
    } else if (corner or midline) {
      cout << 2 << "\n";
    } else if (line) {
      cout << 3 << "\n";
    } else
      cout << 4 << "\n";
  }
  return 0;
}
