{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027$$$$$$\u0027, right: \u0027$$$$$$\u0027, display: true},\n {left: \u0027$$$\u0027, right: \u0027$$$\u0027, display: false},\n {left: \u0027$$\u0027, right: \u0027$$\u0027, display: true},\n {left: \u0027$\u0027, right: \u0027$\u0027, display: false}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eInna and Dima bought a table of size \u003cspan class\u003d\"tex-span\"\u003e\u003ci\u003en\u003c/i\u003e × \u003ci\u003em\u003c/i\u003e\u003c/span\u003e in the shop. Each cell of the table contains a single letter: \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eD\u003c/span\u003e\", \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eI\u003c/span\u003e\", \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eM\u003c/span\u003e\", \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eA\u003c/span\u003e\".\u003c/p\u003e\u003cp\u003eInna 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:\u003c/p\u003e\u003col\u003e \u003cli\u003e initially, Inna chooses some cell of the table where letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eD\u003c/span\u003e\" is written; \u003c/li\u003e\u003cli\u003e then Inna can move to some side-adjacent table cell that contains letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eI\u003c/span\u003e\"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eM\u003c/span\u003e\"; then she can go to a side-adjacent cell that contains letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eA\u003c/span\u003e\". Then Inna assumes that she has gone through her sweetheart\u0027s name; \u003c/li\u003e\u003cli\u003e Inna\u0027s next move can be going to one of the side-adjacent table cells that contains letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eD\u003c/span\u003e\" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eD\u003c/span\u003e\" she always goes to the letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eI\u003c/span\u003e\", from the letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eI\u003c/span\u003e\" she always goes the to letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eM\u003c/span\u003e\", from the letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eM\u003c/span\u003e\" she always goes to the letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eA\u003c/span\u003e\", and from the letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eA\u003c/span\u003e\" she always goes to the letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eD\u003c/span\u003e\". \u003c/li\u003e\u003c/ol\u003e\u003cp\u003eDepending 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\u0027t go through his name once. Help Inna find out what maximum number of times she can go through name DIMA.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line of the input contains two integers \u003cspan class\u003d\"tex-span\"\u003e\u003ci\u003en\u003c/i\u003e\u003c/span\u003e and \u003cspan class\u003d\"tex-span\"\u003e\u003ci\u003em\u003c/i\u003e\u003c/span\u003e \u003cspan class\u003d\"tex-span\"\u003e(1 ≤ \u003ci\u003en\u003c/i\u003e, \u003ci\u003em\u003c/i\u003e ≤ 10\u003csup class\u003d\"upper-index\"\u003e3\u003c/sup\u003e)\u003c/span\u003e. \u003c/p\u003e\u003cp\u003eThen follow \u003cspan class\u003d\"tex-span\"\u003e\u003ci\u003en\u003c/i\u003e\u003c/span\u003e lines that describe Inna and Dima\u0027s table. Each line contains \u003cspan class\u003d\"tex-span\"\u003e\u003ci\u003em\u003c/i\u003e\u003c/span\u003e characters. Each character is one of the following four characters: \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eD\u003c/span\u003e\", \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eI\u003c/span\u003e\", \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eM\u003c/span\u003e\", \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eA\u003c/span\u003e\". \u003c/p\u003e\u003cp\u003eNote that it is not guaranteed that the table contains at least one letter \"\u003cspan class\u003d\"tex-font-style-tt\"\u003eD\u003c/span\u003e\".\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eIf Inna cannot go through name DIMA once, print on a single line \"\u003cspan class\u003d\"tex-font-style-tt\"\u003ePoor Dima!\u003c/span\u003e\" without the quotes. If there is the infinite number of names DIMA Inna can go through, print \"\u003cspan class\u003d\"tex-font-style-tt\"\u003ePoor Inna!\u003c/span\u003e\" without the quotes. Otherwise print a single integer — the maximum number of times Inna can go through name DIMA.\u003c/p\u003e"}},{"title":"Examples","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e1 2\nDI\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003ePoor Dima!\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2 2\nMA\nID\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003ePoor Inna!\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eNotes to the samples:\u003c/p\u003e\u003cp\u003eIn the first test sample, Inna cannot go through name DIMA a single time.\u003c/p\u003e\u003cp\u003eIn 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.\u003c/p\u003e\u003cp\u003eIn 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. \u003c/p\u003e"}}]}