#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const long double pi = acos(-1.0);
const long double eps = 1e-5;
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
int n, m;
char a[100][100];
bool n1s() {
  bool f = true;
  for (int i = (1); i <= (n); i++)
    if (a[i][1] != 'A') f = false;
  if (f) return f;
  f = true;
  for (int i = (1); i <= (n); i++)
    if (a[i][m] != 'A') f = false;
  if (f) return f;
  f = true;
  for (int i = (1); i <= (m); i++)
    if (a[1][i] != 'A') f = false;
  if (f) return f;
  f = true;
  for (int i = (1); i <= (m); i++)
    if (a[n][i] != 'A') f = false;
  return f;
}
bool n2s() {
  for (int j = (2); j <= (m - 1); j++) {
    bool f = true;
    for (int i = (1); i <= (n); i++) {
      if (a[i][j] != 'A') f = false;
    }
    if (f) return f;
  }
  for (int i = (2); i <= (n - 1); i++) {
    bool f = true;
    for (int j = (1); j <= (m); j++) {
      if (a[i][j] != 'A') f = false;
    }
    if (f) return f;
  }
  if (a[1][1] == 'A' || a[1][m] == 'A' || a[n][1] == 'A' || a[n][m] == 'A')
    return true;
  int maxc = 0;
  int cnt = 0;
  for (int i = (1); i <= (n); i++) {
    if (a[i][1] == 'A')
      cnt++;
    else
      break;
  }
  maxc = max(maxc, cnt);
  cnt = 0;
  for (int i = (1); i <= (n); i++) {
    if (a[i][m] == 'A')
      cnt++;
    else
      break;
  }
  maxc = max(maxc, cnt);
  cnt = 0;
  for (int i = (n); i >= (1); i--) {
    if (a[i][1] == 'A')
      cnt++;
    else
      break;
  }
  if (cnt + maxc >= n) return true;
  cnt = 0;
  for (int i = (n); i >= (1); i--) {
    if (a[i][m] == 'A')
      cnt++;
    else
      break;
  }
  if (cnt + maxc >= n) return true;
  maxc = 0;
  cnt = 0;
  for (int j = (1); j <= (m); j++) {
    if (a[1][j] == 'A')
      cnt++;
    else
      break;
  }
  maxc = max(maxc, cnt);
  cnt = 0;
  for (int j = (1); j <= (m); j++) {
    if (a[n][j] == 'A')
      cnt++;
    else
      break;
  }
  maxc = max(maxc, cnt);
  cnt = 0;
  for (int j = (m); j >= (1); j--) {
    if (a[1][j] == 'A')
      cnt++;
    else
      break;
  }
  if (cnt + maxc >= m) return true;
  cnt = 0;
  for (int j = (m); j >= (1); j--) {
    if (a[n][j] == 'A')
      cnt++;
    else
      break;
  }
  if (cnt + maxc >= m) return true;
  for (int i = (2); i <= (n - 1); i++) {
    int cnt = 0;
    for (int j = (1); j <= (m); j++) {
      if (a[i][j] == 'A')
        cnt++;
      else
        break;
    }
    if (cnt >= m) return true;
  }
  for (int j = (2); j <= (m - 1); j++) {
    int cnt = 0;
    for (int i = (1); i <= (n); i++) {
      if (a[i][j] == 'A')
        cnt++;
      else
        break;
    }
    if (cnt >= n) return true;
  }
  return false;
}
bool n3s() {
  bool f = false;
  for (int i = (1); i <= (n); i++)
    if (a[i][1] == 'A' || a[i][m] == 'A') f = true;
  for (int j = (1); j <= (m); j++)
    if (a[1][j] == 'A' || a[n][j] == 'A') f = true;
  if (f) return f;
  int maxc = 0;
  for (int i = (2); i <= (n - 1); i++) {
    int cnt = 0;
    for (int j = (1); j <= (m); j++) {
      if (a[i][j] == 'A')
        cnt++;
      else
        break;
    }
    maxc = max(maxc, cnt);
  }
  int max2c = 0;
  for (int i = (2); i <= (n - 1); i++) {
    int cnt = 0;
    for (int j = (m); j >= (1); j--) {
      if (a[i][j] == 'A')
        cnt++;
      else
        break;
    }
    max2c = max(max2c, cnt);
  }
  if (max2c + maxc >= m) return true;
  maxc = 0;
  max2c = 0;
  for (int j = (2); j <= (m - 1); j++) {
    int cnt = 0;
    for (int i = (1); i <= (n); i++) {
      if (a[i][j] == 'A')
        cnt++;
      else
        break;
    }
    maxc = max(maxc, cnt);
  }
  for (int j = (2); j <= (m - 1); j++) {
    int cnt = 0;
    for (int i = (n); i >= (1); i--) {
      if (a[i][j] == 'A')
        cnt++;
      else
        break;
    }
    max2c = max(max2c, cnt);
  }
  if (max2c + maxc >= m) return true;
  return false;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> m;
    bool mortal = true;
    bool n0s = true;
    for (int i = (1); i <= (n); i++) {
      for (int j = (1); j <= (m); j++) {
        cin >> a[i][j];
        if (a[i][j] == 'A') mortal = false;
        if (a[i][j] == 'P') n0s = false;
      }
    }
    if (mortal) {
      cout << "MORTAL" << '\n';
      continue;
    }
    if (n0s) {
      cout << 0 << '\n';
      continue;
    }
    if (n1s()) {
      cout << 1 << '\n';
      continue;
    }
    if (n2s()) {
      cout << 2 << '\n';
      continue;
    }
    if (n3s()) {
      cout << 3 << '\n';
      continue;
    }
    cout << 4 << '\n';
  }
  return 0;
}
