Inna and Dima
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Inna and Dima bought a table of size n × m in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A".

Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows:

  1. initially, Inna chooses some cell of the table where letter "D" is written;
  2. then Inna can move to some side-adjacent table cell that contains letter "I"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter "M"; then she can go to a side-adjacent cell that contains letter "A". Then Inna assumes that she has gone through her sweetheart's name;
  3. Inna's next move can be going to one of the side-adjacent table cells that contains letter "D" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter "D" she always goes to the letter "I", from the letter "I" she always goes the to letter "M", from the letter "M" she always goes to the letter "A", and from the letter "A" she always goes to the letter "D".

Depending on the choice of the initial table cell, Inna can go through name DIMA either an infinite number of times or some positive finite number of times or she can't go through his name once. Help Inna find out what maximum number of times she can go through name DIMA.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 103).

Then follow n lines that describe Inna and Dima's table. Each line contains m characters. Each character is one of the following four characters: "D", "I", "M", "A".

Note that it is not guaranteed that the table contains at least one letter "D".

Output

If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer — the maximum number of times Inna can go through name DIMA.

Examples
input
1 2
DI

output
Poor Dima!

input
2 2
MA
ID

output
Poor Inna!

input
5 5
DIMAD
DIMAI
DIMAM
DDMAA
AAMID

output
4

Note

Notes to the samples:

In the first test sample, Inna cannot go through name DIMA a single time.

In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner.

In the third test sample the best strategy is to start from the cell in the upper left corner of the table. Starting from this cell, Inna can go through name DIMA four times.

DFS搜索!

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3fint index[4][2]={0,1,0,-1,-1,0,1,0};
char mp[1005][1005];//用于记录走的桌子
int f[1005][1005];//记录不同搜索次序是否走到了同一个点,走到就直接返回该值
int v[1005][1005];//用于记录同一次是否走回去了,形成了回环!形成则返回无穷!
int n,m;
int ans;int dfs(int x,int y)
{int dx,dy;int ret=0;if(f[x][y]!=-1) return f[x][y];if(v[x][y]) return inf;v[x][y]=1;for(int i=0;i<4;i++){dx=x+index[i][0];dy=y+index[i][1];if(x<0||x>n-1||y<0||y>m-1)continue ;if(mp[x][y]=='D'&&mp[dx][dy]=='I') ret=max(dfs(dx,dy),ret);else if(mp[x][y]=='I'&&mp[dx][dy]=='M') ret=max(dfs(dx,dy),ret);else if(mp[x][y]=='M'&&mp[dx][dy]=='A') ret=max(dfs(dx,dy),ret);else if(mp[x][y]=='A'&&mp[dx][dy]=='D') ret=max(dfs(dx,dy),ret);}v[x][y]=0;if(mp[x][y]=='A'){f[x][y]=ret+1;return ret+1;}elsereturn ret;
}int main()
{while(scanf("%d%d",&n,&m)!=EOF){memset(f,-1,sizeof(f));memset(v,0,sizeof(v));ans=0;for(int i=0;i<n;i++)scanf("%s",mp[i]);for(int i=0;i<n;i++)for(int j=0;j<m;j++){if(mp[i][j]=='D'){ans=max(dfs(i,j),ans);}}if(ans==0) puts("Poor Dima!");else if(ans>=inf) puts("Poor Inna!");else printf("%d\n",ans);}return 0;
}

