#include <bits/stdc++.h>
using namespace std;
ifstream fin("A.in");
ofstream fout("A.out");
int n, m, i, j, a, b, y, minv = 1000000000, t = 0, ok, nr;
char cor[256], s[1001][1001];
int wh[1001][1001];
int viz[1001 * 1001], pred[1001 * 1001], best[1001 * 1001], good[1001 * 1001],
    st[1001 * 1001];
queue<int> q;
vector<int> G[1001 * 1001];
char c[4] = {'D', 'I', 'M', 'A'};
void dfs(int x) {
  viz[x] = 1;
  st[x] = 1;
  for (vector<int>::iterator it = G[x].begin(); it != G[x].end(); ++it) {
    if (!viz[*it])
      dfs(*it);
    else if (st[*it])
      ok = 1;
  }
  st[x] = 0;
}
int main() {
  cin >> n >> m;
  cor['D'] = 'I';
  cor['I'] = 'M';
  cor['M'] = 'A';
  cor['A'] = 'D';
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    for (int j = 0; j < m; ++j) {
      wh[i][j] = ++t;
      if (s[i][j] == 'D') {
        good[wh[i][j]] = 1;
      }
    }
  }
  for (int k = 0; k < 4; ++k) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (s[i][j] == c[k] && good[wh[i][j]]) {
          if (i > 1) {
            if (s[i - 1][j] == cor[s[i][j]]) {
              G[wh[i][j]].push_back(wh[i - 1][j]);
              pred[wh[i - 1][j]]++;
              good[wh[i - 1][j]] = 1;
            }
          }
          if (j > 0) {
            if (s[i][j - 1] == cor[s[i][j]]) {
              G[wh[i][j]].push_back(wh[i][j - 1]);
              pred[wh[i][j - 1]]++;
              good[wh[i][j - 1]] = 1;
            }
          }
          if (i < n) {
            if (s[i + 1][j] == cor[s[i][j]]) {
              G[wh[i][j]].push_back(wh[i + 1][j]);
              pred[wh[i + 1][j]]++;
              good[wh[i + 1][j]] = 1;
            }
          }
          if (j < m - 1) {
            if (s[i][j + 1] == cor[s[i][j]]) {
              G[wh[i][j]].push_back(wh[i][j + 1]);
              pred[wh[i][j + 1]]++;
              good[wh[i][j + 1]] = 1;
            }
          }
        }
      }
    }
  }
  for (int i = 1; i <= t; ++i) {
    if (good[i] && !viz[i]) {
      dfs(i);
    }
  }
  if (ok) {
    cout << "Poor Inna!";
    return 0;
  }
  for (int i = 1; i <= t; ++i) {
    if (pred[i] == 0 && good[i]) q.push(i);
    best[i] = 1;
  }
  while (!q.empty()) {
    int x = q.front();
    for (vector<int>::iterator it = G[x].begin(); it != G[x].end(); ++it) {
      --pred[*it];
      if (pred[*it] == 0) q.push(*it);
      best[*it] = max(best[*it], best[x] + 1);
    }
    q.pop();
  }
  int maxv = 0;
  for (int i = 1; i <= t; ++i) {
    maxv = max(best[i], maxv);
  }
  if (maxv / 4 == 0) {
    cout << "Poor Dima!";
  } else
    cout << maxv / 4;
}
