Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem L
Wall Making Game

The game Wall Making Game, a two-player board game, is all the rage.

This game is played on an $H \times W$ board. Each cell of the board is one of empty, marked, or wall. At the beginning of the game, there is no wall on the board.

In this game, two players alternately move as follows:

A player chooses one of the empty cells (not marked and not wall). If the player can't choose a cell, he loses.

Towards each of the four directions (upper, lower, left, and right) from the chosen cell, the player changes cells (including the chosen cell) to walls until the player first reaches a wall or the outside of the board.

Note that marked cells cannot be chosen in step 1, but they can be changed to walls in step 2.

Fig.1 shows an example of a move in which a player chooses the cell at the third row and the fourth
column.

Fig.1: An example of a move in Wall Making Game.

Your task is to write a program that determines which player wins the game if the two players play optimally from a given initial board.

Input

The first line of the input consists of two integers $H$ and $W$ $(1 \leq H, W \leq 20)$, where $H$ and $W$ are the height and the width of the board respectively. The following $H$ lines represent the initial board. Each of the $H$ lines consists of $W$ characters.

The $j$-th character of the $i$-th line is '.' if the cell at the $j$-th column of the $i$-th row is empty, or 'X' if the cell is marked.

Output

Print "First" (without the quotes) in a line if the first player wins the given game. Otherwise, print "Second" (also without the quotes) in a line.

Sample Input 1

2 2
..
..

Output for the Sample Input 1

Second

Sample Input 2

2 2
X.
..

Output for the Sample Input 2

First

Sample Input 3

4 5
X....
...X.
.....
.....

Output for the Sample Input 3

First

Solution in Scala (judge verdict: Accepted)

object Main extends App {
  import scala.io.StdIn._
  val Array(h, w) = readLine.trim.split(' ').map(_.toInt)
  val state = new State(Array.tabulate(h){_ ⇒ readLine.trim})
  println(
    if (state.grundyNumberAt(0, w - 1, 0, h - 1) == 0) "Second"
    else "First"
  )
  class State(state: Array[String]) {
    private val memo: Array[Array[Array[Array[Option[Int]]]]] = Array.tabulate(state.head.length){_ ⇒ Array.tabulate(state.head.length){_ ⇒ Array.tabulate(state.length){_ ⇒ Array.tabulate(state.length){_ ⇒ None: Option[Int]}}}}
    private val isMarked: Array[Array[Boolean]] = state.map(str ⇒ str.map(_ == 'X').toArray)
    def grundyNumberAt(left: Int, right: Int, top: Int, bottom: Int): Int = {
      if (left > right || top > bottom) 0
      else memo(left)(right)(top)(bottom) match {
        case Some(r) ⇒ r
        case None if left == right || top == bottom ⇒
          if ((left to right).forall(x ⇒ (top to bottom).forall(y ⇒ isMarked(y)(x)))) {
            memo(left)(right)(top)(bottom) = Some(0)
            0
          }
          else {
            memo(left)(right)(top)(bottom) = Some(1)
            1
          }

        case None ⇒
          val next = (left to right).flatMap(x ⇒
            (top to bottom).withFilter(y ⇒ !isMarked(y)(x)).map(y ⇒
              grundyNumberAt(left, x - 1, top, y - 1) ^
                grundyNumberAt(x + 1, right, top, y - 1) ^
                grundyNumberAt(left, x - 1, y + 1, bottom) ^
              grundyNumberAt(x + 1, right, y + 1, bottom)
            )).distinct.toSet
          val g = (0 to 1000).find(i ⇒ !next.contains(i)).get
          memo(left)(right)(top)(bottom) = Some(g)
          g
      }
    }
  }
}