#include <bits/stdc++.h>
using namespace std;
int n, m;
string s[1001];
int vist[1001][1001];
int val[1001][1001];
string dima = "DIMA";
int dfs(int i, int j) {
  if (val[i][j] != -1) return val[i][j];
  int a = 0, b = 0, c = 0, d = 0;
  if (s[i][j] == 'D') {
    if (vist[i][j] == 1) return 900000000;
    vist[i][j] = 1;
    if (i > 0 && s[i - 1][j] == 'I') a = dfs(i - 1, j);
    if (j > 0 && s[i][j - 1] == 'I') b = dfs(i, j - 1);
    if (i < n - 1 && s[i + 1][j] == 'I') c = dfs(i + 1, j);
    if (j < m - 1 && s[i][j + 1] == 'I') d = dfs(i, j + 1);
    val[i][j] = max(max(max(a, b), c), d);
    return val[i][j];
  }
  if (s[i][j] == 'I') {
    if (i > 0 && s[i - 1][j] == 'M') a = dfs(i - 1, j);
    if (j > 0 && s[i][j - 1] == 'M') b = dfs(i, j - 1);
    if (i < n - 1 && s[i + 1][j] == 'M') c = dfs(i + 1, j);
    if (j < m - 1 && s[i][j + 1] == 'M') d = dfs(i, j + 1);
    val[i][j] = max(max(max(a, b), c), d);
    return val[i][j];
  }
  if (s[i][j] == 'M') {
    if (i > 0 && s[i - 1][j] == 'A') a = dfs(i - 1, j);
    if (j > 0 && s[i][j - 1] == 'A') b = dfs(i, j - 1);
    if (i < n - 1 && s[i + 1][j] == 'A') c = dfs(i + 1, j);
    if (j < m - 1 && s[i][j + 1] == 'A') d = dfs(i, j + 1);
    val[i][j] = max(max(max(a, b), c), d);
    return val[i][j];
  }
  if (s[i][j] == 'A') {
    if (i > 0 && s[i - 1][j] == 'D') a = dfs(i - 1, j);
    if (j > 0 && s[i][j - 1] == 'D') b = dfs(i, j - 1);
    if (i < n - 1 && s[i + 1][j] == 'D') c = dfs(i + 1, j);
    if (j < m - 1 && s[i][j + 1] == 'D') d = dfs(i, j + 1);
    val[i][j] = 1 + max(max(max(a, b), c), d);
    return val[i][j];
  }
}
int main() {
  cin >> n >> m;
  memset(val, 255, sizeof(val));
  for (int i = 0; i < n; i++) cin >> s[i];
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'D') ans = max(ans, dfs(i, j));
  if (ans == 0)
    cout << "Poor Dima!" << endl;
  else if (ans > 2000000)
    cout << "Poor Inna!" << endl;
  else
    cout << ans << endl;
  return 0;
}
