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 \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 $$$a$$$, passes by another country $$$b$$$, they change the dominant religion of country $$$b$$$ to the dominant religion of country $$$a$$$.In particular, a single use of your power is this:   You choose a horizontal $$$1 \times x$$$ subgrid or a vertical $$$x \times 1$$$ subgrid. That value of $$$x$$$ is up to you;  You choose a direction $$$d$$$. 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 $$$s$$$ of steps;  You command each country in the subgrid to send a missionary group that will travel $$$s$$$ steps towards direction $$$d$$$. In each step, they will visit (and in effect convert the dominant religion of) all $$$s$$$ countries they pass through, as detailed above.  The parameters $$$x$$$, $$$d$$$, $$$s$$$ 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 \times 4$$$ subgrid, the direction NORTH, and $$$s = 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 $$$t$$$ ($$$1 \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 $$$r$$$ and $$$c$$$ denoting the dimensions of the grid ($$$1 \le r, c \le 60$$$). The next $$$r$$$ lines each contains $$$c$$$ characters describing the dominant religions in the countries. In particular, the $$$j$$$-th character in the $$$i$$$-th line describes the dominant religion in the country at the cell with row $$$i$$$ and column $$$j$$$, 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 $$$r \cdot c$$$ in a single file is at most $$$3 \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.

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

NoteIn 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".

object Main {
  import java.io.{BufferedReader, InputStream, InputStreamReader}
  import java.util.StringTokenizer
  import scala.reflect.ClassTag

  def main(args: Array[String]): Unit = {
    val out = new java.io.PrintWriter(System.out)
    new Main(out, new InputReader(System.in)).solve()
    out.flush()
  }

  private[this] val oj = System.getenv("ATCODER_DEBUG") == null
  @inline private final def DEBUG(f: => Unit): Unit = {
    if (!oj){ f }
  }
  @inline private final def debug(as: Array[Boolean]): Unit = if (!oj){ debug(as.map(x => if(x) "1" else "0").mkString) }
  @inline def debug(as: Array[Int]): Unit = if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debug(as: Array[Long]): Unit =if (!oj){ debug(as.mkString(" ")) }
  @inline private final def debugDim(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m.length) { i =>
      debug(m(i))
    }
  }
  @inline private final def debugDimFlip(m: Array[Array[Long]]): Unit = if (!oj){
    REP(m(0).length) { j =>
      REP(m.length) { i =>
        System.err.print(m(i)(j))
        System.err.print(" ")
      }
      System.err.println()
    }
  }
  @inline def debug(s: => String): Unit = DEBUG {
    System.err.println(s)
  }
  @inline private final def debugL(num: => Long): Unit = DEBUG {
    System.err.println(num)
  }
  private def isDebug[A](debug: => A, online: => A): A = {
    if (oj) online else debug
  }

  class InputReader(val stream: InputStream) {
    private[this] val reader = new BufferedReader(new InputStreamReader(stream), 32768)
    private[this] var tokenizer: StringTokenizer = _

    @inline private[this] final def next(): String = {
      while (tokenizer == null || !tokenizer.hasMoreTokens)
        tokenizer = new StringTokenizer(reader.readLine)
      tokenizer.nextToken
    }

    def nextInt(): Int = Integer.parseInt(next())
    def nextLong(): Long = java.lang.Long.parseLong(next())
    def nextChar(): Char = next().charAt(0)

    def ni(): Int = nextInt()
    def nl(): Long = nextLong()
    def nc(): Char = nextChar()
    def ns(): String = next()
    def ns(n: Int): Array[Char] = ns().toCharArray
    def na(n: Int, offset: Int = 0): Array[Int] = map(n)(_ => ni() + offset)
    def na2(n: Int, offset: Int = 0): (Array[Int], Array[Int]) = {
      val A1, A2 = Array.ofDim[Int](n)
      REP(n) { i =>
        A1(i) = ni() + offset
        A2(i) = ni() + offset
      }
      (A1, A2)
    }
    def nm(n: Int, m: Int): Array[Array[Int]] = {
      val A = Array.ofDim[Int](n, m)
      REP(n) { i =>
        REP(m) { j =>
          A(i)(j) = ni()
        }
      }
      A
    }
    def nal(n: Int): Array[Long] = map(n)(_ => nl())
    def nm_c(n: Int, m: Int): Array[Array[Char]] = map(n) (_ => ns(m))
  }

  def REP(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = offset
    val N = n + offset
    while(i < N) { f(i); i += 1 }
  }
  def REP_r(n: Int, offset: Int = 0)(f: Int => Unit): Unit = {
    var i = n - 1 + offset
    while(i >= offset) { f(i); i -= 1 }
  }
  def TO(from: Int, to: Int)(f: Int => Unit): Unit = {
    REP(to - from + 1, from)(f)
  }
  def map[@specialized A: ClassTag](n: Int, offset: Int = 0)(f: Int => A): Array[A] = {
    val res = Array.ofDim[A](n)
    REP(n)(i => res(i) = f(i + offset))
    res
  }

  def sumL(as: Array[Int]): Long = {
    var s = 0L
    REP(as.length)(i => s += as(i))
    s
  }
  def cumSum(as: Array[Int]): Array[Long] = {
    val cum = Array.ofDim[Long](as.length + 1)
    REP(as.length) { i =>
      cum(i + 1) = cum(i) + as(i)
    }
    cum
  }
}

