#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int r, c;
    cin >> r;
    cin >> c;
    vector<string> matrix(r);
    string row;
    for (long long int i = 0; i <= r - 1; i++) {
      cin >> matrix[i];
    }
    bool Apresent = false, Ppresent = false;
    int broken = 0, bb;
    int moves;
    bb = 0;
    for (long long int i = 0; i <= r - 1; i++) {
      broken = 0;
      for (long long int j = 0; j <= c - 1; j++) {
        if (matrix[i][j] == 'A') {
          Apresent = true;
        } else {
          Ppresent = true;
        }
        if (Apresent == true && Ppresent == true) {
          broken = 1;
          break;
        }
      }
      if (broken == 1) {
        bb = 1;
        break;
      }
    }
    if (Ppresent == false) {
      moves = 0;
      cout << moves << "\n";
      continue;
    }
    long long int edgeCompleteA = 0;
    string rowA = "", columnA;
    for (long long int i = 0; i <= c - 1; i++) {
      rowA += "A";
    }
    if (rowA == matrix[0]) {
      edgeCompleteA++;
    } else if (rowA == matrix[r - 1]) {
      edgeCompleteA++;
    } else {
      broken = 0;
      for (long long int i = 0; i <= r - 1; i++) {
        if (matrix[i][0] != 'A') {
          broken = 1;
          break;
        }
      }
      if (broken == 0) {
        edgeCompleteA++;
      }
      broken = 0;
      for (long long int i = 0; i <= r - 1; i++) {
        if (matrix[i][c - 1] != 'A') {
          broken = 1;
          break;
        }
      }
      if (broken == 0) {
        edgeCompleteA++;
      }
    }
    if (edgeCompleteA > 0) {
      moves = 1;
      cout << moves << "\n";
      continue;
    }
    if (matrix[0][0] == 'A' || matrix[0][c - 1] == 'A' ||
        matrix[r - 1][0] == 'A' || matrix[r - 1][c - 1] == 'A') {
      moves = 2;
      cout << moves << "\n";
      continue;
    }
    broken = 0;
    for (long long int i = 1; i <= r - 2; i++) {
      if (rowA == matrix[i]) {
        broken = 1;
        break;
      }
    }
    if (broken == 1) {
      moves = 2;
      cout << moves << "\n";
      continue;
    }
    bb = 0;
    for (long long int j = 1; j <= c - 2; j++) {
      broken = 0;
      for (long long int i = 0; i <= r - 1; i++) {
        if (matrix[i][j] != 'A') {
          broken = 1;
          break;
        }
      }
      if (broken == 0) {
        moves = 2;
        cout << moves << "\n";
        bb = 1;
        break;
      }
    }
    if (bb == 1) {
      continue;
    }
    broken = 0;
    for (long long int j = 1; j <= c - 2; j++) {
      if (matrix[0][j] == 'A') {
        broken = 1;
        break;
      }
    }
    if (broken == 1) {
      moves = 3;
      cout << moves << "\n";
      continue;
    }
    broken = 0;
    for (long long int j = 1; j <= c - 2; j++) {
      if (matrix[r - 1][j] == 'A') {
        broken = 1;
        break;
      }
    }
    if (broken == 1) {
      moves = 3;
      cout << moves << "\n";
      continue;
    }
    broken = 0;
    for (long long int i = 1; i <= r - 2; i++) {
      if (matrix[i][0] == 'A') {
        broken = 1;
        break;
      }
    }
    if (broken == 1) {
      moves = 3;
      cout << moves << "\n";
      continue;
    }
    broken = 0;
    for (long long int i = 1; i <= r - 2; i++) {
      if (matrix[i][c - 1] == 'A') {
        broken = 1;
        break;
      }
    }
    if (broken == 1) {
      moves = 3;
      cout << moves << "\n";
      continue;
    }
    if (Apresent) {
      moves = 4;
      cout << moves << "\n";
    } else {
      cout << "MORTAL"
           << "\n";
    }
  }
}
