#include <bits/stdc++.h>   using namespace std;   char a[60][60];   int ch(int r, int c) {     int i, j, f = 0;     for (i = 0; i < c; i++) {       if (a[0][i] ==  P ) f = 1;     }     if (f == 0) return 0;     f = 0;     for (i = 0; i < c; i++) {       if (a[r - 1][i] ==  P ) f = 1;     }     if (f == 0) return 0;     f = 0;     for (i = 0; i < r; i++) {       if (a[i][0] ==  P ) f = 1;     }     if (f == 0) return 0;     f = 0;     for (i = 0; i < r; i++) {       if (a[i][c - 1] ==  P ) f = 1;     }     return f;   }   int chh(int r, int c) {     int i, j, f = 0;     for (i = 1; i < r - 1; i++) {       f = 0;       for (j = 0; j < c; j++) {         if (a[i][j] ==  P ) f = 1;       }       if (f == 0) return 0;     }     for (i = 1; i < c - 1; i++) {       f = 0;       for (j = 0; j < r; j++) {         if (a[j][i] ==  P ) f = 1;       }       if (f == 0) return 0;     }     return 1;   }   int al(int r, int c) {     int i, j, f = 0;     for (i = 0; i < r; i++) {       for (j = 0; j < c; j++) {         if (a[i][j] ==  P ) return 1;       }     }     return 0;   }   int main() {     int t;     cin >> t;     while (t--) {       int r, c, i, j, f = 0;       cin >> r >> c;       cin.ignore(256,   n );       for (i = 0; i < r; i++) {         string s;         getline(cin, s);         for (j = 0; j < c; j++) a[i][j] = s[j];       }       for (i = 0; i < r; i++) {         for (j = 0; j < c; j++) {           if (a[i][j] ==  A ) {             f = 1;             break;           }         }       }       if (f == 0) {         cout <<  MORTAL n ;       } else if (al(r, c) == 0)         cout << 0 << endl;       else {         if (ch(r, c) == 0)           cout << 1 << endl;         else if (a[0][0] ==  A  || a[0][c - 1] ==  A  || a[r - 1][0] ==  A  ||                  a[r - 1][c - 1] ==  A  || chh(r, c) == 0)           cout << 2 << endl;         else {           f = 0;           for (i = 1; i < c - 1; i++) {             if (a[0][i] ==  A ) f = 1;             if (a[r - 1][i] ==  A ) f = 1;           }           for (i = 1; i < r - 1; i++) {             if (a[i][0] ==  A ) f = 1;             if (a[i][c - 1] ==  A ) f = 1;           }           if (f == 1)             cout << 3 << endl;           else             cout << 4 << endl;         }       }     }   }   