C. Inna and Dima相关推荐

  1. [codeforces366C]Dima and Salad

    time limit per test : 1 second memory limit per test : 256 megabytes Dima, Inna and Seryozha have ga ...

  2. Codeforces Round #220 (Div. 2)

    A. Inna and Pink Pony 题意:给出如下参数,          n, m, i, j, a, b (1 ≤ n, m ≤ 106; 1 ≤ i ≤ n; 1 ≤ j ≤ m; 1  ...

  3. Codeforces #208 div2前两题及思维风暴

    昨晚原本准备在宿舍打cf的,结果吵吵闹闹的,也没打成,头也晕晕的,当时看了只看了第一个题,越想越麻烦,最后竟然陷入了误区,半小时也没解,虽然注册了,一发也没交... A. Dima and Conti ...

  4. Codeforces2000分左右DP泛刷

    乱七八糟的DP题随便刷刷 文章目录 CF148E - Porcelain CF1131D - Gourmet choice CF629C - Famil Door and Brackets CF895 ...

  5. CF 400 div2

    从昨天周赛开始,要耍CF了~ 开始还害怕个人赛自己数论压根不会怎么办,发现CF全是乱搞的题orz 但是就算是乱搞的也不能1A  T^T 犯的错误基本上都是没有把情况归类导致要么是情况想少了,要么是细节 ...

  6. Codeforces 358 D. Dima and Hares

    dp[i][0]表示i号兔子先于i-1号兔子喂食,dp[i][1]反过来. 倒着DP D. Dima and Hares time limit per test 2 seconds memory li ...

  7. CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段)

    CodeForces 390E Inna and Large Sweet Matrix(树状数组改段求段) 树状数组仅仅能实现线段树区间改动和区间查询的功能,能够取代不须要lazy tag的线段树.且 ...

  8. codeforces 374A Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行  m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...

  9. Codeforces Round #167 (Div. 1) C. Dima and Horses(BFS+贪心)

    题目大意 有 n(1≤n≤3*105) 匹马,每条马都有几个敌人(不超过 3 个),现在要求把这些马分成两部分(允许一部分中没有一条马),使得对于每条马,和它在同一部分中的敌人的数量不超过1个 给出了 ...

  10. codeforce 272B Dima and Sequence

    B. Dima and Sequence Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisti ...

最新文章

  1. linux修改主机名+免密认证+关闭防火墙
  2. java通过ip获取网卡MAC地址
  3. 年度调薪一般涨多少_又到年底,HR 你拿什么标准来调薪?
  4. c语言中二重指针如何赋值,关于二重指针释放的有关问题
  5. Android通讯录查询篇--ContactsContract.Data 二(续)
  6. JVM学习手册(X):查看堆内存使用情况以及排错
  7. svn 存储方式BDB与FSFS比较
  8. 济安横断面为什么会有水印_PS差值模式是如何去水印的?你看的懂抖音的沙雕做法?...
  9. 建立自己的voc数据集_Mac上 制作自己的VOC数据集
  10. XRD测试常见问题及解答(二)
  11. Css3之画五角星跟六角星
  12. 猫和老鼠服务器维护多久结束,猫和老鼠手游:长时间不玩游戏,再次进入游戏后会发生这些事...
  13. 2022年金砖国家职业技能大赛(决赛)网络空间安全赛项 | 浙江赛区选拔赛 任务书
  14. 陪您幸福一辈子的牛皮凉席
  15. TEZ和MR简要区别
  16. fatal error C1010:在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include“stdafx.h“”
  17. 用Python+Moviepy+OpenCV制作灯光秀短视频
  18. 英文写作:讨论的中文逻辑是什么?讨论怎么写?
  19. wangeditor支持图片和视频上传
  20. python猜数游戏实验报告_python实现猜数游戏

热门文章

  1. Python之abandon(一)
  2. Android 设计模式之二:MVP模式与MVC模式 .
  3. 苹果电脑上的Word打不开怎么办?Word文件怎么恢复?
  4. 生命已经这么短,为什么还不追求自己真正想要的
  5. 物联网大赛 - Android学习笔记(三)Android 事件处理
  6. shopnc mysql_(转) shopnc数据库操作
  7. Ant Design Pro 使用Authorized组件做权限验证
  8. Matlab绘制经纬度地图并添加坐标点
  9. 什么是青藤零域·微隔离安全平台?
  10. 我对TCP CDG拥塞控制算法的改进和优化