#include <bits/stdc++.h>
using namespace std;
int dir[4] = {1, -1, 0, 0}, dir2[4] = {0, 0, 1, -1};
int n, m;
struct node {
  int x, y;
};
node cc[1005 * 1005];
vector<int> g[1005 * 1005];
int d[1005 * 1005], num[1005][1005], vis[1005 * 1005], dp[1005 * 1005];
char s[1005][1005];
void link(int v) {
  int x = cc[v].x, y = cc[v].y;
  for (int i = 0; i < 4; ++i) {
    if (x + dir[i] < 1 || x + dir[i] > n || y + dir2[i] < 1 || y + dir2[i] > m)
      continue;
    int u = num[x + dir[i]][y + dir2[i]];
    if (s[x][y] == 'D' && s[x + dir[i]][y + dir2[i]] == 'I') {
      g[v].push_back(u);
      d[u]++;
    }
    if (s[x][y] == 'I' && s[x + dir[i]][y + dir2[i]] == 'M') {
      g[v].push_back(u);
      d[u]++;
    }
    if (s[x][y] == 'M' && s[x + dir[i]][y + dir2[i]] == 'A') {
      g[v].push_back(u);
      d[u]++;
    }
    if (s[x][y] == 'A' && s[x + dir[i]][y + dir2[i]] == 'D') {
      g[v].push_back(u);
      d[u]++;
    }
  }
}
void dfs(int x) {
  vis[x] = 1;
  for (auto i : g[x]) {
    if (vis[i] && s[cc[i].x][cc[i].y] == 'D') {
      cout << "Poor Inna!";
      exit(0);
    }
    if (dp[i] < dp[x] + 1) {
      dp[i] = max(dp[i], dp[x] + 1);
      dfs(i);
    }
  }
  vis[x] = 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (long long(i) = 1; (i) <= (n); i++) cin >> (s[i] + 1);
  int cnt = 1;
  for (long long(i) = 1; (i) <= (n); i++) {
    for (long long(j) = 1; (j) <= (m); j++) {
      num[i][j] = cnt;
      cc[cnt].y = j;
      cc[cnt++].x = i;
    }
  }
  for (long long(i) = 1; (i) <= (cnt); i++) {
    link(i);
  }
  queue<int> q;
  for (long long(i) = 1; (i) <= (cnt); i++) {
    if (s[cc[i].x][cc[i].y] == 'D') {
      dp[i] = 1;
      q.push(i);
    }
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    dfs(x);
  }
  int ans = 0;
  for (long long(i) = 1; (i) <= (cnt); i++) {
    if (s[cc[i].x][cc[i].y] == 'A') ans = max(ans, dp[i]);
  }
  if (ans == 0) {
    cout << "Poor Dima!";
    return 0;
  }
  cout << ans / 4;
  return 0;
}