object Workspace {
  import Main._
}

class Main(out: java.io.PrintWriter, sc: Main.InputReader) {
  import sc._
  import Main._
  import java.util.Arrays.sort

  import scala.collection.mutable
  import math.{abs, max, min}
  import mutable.ArrayBuffer
  import Workspace._

  // toIntとか+7とかするならvalにしろ
  final private[this] val MOD = 1000000007

  def solve2(): Unit = {
    val R, C = ni()
    val g = nm_c(R, C)
    def allA = map(R) { i =>
      g(i).forall(_ == 'A')
    }.reduce(_ && _)

    if (allA) {
      out.println(0)
      return
    }

    def allP = map(R) { i =>
      g(i).forall(_ == 'P')
    }.reduce(_ && _)

    if (allA) {
      out.println(0)
    }
    else if (allP) {
      out.println("MORTAL")
    } else {
      out.println(solve3(R, C, g))
    }
  }

  def cnt(N: Int, M: Int)(g: Int => Int => Char) = {
    var res = 1e9.toInt

    def allA(i: Int): Boolean = {
      var allA = true
      REP(M) { j =>
        allA &&= g(i)(j) == 'A'
      }
      allA
    }

    def containsA(i: Int): Boolean = {
      var ok = false
      REP(M) { j =>
        ok ||= g(i)(j) == 'A'
      }
      ok
    }

    REP(N) { i =>
      val cnt = if (allA(i)) {
        2
      } else if (g(i)(0) == 'A' || g(i)(M - 1) == 'A') {
        3
      } else {
        if (containsA(i))
          4
        else
          1e9.toInt
      }

      if (i == 0 || i == N - 1)
        res = min(res, cnt - 1)
      else
        res = min(res, cnt)
    }
    res
  }

  def solve3(R: Int, C: Int, g: Array[Array[Char]]): Int = {
    var ans = 1e9.toInt
    ans = min(ans, cnt(R, C) { r => c =>
      g(r)(c)
    })
    ans = min(ans, cnt(C, R) { c => r =>
      g(r)(c)
    })
    ans
  }

  def solve(): Unit = {
    REP(ni()) { _ =>
      solve2()
    }
  }
}


Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are ai oskols sitting on the i-th wire.  Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the i-th wire). Consequently all the birds on the i-th wire to the left of the dead bird get scared and jump up on the wire number i - 1, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number i + 1, if there exists no such wire they fly away. Shaass has shot m birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots.

The first line of the input contains an integer n, (1 ≤ n ≤ 100). The next line contains a list of space-separated integers a1, a2, ..., an, (0 ≤ ai ≤ 100).  The third line contains an integer m, (0 ≤ m ≤ 100). Each of the next m lines contains two integers xi and yi. The integers mean that for the i-th time Shaass shoot the yi-th (from left) bird on the xi-th wire, (1 ≤ xi ≤ n, 1 ≤ yi). It's guaranteed there will be at least yi birds on the xi-th wire at that moment.

On the i-th line of the output print the number of birds on the i-th wire.

object Solution extends App {
  val in = scala.io.Source.stdin.getLines()
  val n = in.next().toInt
  val birds = in.next().split(" ").map(_.toInt)
  val m = in.next().toInt
  (1 to m).foreach { _ =>
    val Array(x, y) = in.next().split(" ").map(_.toInt)
    val indexX = x - 1
    val birdCount = birds(indexX)
    val up = y - 1
    val down = birdCount - y
    if (indexX != 0)
      birds(indexX - 1) += up
    if (indexX != n - 1)
      birds(indexX + 1) += down
    birds(indexX) = 0
  }
  println(birds.mkString("\n"))
}

object Main extends App  {

    val numberN = readLine().toInt
    val birdsOn = readLine().split("\\s+").map(_.toInt)
    val numberM = readLine().toInt

