#include <bits/stdc++.h>
using namespace std;
int n, m, Si;
const int M = 1010;
char table[M][M];
char c[4] = {'D', 'I', 'M', 'A'};
int d[5] = {0, 1, 0, -1, 0};
int V[M][M];
struct point {
  int x, y;
} p;
queue<point> q;
int len = -1;
int GT(int s, int t, int l) {
  if (l > Si) {
    return -1;
  }
  for (int i = 0; i < 4; i++) {
    int x = s + d[i], y = t + d[i + 1];
    if (x >= 1 && x <= n && y >= 1 && y <= m && table[x][y] == c[l % 4]) {
      if (V[x][y] > 0)
        len = max(len, l + V[x][y]);
      else if (GT(x, y, l + 1) == -1)
        return -1;
    }
  }
  len = max(len, l);
  V[s][t] = len - l + 1;
  if (l == 1) {
    return len;
  }
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    Si = m * n;
    getchar();
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        scanf("%c", &table[i][j]);
        if (table[i][j] == 'D') {
          p.x = i;
          p.y = j;
          q.push(p);
        }
      }
      getchar();
    }
    memset(V, 0, sizeof(V));
    if (n * m < 4 || q.empty()) {
      cout << "Poor Dima!\n";
      return 0;
    }
    int re = 0;
    while (!q.empty()) {
      len = -1;
      p = q.front();
      q.pop();
      if (V[p.x][p.y] > 0) {
        continue;
      }
      int ll = GT(p.x, p.y, 1);
      if (ll == -1) {
        cout << "Poor Inna!\n";
        return 0;
      } else
        re = max(re, ll);
    }
    if (re >= 4)
      cout << re / 4 << endl;
    else
      cout << "Poor Dima!\n";
  }
  return 0;
}
