dfs 如果最后的值等于最大值(不可能超过1e7)的话就认为是无限的
否则除4就是最大的值
//cyc
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mst(a) memset(a,0,sizeof a)
using namespace std;
typedef pair<int,int> pii;
string s1[1005];
int dp[1005][1005];
int vis[1005][1005];
int n,m;
const int inf=1e7;
int mvs[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char nextc(char c)
{
if(c=='D')return 'I';
if(c=='I')return 'M';
if(c=='M')return 'A';
if(c=='A')return 'D';
}
int dfs(int x,int y)
{
int cur=0;
if(dp[x][y])return dp[x][y];
dp[x][y]=inf;
int xx,yy;
for(int i=0;i<=3;i++){
xx=x+mvs[i][0];
yy=y+mvs[i][1];
char temp;
temp=s1[x][y];
temp=nextc(temp);
// cout<<temp<<endl;
if(xx<0||xx>=n||yy<0||yy>=m)continue;
if(temp!=s1[xx][yy])continue;
cur=max(cur,dfs(xx,yy));
}
return dp[x][y]=min(cur+1,inf);
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++)cin>>s1[i];
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(s1[i][j]=='D'){
// cout<<i<<" "<<j<<endl;
ans=max(ans,dfs(i,j));
}
}
}
// cout<<ans<<endl;
ans/=4;
if(ans==inf/4){
cout<<"Poor Inna!"<<endl;
}
else if(!ans)cout<<"Poor Dima!"<<endl;
else cout<<ans<<endl;
}