#include <bits/stdc++.h>
using namespace std;
const int MAXN = 65;
char maze[MAXN][MAXN];
int r, c;
int solve_NOTLE() {
  int sum = 0;
  vector<int> col_sum(c);
  vector<int> row_sum(r);
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      sum += maze[i][j];
      col_sum[j - 1] += maze[i][j];
      row_sum[i - 1] += maze[i][j];
    }
  }
  if (sum == r * c)
    return 0;
  else if (sum == 0)
    return -1;
  else if (col_sum[0] == r || row_sum[0] == c || col_sum.back() == r ||
           row_sum.back() == c)
    return 1;
  else if (maze[1][1] || maze[1][c] || maze[r][1] || maze[r][c])
    return 2;
  else if (*(max_element(col_sum.begin(), col_sum.end())) == r ||
           *(max_element(row_sum.begin(), row_sum.end())) == c)
    return 2;
  else if (col_sum[0] || col_sum.back() || row_sum[0] || row_sum.back())
    return 3;
  else
    return 4;
}
int solve(int step, int begin) {
  int ans = 0x3f3f3f3f;
  for (int i = begin; i <= r; i += step) {
    int times = !maze[i][1] + !maze[i][c];
    if (times == 2) {
      for (int j = 1; j <= c; j++) {
        if (maze[i][j]) {
          times++;
          break;
        }
      }
      if (times == 2) times = 0x3f3f3f3f;
    } else {
      int sum = 0;
      for (int j = 1; j <= c; j++) sum += maze[i][j];
      if (sum == c)
        times = 1;
      else
        times = 2;
    }
    ans = min(ans, times);
  }
  return ans;
}
int solve1(int step, int begin) {
  int ans = 0x3f3f3f3f;
  for (int i = begin; i <= c; i += step) {
    int times = !maze[1][i] + !maze[r][i];
    if (times == 2) {
      for (int j = 1; j <= r; j++) {
        if (maze[j][i]) {
          times++;
          break;
        }
      }
      if (times == 2) times = 0x3f3f3f3f;
    } else {
      int sum = 0;
      for (int j = 1; j <= r; j++) sum += maze[j][i];
      if (sum == r)
        times = 1;
      else
        times = 2;
    }
    ans = min(ans, times);
  }
  return ans;
}
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    cin >> r >> c;
    for (int i = 1; i <= r; i++) {
      for (int j = 1; j <= c; j++) {
        cin >> maze[i][j];
        maze[i][j] = maze[i][j] == 'A';
      }
    }
    int ans = solve_NOTLE();
    if (~ans) {
      cout << ans << endl;
    } else {
      cout << "MORTAL" << endl;
    }
  }
  return 0;
}