    for (i <- 0 to (numberM - 1)) {
        val pairXY = readLine().split("\\s+") match { case Array (x, y) => (x.toInt, y.toInt)}

        if (pairXY._1 - 2 >= 0) {
            birdsOn(pairXY._1 - 2) += pairXY._2 - 1;
        }

        if (pairXY._1  <= numberN - 1) {
            birdsOn(pairXY._1) += birdsOn(pairXY._1 - 1) - pairXY._2
        }

        birdsOn(pairXY._1 - 1) = 0
    }

    for (i <- birdsOn) {
        println(i)
    }

}

object A294 {

  import IO._
  import collection.{mutable => cu}

  def main(args: Array[String]): Unit = {
    val Array(n) = readInts(1)
    val ai = readInts(n)
    val Array(m) = readInts(1)
    for(_ <- 1 to m) {
      var Array(x, y) = readInts(2)
      x -= 1
      val curr = ai(x)
      ai(x) = 0
      if(x-1 >= 0) {
        ai(x-1) += y-1
      }
      if(x + 1 < n) {
        ai(x+1) += curr-y
      }
    }

    out.println(ai.mkString("\n"))
    out.close()
  }

  object IO {
    private val input = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    //    private val input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream("src/input")))
    val out = new java.io.PrintWriter(System.out)

    @inline def read: String = input.readLine()

    @inline def tokenizeLine = new java.util.StringTokenizer(read)

    def readInt: Int = tokenizeLine.nextToken.toInt

    def readInts(n: Int): Array[Int] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toInt)
    }

    def readLongs(n: Int): Array[Long] = {
      val tl = tokenizeLine
      Array.fill(n)(tl.nextToken.toLong)
    }

    def readBigs(n: Int): Array[BigInt] = {
      val tl = tokenizeLine
      Array.fill(n)(BigInt(tl.nextToken))
    }
  }

}

import java.util.Scanner
import java.io.PrintWriter
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Random

object P294A extends App {
  val sc = new Scanner(System.in)
  val out = new PrintWriter(System.out)

  val N = sc.nextInt
  val w = Array.fill(N + 2)(Int.MinValue)
  for (i <- 1 to N)
    w(i) = sc.nextInt
  val M = sc.nextInt

  def shoot: Unit = {
    val x, y = sc.nextInt
    val left = y - 1
    val right = w(x) - y
    w(x) = 0
    w(x - 1) += left
    w(x + 1) += right
  }

  for (i <- 0 until M) shoot

  for (i <- 1 to N)
    out.println(w(i))

  out.close
}

object A {
  def READ: Array[Int] = readLine() split (" ") map (_.toInt);

  def main(args: Array[String]) {
    var Array(n) = READ
    var a = READ
    var Array(m) = READ
    for (i <- 1 to m) {
      var Array(x, y) = READ
      if (x > 1) a(x-2) += y-1;
      if (x < n) a(x) += a(x-1)-y;
      a(x-1) = 0;
    }
    a map (println(_))
  }
}

import java.io._
import scala.annotation.tailrec

object Easy {
  val reader = new BufferedReader(new InputStreamReader(System.in))
  def readInt = reader.readLine().toInt
  def readInts = reader.readLine().split(" ").map(_.toInt)

  val n = readInt
  val lines = 0 +: readInts :+ 0
  for(_ <- 1 to readInt) {
    val Array(r, c) = readInts
    lines.update(r - 1, lines(r - 1) + c - 1)
    lines.update(r + 1, lines(r + 1) + lines(r) - c)
    lines.update(r, 0)
  }

  def main(a: Array[String]) {
    println(lines.drop(1).take(n).mkString("\n"))
  }
}

object Main {
  class Wires(val n: Int) {
    private val data = readLine().split(" ").map(_.toInt).toArray

    def shoot() = {
      val s = readLine().split(" ").map(_.toInt)
      val x = s(0)
      val y = s(1)

      val up = y - 1
      val down = data(x - 1) - y

      data(x - 1) = 0
      if (x - 2 >= 0) data(x - 2) += up
      if (x < n) data(x) += down
    }

    def print() {
      data.foreach(println(_))
    }
  }

  def main(args: Array[String]) {
    val n = readInt
    val w = new Wires(n)
    val m = readInt
    for (_ <- 1 to m) { w.shoot() }
    w.print()
  }
}

