#include <bits/stdc++.h>   using namespace std;   const int INF = 1000000000;   const int MOD = 1000000007;   const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};   const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};   int n, m;   char grid[65][65];   bool row(int row, char c) {     for (int i = 0; i < m; i++)       if (grid[row][i] != c) return false;     return true;   }   bool col(int col, char c) {     for (int i = 0; i < n; i++)       if (grid[i][col] != c) return false;     return true;   }   void solve() {     cin >> n >> m;     int a = 0, b = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> grid[i][j];         if (grid[i][j] ==  A )           ++a;         else           ++b;       }     if (a == 0) {       cout <<  MORTAL             <<   n ;       return;     }     if (b == 0) {       cout << 0 <<   n ;       return;     }     if (row(0,  A ) || row(n - 1,  A ) || col(0,  A ) || col(m - 1,  A )) {       cout << 1 <<   n ;       return;     }     for (int i = 0; i < n; i++)       if (row(i,  A )) {         cout << 2 <<   n ;         return;       }     for (int i = 0; i < m; i++)       if (col(i,  A )) {         cout << 2 <<   n ;         return;       }     if (grid[0][0] ==  A  || grid[n - 1][0] ==  A  || grid[0][m - 1] ==  A  ||         grid[n - 1][m - 1] ==  A ) {       cout << 2 <<   n ;       return;     }     for (int i = 0; i < n; i++) {       if (grid[i][0] ==  A  || grid[i][m - 1] ==  A ) {         cout << 3 <<   n ;         return;       }     }     for (int j = 0; j < m; j++) {       if (grid[0][j] ==  A  || grid[n - 1][j] ==  A ) {         cout << 3 <<   n ;         return;       }     }     cout << 4 <<   n ;   }   signed main() {     cin.tie(0), cout.tie(0);     ios::sync_with_stdio(0);     int q;     cin >> q;     while (q--) solve();     return 0;   }   