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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::io::*;
use std::str::FromStr;
use std::collections::HashMap;
use std::collections::BTreeSet;
use std::collections::HashSet;
use std::cmp;
use std::f64::consts;

fn main() {
    let height: i32 = read();
    let width: i32 = read();
    let mut vec: Vec<Vec<bool>> = Vec::new();

    for h in 0..height {
        let mut in_vec: Vec<bool> = Vec::new();
        for w in 0..width{
            in_vec.push(false);
        }
        vec.push(in_vec);
    }

    for h in 0..height as i32 {
        for w in 0..width as i32 {
            if vec[h as usize][w as usize] {
                vec[h as usize][w as usize] = false;
            } else {
                vec[h as usize][w as usize] = true;
            }

            if (h-1) >= 0 {
                if vec[(h-1) as usize][w as usize] {
                    vec[(h-1) as usize][w as usize] = false;
                } else {
                    vec[(h-1) as usize][w as usize] = true;
                }
            }

            if h-1 >= 0 && w+1 < width {
                if vec[h as usize-1][w as usize+1] {
                    vec[h as usize-1][w as usize+1] = false;
                } else {
                    vec[h as usize-1][w as usize+1] = true;
                }
            }

            if w+1 < width {
                if vec[h as usize][w as usize+1] {
                    vec[h as usize][w as usize+1] = false;
                } else {
                    vec[h as usize][w as usize+1] = true;
                }
            }

            if w+1 < width && h+1 < height {
                if vec[h as usize+1][w as usize+1] {
                    vec[h as usize+1][w as usize+1] = false;
                } else {
                    vec[h as usize+1][w as usize+1] = true;
                }
            }

            if h+1 < height {
                if vec[h as usize+1][w as usize] {
                    vec[h as usize+1][w as usize] = false;
                } else {
                    vec[h as usize+1][w as usize] = true;
                }
            }

            if h+1 < height && w-1 >= 0 {
                if vec[h as usize+1][w as usize-1] {
                    vec[h as usize+1][w as usize-1] = false;
                } else {
                    vec[h as usize+1][w as usize-1] = true;
                }
            }

            if w-1 >= 0 {
                if vec[h as usize][w as usize-1] {
                    vec[h as usize][w as usize-1] = false;
                } else {
                    vec[h as usize][w as usize-1] = true;
                }
            }

            if w-1>=0 && h-1>=0 {
                if vec[h as usize-1][w as usize-1] {
                    vec[h as usize-1][w as usize-1] = false;
                } else {
                    vec[h as usize-1][w as usize-1] = true;
                }
            }
//            println!("{:?}", vec);
        }
    }

    let mut count = 0;

    for h in 0..height {
        for w in 0..width {
            if vec[h as usize][w as usize] {
                count+=1;
            }
        }
    }

    println!("{}", count);
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

// 最大公約数
fn gcd(a: i32, b: i32) -> i32 {
    match b {
        0 => a,
        _ => gcd(b, a % b)
    }
}

// 最小公倍数
fn lcm(a: i32, b: i32) -> i32 {
    a * b / gcd(a, b)
}

// 階乗
fn kaijou(n: i32)->i32 {
    if n == 1 {
        return n;
    }
    return n * kaijou(n-1);
}