object CF178Div2A {
  def main(args:Array[String]){
    val n = readLine.toInt
    var arr = readLine.split(" ").map(_.toInt)
    val m = readLine.toInt
    (1 to m).foreach(i =>{
      val Array(x,y) = readLine.split(" ").map(_.toInt - 1)
      if(x > 0) arr(x-1) += y
      if(x < n-1) arr(x+1) += arr(x)-(y+1)
      arr(x) = 0
    })
    arr.foreach(x => println(x))
  }
}

  object Birds extends App{
  		  	def resolve(lines: IndexedSeq[String]) = {
  		  	  		val n = lines(0).toInt
  		  	  		var birdsByWire = Map[Int, Int]()

  		  	  		for ((birds, wire) <- lines(1).split(" ").toList.zip(1 to n)) {
  		  	  		  			birdsByWire = birdsByWire + (wire -> birds.toInt)
  		  	  		 }

  		  	  		val cases = lines.drop(3)
  							for(l <- cases) {

  								val (wire, shot) = {
  									val tokens = l.split(" ")
	  								(tokens(0).toInt, tokens(1).toInt)
  								}

  								val left = shot - 1
  								val right = birdsByWire(wire) - shot

					  			if (n == 1) {
					  				birdsByWire += (n -> 0)
					  			} else if (wire == n) {
					  				birdsByWire += (wire - 1 -> (birdsByWire(wire - 1) + left))
					        	birdsByWire += (n -> 0)
					  			} else if (wire == 1) {
					  				birdsByWire += (wire + 1 -> (birdsByWire(wire + 1) + right))
					  				birdsByWire += (1 -> 0)
					  			} else {
					  				birdsByWire += (wire + 1 -> (birdsByWire(wire + 1) + right))
					  				birdsByWire += (wire - 1 -> (birdsByWire(wire - 1) + left))
					  				birdsByWire += (wire -> 0)
					  			}

						  	}

  							for(w <- 1 to n) println(birdsByWire(w))
  					}

			resolve(io.Source.stdin.getLines.toIndexedSeq)

 }

  object Birds extends App {
  	val lines = io.Source.stdin.getLines.toIndexedSeq
  	val n = lines(0).toInt
  	var birdsByLine = Map[Int, Int]()

  	for ((birds, line) <- lines(1).split(" ").toList.zip(1 to n)) {
  		birdsByLine = birdsByLine + (line -> birds.toInt)
  	}

  	for(l <- lines.drop(3)) {
  		val (shot, line) = {
  			val tokens = l.split(" ")
  			(tokens(1).toInt, tokens(0).toInt)
  		}

  		val birdsToTheLeft = shot - 1
  		val birdsToTheRigth = birdsByLine(line) - shot

  		if (n == 1) {
  			birdsByLine += (n -> 0)
  		} else if (line == n) {
  			birdsByLine += (line - 1 -> (birdsByLine(line - 1) + birdsToTheLeft))
        birdsByLine += (n -> 0)
  		} else if (line == 1) {
  			birdsByLine += (line + 1 -> (birdsByLine(line + 1) + birdsToTheRigth))
  			birdsByLine += (1 -> 0)
  		} else {
  			birdsByLine += (line + 1 -> (birdsByLine(line + 1) + birdsToTheRigth))
  			birdsByLine += (line - 1 -> (birdsByLine(line - 1) + birdsToTheLeft))
  			birdsByLine += (line -> 0)
  		}
  	}

  	birdsByLine.foreach(p => println(p._2))
  }

  object Birds extends App {
  	val lines = io.Source.stdin.getLines.toIndexedSeq
  	val n = lines(0).toInt
  	var birdsByLine = Map[Int, Int]()

  	for ((birds, line) <- lines(1).split(" ").toList.zip(1 to n)) {
  		birdsByLine = birdsByLine + (line -> birds.toInt)
  	}

  	for(l <- lines.drop(3)) {
  		val (shot, line) = {
  			val tokens = l.split(" ")
  			(tokens(1).toInt, tokens(0).toInt)
  		}

  		val birdsToTheLeft = shot - 1
  		val birdsToTheRigth = birdsByLine(line) - shot

  		if (n == 1) {
  			birdsByLine += (n -> 0)
  		} else if (line == n) {
  			birdsByLine += (line - 1 -> (birdsByLine(line - 1) + birdsToTheLeft))
			birdsByLine += (line -> 0)
        birdsByLine += (n -> 0)
  		} else if (line == 1) {
  			birdsByLine += (line + 1 -> (birdsByLine(line + 1) + birdsToTheRigth))
  			birdsByLine += (1 -> 0)
  		} else {
  			birdsByLine += (line + 1 -> (birdsByLine(line + 1) + birdsToTheRigth))
  			birdsByLine += (line - 1 -> (birdsByLine(line - 1) + birdsToTheLeft))
  			birdsByLine += (line -> 0)
  		}
  	}

  	birdsByLine.foreach(p => println(p._2))
  }