#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 17;
int n, m, col[MAXN][MAXN], LP[MAXN][MAXN];
char a[MAXN][MAXN];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
deque<pair<int, int> > topol;
bool mark[MAXN][MAXN];
vector<pair<int, int> > adj[MAXN][MAXN], adjIN[MAXN][MAXN];
bool dfs(int i, int j) {
  col[i][j] = 1;
  bool res = false;
  for (auto v : adj[i][j]) {
    if (col[v.first][v.second] == 2)
      continue;
    else if (col[v.first][v.second] == 1)
      res = true;
    else
      res = res | dfs(v.first, v.second);
  }
  col[i][j] = 2;
  return res;
}
bool Detect_Cycle() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!col[i][j])
        if (dfs(i, j)) return true;
  return false;
}
void topolSort(int i, int j) {
  mark[i][j] = true;
  for (auto v : adj[i][j])
    if (!mark[v.first][v.second]) topolSort(v.first, v.second);
  topol.push_front({i, j});
}
int Longest_Path() {
  int res = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!mark[i][j]) topolSort(i, j);
  for (int i = topol.size() - 1; i >= 0; i--) {
    int tmp = 0;
    for (auto v : adj[topol[i].first][topol[i].second])
      tmp = max(tmp, LP[v.first][v.second]);
    tmp++;
    LP[topol[i].first][topol[i].second] = tmp;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'D') res = max(res, LP[i][j]);
  return res;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  memset(a, '#', sizeof(a));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 'D') {
        for (int k = 0; k < 4; k++)
          if (a[i + dx[k]][j + dy[k]] == 'I')
            adj[i][j].push_back({i + dx[k], j + dy[k]});
      } else if (a[i][j] == 'I') {
        for (int k = 0; k < 4; k++)
          if (a[i + dx[k]][j + dy[k]] == 'M')
            adj[i][j].push_back({i + dx[k], j + dy[k]});
      } else if (a[i][j] == 'M') {
        for (int k = 0; k < 4; k++)
          if (a[i + dx[k]][j + dy[k]] == 'A')
            adj[i][j].push_back({i + dx[k], j + dy[k]});
      } else {
        for (int k = 0; k < 4; k++)
          if (a[i + dx[k]][j + dy[k]] == 'D')
            adj[i][j].push_back({i + dx[k], j + dy[k]});
      }
    }
  if (Detect_Cycle()) {
    cout << "Poor Inna!" << endl;
    return 0;
  }
  int ans = Longest_Path();
  cerr << "# " << ans << endl;
  ans /= 4;
  if (!ans)
    cout << "Poor Dima!" << endl;
  else
    cout << ans << endl;
  return 0;
}
