Problem:
Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080

Solution in Scala (2.11.7) (judge verdict: Runtime Error)

object Main extends App {

  val In = io.StdIn.readLine()
  val A = In.split(" ")

  var arr = Array.ofDim[Int](A(0).toInt, A(1).toInt)

  //0：表 1：裏
  def rev(a: Int): Int = {
    if (a == 0)
      return 1  //表→裏
    else
      return 0  //裏→表
  }

  //(i,j)の周辺のカードを裏返す
  for (i <- 0 to A(0).toInt - 1) {    //iは行操作と定義
    for (j <- 0 to A(1).toInt - 1) {  //jは列操作と定義
      //右
      if (i+1 != A(0).toInt)
        arr(i + 1)(j) = rev(arr(i + 1)(j))
      //左
      if (i-1 != -1)
        arr(i - 1)(j) = rev(arr(i - 1)(j))
      //上
      if (j+1 != A(1).toInt)
        arr(i)(j + 1) = rev(arr(i)(j + 1))
      //下
      if (j-1 != -1)
        arr(i)(j - 1) = rev(arr(i)(j - 1))
      //右上
      if (i+1 != A(0).toInt && j+1 != A(1).toInt)
        arr(i + 1)(j + 1) = rev(arr(i + 1)(j + 1))
      //右下
      if (i+1 != A(0).toInt && j-1 != -1)
        arr(i + 1)(j - 1) = rev(arr(i + 1)(j - 1))
      //左上
      if (i-1 != -1 && j+1 != A(1).toInt)
        arr(i - 1)(j + 1) = rev(arr(i - 1)(j + 1))
      //左下
      if (i-1 != -1 && j-1 != -1)
        arr(i - 1)(j - 1) = rev(arr(i - 1)(j - 1))
    }
  }

  var n = 0

  for (i <- 0 to A(0).toInt - 1) {
    for (j <- 0 to A(1).toInt - 1) {
      n += arr(i)(j)
    }
  }

  val N: Int = A(0).toInt * A(1).toInt - n

  print(N)

}