CF1280B.Beingawesomeism

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are an all-powerful being and you have created a rectangular world. In fact, your world is so bland that it could be represented by a r×cr \times c grid. Each cell on the grid represents a country. Each country has a dominant religion. There are only two religions in your world. One of the religions is called Beingawesomeism, who do good for the sake of being good. The other religion is called Pushingittoofarism, who do murders for the sake of being bad.

Oh, and you are actually not really all-powerful. You just have one power, which you can use infinitely many times! Your power involves missionary groups. When a missionary group of a certain country, say aa , passes by another country bb , they change the dominant religion of country bb to the dominant religion of country aa .

In particular, a single use of your power is this:

  • You choose a horizontal 1×x1 \times x subgrid or a vertical x×1x \times 1 subgrid. That value of xx is up to you;
  • You choose a direction dd . If you chose a horizontal subgrid, your choices will either be NORTH or SOUTH. If you choose a vertical subgrid, your choices will either be EAST or WEST;
  • You choose the number ss of steps;
  • You command each country in the subgrid to send a missionary group that will travel ss steps towards direction dd . In each step, they will visit (and in effect convert the dominant religion of) all ss countries they pass through, as detailed above.
  • The parameters xx , dd , ss must be chosen in such a way that any of the missionary groups won't leave the grid.

The following image illustrates one possible single usage of your power. Here, A represents a country with dominant religion Beingawesomeism and P represents a country with dominant religion Pushingittoofarism. Here, we've chosen a 1×41 \times 4 subgrid, the direction NORTH, and s=2s = 2 steps.

You are a being which believes in free will, for the most part. However, you just really want to stop receiving murders that are attributed to your name. Hence, you decide to use your powers and try to make Beingawesomeism the dominant religion in every country.

What is the minimum number of usages of your power needed to convert everyone to Beingawesomeism?

With god, nothing is impossible. But maybe you're not god? If it is impossible to make Beingawesomeism the dominant religion in all countries, you must also admit your mortality and say so.

输入格式

The first line of input contains a single integer tt ( 1t21041 \le t \le 2\cdot 10^4 ) denoting the number of test cases.

The first line of each test case contains two space-separated integers rr and cc denoting the dimensions of the grid ( 1r,c601 \le r, c \le 60 ). The next rr lines each contains cc characters describing the dominant religions in the countries. In particular, the jj -th character in the ii -th line describes the dominant religion in the country at the cell with row ii and column jj , where:

  • "A" means that the dominant religion is Beingawesomeism;
  • "P" means that the dominant religion is Pushingittoofarism.

It is guaranteed that the grid will only contain "A" or "P" characters. It is guaranteed that the sum of the rcr \cdot c in a single file is at most 31063 \cdot 10^6 .

输出格式

For each test case, output a single line containing the minimum number of usages of your power needed to convert everyone to Beingawesomeism, or the string "MORTAL" (without quotes) if it is impossible to do so.

输入输出样例

  • 输入#1

    4
    7 8
    AAPAAAAA
    PPPPAAAA
    PPPPAAAA
    APAAPPPP
    APAPPAPP
    AAAAPPAP
    AAAAPPAA
    6 5
    AAAAA
    AAAAA
    AAPAA
    AAPAP
    AAAPP
    AAAPP
    4 4
    PPPP
    PPPP
    PPPP
    PPPP
    3 4
    PPPP
    PAAP
    PPPP
    

    输出#1

    2
    1
    MORTAL
    4
    

说明/提示

In the first test case, it can be done in two usages, as follows:

Usage 1:

Usage 2:

In the second test case, it can be done with just one usage of the power.

In the third test case, it is impossible to convert everyone to Beingawesomeism, so the answer is "MORTAL".

首页