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



Sample input:
2 2


Sample output:
0

Submission status: Time Limit Exceeded

Solution code:
S = input().split(" ")
N = int(S[0])
M = int(S[1])

def calculate(n,m):
    result = []
    for i in range(n):
        for j in range(m):
            count = 0
            if i - 1 >=0:
                count = count + 1

            if i + 1 <= n - 1:
                count = count + 1

            if j - 1 >= 0:
                count = count + 1

            if j + 1 <= m - 1:
                count = count + 1

            if (i -1 >= 0) and (j-1 >= 0):
                count = count + 1

            if (i - 1 >= 0) and (j + 1 <= m - 1):
                count = count + 1

            if (i + 1 <= n - 1) and (j - 1 >= 0):
                count = count + 1

            if (i + 1 <= n - 1) and (j + 1 <= m - 1):
                count = count + 1


            result.append((count+1) % 2)

    print(sum(result))
calculate(N,M)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split(" "))


def get_edges(n, m):
    edges = list()
    for x in range(m):
        edges.append((x, 0))
        edges.append((x, n-1))
    for y in range(n):
        edges.append((0, y))
        edges.append((m-1, y))
    return set(edges)


def get_num_center(n, m):
    x = m - 2
    y = n - 2
    if x <= 0 or y <= 0:
        return 0
    else:
        return x * y


def get_next(x, y):
    nexts = ((x-1, y-1),
            (x-1, y),
            (x-1, y+1),
            (x, y-1),
            (x, y),
            (x, y+1),
            (x+1, y-1),
            (x+1, y),
            (x+1, y+1)
             )
    count = 0
    for next in nexts:
        x, y = next
        if 0 <= x <= M-1:
            if 0 <= y <= N-1:
                count += 1
    return count % 2


on_edges = get_edges(N, M)
on_center = get_num_center(N, M)

count = 0
for on_edge in on_edges:
    count += get_next(*on_edge)
count += on_center
print(count)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split(" "))

def get_next(x, y):
    nexts = ((x-1, y-1),
            (x-1, y),
            (x-1, y+1),
            (x, y-1),
            (x, y),
            (x, y+1),
            (x+1, y-1),
            (x+1, y),
            (x+1, y+1))
    count = 0
    for next in nexts:
        x, y = next
        if 0 <= x <= M-1:
            if 0 <= y <= N-1:
                count += 1
    return count


result = 0
for x in range(M):
    for y in range(N):
        neighbors = get_next(x, y)
        if neighbors % 2 == 1:
            result += 1
print(result)

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
ans = 0
for i in range(1,n+1):
    if i == 1 or i == n:
        continue
    ans += abs(m-2)
if n == 1:
    if m == 1:
        ans = 1
    else:
        ans = m-2
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def check(i, j, n, m):
  cnt = 0
  for a in [- 1, 0, 1]:
    for b in [- 1, 0, 1]:
      if(0 <= i + a and i + a < n and 0 <= j + b and j + b < m):
        cnt += 1
  if(cnt % 2 == 0):
    return 0
  else:
    return 1

n, m = list(map(int, input().split()))
cnt = 0
for i in range(n):
  for j in range(m):
    cnt += check(i, j, n, m)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int, input().split())
f=[[1 for _ in range(m+2)] for _ in range(n+2)]

# 1:表，0:裏
# 裏返す:-1を加える

for i in range(1,n+1):
    for j in range(1,m+1):
        for k in range(i-1,i+2):
            for l in range(j-1,j+2):
                f[k][l]-=1
                if f[k][l]==-1:
                    f[k][l]=1

cnt=0
for i in range(1,n+1):
    for j in range(1,m+1):
        if f[i][j]==0:
            cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
grid = [[True for _ in range(m)] for _ in range(n)]
dict = (
    (-1, -1), (-1, 0), (-1, 1),
    (0, -1), (0, 0), (0, 1),
    (1, -1), (1, 0), (1, 1),
)

for i in range(0, n):
    for j in range(0, m):
        for d in dict:
            dy, dx = d
            y = i+dy
            x = j+dx
            if y >= 0 and y < n and x >= 0 and x < m:
                grid[y][x] = not grid[y][x]

f = sum(grid, [])
print(f.count(False))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())

grid = [['#' for _ in range(m + 2)]]
for i in range(n):
    row = ['#'] + [True for _ in range(m)] + ['#']
    grid.append(row)
grid.append(['#' for _ in range(m + 2)])

dict = (
    (-1, -1), (-1, 0), (-1, 1),
    (0, -1), (0, 0), (0, 1),
    (1, -1), (1, 0), (1, 1),
)

for i in range(1, n+1):
    for j in range(1, m+1):
        for d in dict:
            dy, dx = d
            y = i+dy
            x = j+dx
            if grid[y][x] != '#':
                grid[y][x] = not grid[y][x]


f = sum(grid, [])
print(f.count(False))

Submission status: Time Limit Exceeded

Solution code:
(n, m) = map(int, input().split())
s = []
s.append([0 for _ in range(m + 2)])
for _ in range(n):
    s.append([0] + [1 for _ in range(m)] + [0])
s.append([0 for _ in range(m + 2)])

sans = [[0 for _ in range(m + 2)] for _ in range(n + 2)]

for i in range(1, n + 1):
    for j in range(1, m + 1):
        sans[i][j] = (\
        s[i + 1][j + 1]+ \
        s[i + 1][j]+ \
        s[i + 1][j - 1]+ \
        s[i][j + 1]+ \
        s[i][j] + \
        s[i][j - 1]+ \
        s[i - 1][j + 1]+ \
        s[i - 1][j]+ \
        s[i - 1][j - 1]) % 2

sums = 0
for i in range(len(sans)):
    sums += sum(sans[i])
print(sums)

Submission status: Time Limit Exceeded

Solution code:

def main():
    n, m = [int(i) for i in input().split()]

    def flip(v):
        return '.' if v == '#' else '#'

    a = [['#' for _ in range(n)] for _ in range(m)]
    for i in range(n):
        for j in range(m):
            a[j  ][i  ] = flip(a[j  ][i  ])
            if j-1 >= 0:
                a[j-1][i  ] = flip(a[j-1][i  ])
            if i-1 >= 0:
                a[j  ][i-1] = flip(a[j  ][i-1])
            if j+1 < m:
                a[j+1][i  ] = flip(a[j+1][i  ])
            if i+1 < n:
                a[j  ][i+1] = flip(a[j  ][i+1])
            if j+1 < m and i+1 < n:
                a[j+1][i+1] = flip(a[j+1][i+1])
            if j-1 >= 0 and i-1 >= 0:
                a[j-1][i-1] = flip(a[j-1][i-1])
            if j+1 < m and i-1 >= 0:
                a[j+1][i-1] = flip(a[j+1][i-1])
            if j-1 >= 0 and i+1 < n:
                a[j-1][i+1] = flip(a[j-1][i+1])

    c = 0
    for i in range(n):
        for j in range(m):
            if a[j][i] == '.':
                c += 1

    print(c)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

# 入力を各変数に格納
n, m = map(int, input().split())

cells = [[1 for i in range(n)] for j in range(m)]

# カードを裏返す関数
def turnCard(_y, _x):

    global cells

    if cells[_y][_x] == 0:
        cells[_y][_x] = 1
    elif cells[_y][_x] == 1:
        cells[_y][_x] = 0

# 各マスを周回して8方向に裏返し処理
for y in range(m):
    for x in range(n):

        # そのマスを裏返す
        turnCard(y, x)
        # 左端以外
        if x != 0:
            # 左隣を裏返す
            turnCard(y, x-1)
        # 右端以外
        if x != n-1:
            # 右隣を裏返す
            turnCard(y, x+1)
        # 最上段以外
        if y != 0:
            # ひとつ上を裏返す
            turnCard(y-1, x)
        # 最下段以外
        if y != m-1:
            # ひとつ下を裏返す
            turnCard(y+1, x)
        # 左端と最上段以外
        if x != 0 and y != 0:
            # 左斜め上を裏返す
            turnCard(y-1, x-1)
        # 右端と最上段以外
        if x != n-1 and y != 0:
            # 右斜め上を裏返す
            turnCard(y-1, x+1)
        # 左端と最下段以外
        if x != 0 and y != m-1:
            # 左斜め下を裏返す
            turnCard(y+1, x-1)
        # 右端と最下段以外
        if x != n-1 and y != m-1:
            # 右斜め下を裏返す
            turnCard(y+1, x+1)
        
# 各マスを周回して裏のマスで+1
ans = 0
for y in range(m):
    for x in range(n):
        if cells[y][x] == 0:
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
[N, M] = [int(i) for i in input().split()]
a = [[0 for m in range(M+2)] for n in range(N+2)]
ans = 0

def func(n, m):
    for i in [-1,0,1]:
        for j in [-1,0,1]:
            if a[n+i][m+j] == 0:
                a[n+i][m+j] = 1
            else:
                a[n+i][m+j] = 0

for n in range(1, N+1):
    for m in range(1, M+1):
        if n in (0, N+1) or m in (0, M+1):
            continue
        else:
            func(n, m)

for i in range(1, N+1):
    for j in range(1, M+1):
        ans += a[i][j]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
[N, M] = [int(i) for i in input().split()]
a = [[0 for m in range(M+2)] for n in range(N+2)]
ans = 0

def rev(i):
    if i == 0:
        return 1
    if i == 1:
        return 0

def func(n, m):
    for i in [-1,0,1]:
        for j in [-1,0,1]:
            a[n+i][m+j] = rev(a[n+i][m+j])


for n in range(1, N+1):
    for m in range(1, M+1):
        if n in (0, N+1) or m in (0, M+1):
            continue
        else:
            func(n, m)

for i in range(1, N+1):
    for j in range(1, M+1):
        ans += a[i][j]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())

count = 0
for x in range(N):
    for y in range(M):
        sur = 1
        if x-1 >= 0 and y -1 >= 0: sur +=1
        if y -1 >= 0:               sur +=1
        if x+1 < N and y -1 >= 0:  sur +=1
        if x-1 >= 0:                sur +=1
        if x+1 < N:                 sur +=1
        if x-1 >= 0 and y +1 < M:  sur +=1
        if y +1 < M:                sur +=1
        if x+1 < N and y +1 < M:   sur +=1
        if sur %2 == 1:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())

field = []
for i in range(n + 2):
    row = [0 for j in range(m + 2)]
    field.append(row)
#print(field)

for i in range(1, n + 1):
    for j in range(1, m + 1):
        field[i][j] = 1
#print(field)

for i in range(1, n + 1):
    for j in range(1, m + 1):
        # 左上
        field[i - 1][j - 1] *= -1
        # 上
        field[i - 1][j] *= -1
        # 右上
        field[i - 1][j + 1] *= -1
        # 左
        field[i][j - 1] *= -1
        # 自分
        field[i][j] *= -1
        # 右
        field[i][j + 1] *= -1
        # 左下
        field[i + 1][j - 1] *= -1
        # 下
        field[i + 1][j] *= -1
        # 右下
        field[i + 1][j + 1] *= -1

count = 0
for i in range(1, n + 1):
    for j in range(1, m + 1):
        if field[i][j] == -1:
            count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
def back(x, y, N, M):
    if x == 0 and x == (N - 1) and y == 0 and y == (M - 1):
        return True

    if x == 0 and x == (N - 1) and (y == 0 or y == (M - 1)):
        return False

    if (x == 0 or x == (N - 1)) and y == 0 and y == (M - 1):
        return False

    if x == 0 and x == (N - 1):
        return True

    if y == 0 and y == (M - 1):
        return True

    if x == 0:
        return False

    if x == (N - 1):
        return False

    if y == 0:
        return False

    if y == (M - 1):
        return False

    return True



N, M = [int(i) for i in input().split()]

print(len([None for i in range(N) for j in range(M) if back(i, j, N, M)]))

Submission status: Time Limit Exceeded

Solution code:
def back(p, N, M):
    x, y = p
    if x == 0 and x == (N - 1) and y == 0 and y == (M - 1):
        return True

    if x == 0 and x == (N - 1) and (y == 0 or y == (M - 1)):
        return False

    if (x == 0 or x == (N - 1)) and y == 0 and y == (M - 1):
        return False

    if x == 0 and x == (N - 1):
        return True

    if y == 0 and y == (M - 1):
        return True

    if x == 0:
        return False

    if x == (N - 1):
        return False

    if y == 0:
        return False

    if y == (M - 1):
        return False

    return True



N, M = [int(i) for i in input().split()]

print(len([p for p in [[i, j] for i in range(N) for j in range(M)] if back(p, N, M)]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    #1. input
    m,n = map(int, input().split())
    
    #2. make the list of valid field
    valid_field = list()
    for i in range(1,m+1):
        for j in range(1, n+1):
            valid_field.append([i,j])
    
    #3. judge the number of being turned
    num_being_turned = [0]*len(valid_field)
    cnt = 0
    for i in range(1, m+1):
        for j in range(1, n+1):
            num_being_turned[cnt] = int([i-1,j-1] in valid_field) \
                                                      + int([i-1,j] in valid_field) \
                                                      + int([i-1,j+1] in valid_field) \
                                                      + int([i,j-1] in valid_field) \
                                                      + int([i,j] in valid_field) \
                                                      + int([i,j+1] in valid_field) \
                                                      + int([i+1,j-1] in valid_field) \
                                                      + int([i+1,j] in valid_field) \
                                                      + int([i+1,j+1] in valid_field)
            cnt = cnt+1
                                           
    #4. output
    for i in range(len(num_being_turned)):
        num_being_turned[i] = num_being_turned[i] % 2
    
    print(sum(num_being_turned))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N, M = (int(i) for i in input().split())

tiles = [[True for i in range(M + 2)] for j in range(N + 2)]
for i in range(1, N + 1):
    for j in range(1, M + 1):
        tiles[i - 1][j - 1] = not (tiles[i - 1][j - 1])
        tiles[i - 1][j] = not (tiles[i - 1][j])
        tiles[i - 1][j + 1] = not (tiles[i - 1][j + 1])
        tiles[i][j - 1] = not (tiles[i][j - 1])
        tiles[i][j] = not (tiles[i][j])
        tiles[i][j + 1] = not (tiles[i][j + 1])
        tiles[i + 1][j - 1] = not (tiles[i + 1][j - 1])
        tiles[i + 1][j] = not (tiles[i + 1][j])
        tiles[i + 1][j + 1] = not (tiles[i + 1][j + 1])

count = 0

for i in range(1, N + 1):
    for j in range(1, M + 1):
        if not tiles[i][j]:
            count = count + 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())

C = [[0 for i in range(M)] for j in range(N)]
#print(C)
counter=0

for i in range(N):
    for j in range(M):
        C[i][j]+=1
#        print(i)
#        print(j)
#        print('Cij')
        if i-1>=0:
             if j-1>=0:
                 C[i-1][j-1]+=1
#                 print(C)
        if i+1<=N-1:
             if j-1>=0:
                C[i+1][j-1]+=1

        if i+1<=N-1:
             if j+1<=M-1:
                C[i+1][j+1]+=1
        if i-1>=0:
             if j+1<=M-1:
                C[i-1][j+1]+=1
        if j+1<=M-1:
            C[i][j+1]+=1
        if j-1>=0:
            C[i][j-1]+=1
        if i-1>=0:
            C[i-1][j]+=1
        if i+1<=N-1:
           C[i+1][j]+=1

for i in range(N):
    for j in range(M):
        if (C[i][j]%2==1):
            counter+=1

print(counter)

Submission status: Time Limit Exceeded

Solution code:
def turn(x):
    return 1 if x==0 else 0

input_data = input().split()
n = int(input_data[0])
m = int(input_data[1])

array = [[-1 for _ in range(m+2)] for _ in range(n+2)]

for i in range(1, n+1):
    for j in range(1, m+1):
        array[i][j] = 1

for i in range(1, n+1):
    for j in range(1, m+1):
        if array[i-1][j-1] != -1 : array[i-1][j-1] = turn(array[i-1][j-1])
        if array[i-1][j]   != -1 : array[i-1][j]   = turn(array[i-1][j])
        if array[i-1][j+1] != -1 : array[i-1][j+1] = turn(array[i-1][j+1])
        if array[i][j-1]   != -1 : array[i][j-1]   = turn(array[i][j-1])
        if array[i][j+1]   != -1 : array[i][j+1]   = turn(array[i][j+1])
        if array[i+1][j-1] != -1 : array[i+1][j-1] = turn(array[i+1][j-1])
        if array[i+1][j]   != -1 : array[i+1][j]   = turn(array[i+1][j])
        if array[i+1][j+1] != -1 : array[i+1][j+1] = turn(array[i+1][j+1])

count = 0

for i in range(1, n+1):
    for j in range(1, m+1):
        count += array[i][j]

print(count)

Submission status: Time Limit Exceeded

Solution code:
rows, columns = map(int, input().split(' '))

result = 0
for row in range(rows):
    for column in range(columns):
        target_time = 0

        inc_column = column + 1 < columns
        dec_column = 0 <= column - 1
        inc_row = row + 1 < rows
        dec_row = 0 <= row - 1

        if inc_column:
            if inc_row:
                target_time += 1
            if dec_row:
                target_time += 1
        if dec_column:
            if inc_row:
                target_time += 1
            if dec_row:
                target_time += 1

        if dec_column:
            target_time += 1
        if inc_column:
            target_time += 1
        if inc_row:
            target_time += 1
        if dec_row:
            target_time += 1

        if target_time % 2 == 0:
            result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
rows, columns = map(int, input().split(' '))

result = 0
for row in range(rows):
    for column in range(columns):
        target_time = 0
        if column + 1 < columns and row + 1 < rows:
            target_time += 1
        if column + 1 < columns and 0 <= row - 1:
            target_time += 1
        if 0 <= column - 1 and row + 1 < rows:
            target_time += 1
        if 0 <= column - 1 and 0 <= row - 1:
            target_time += 1

        if 0 <= column - 1:
            target_time += 1
        if column + 1 < columns:
            target_time += 1
        if row + 1 < rows:
            target_time += 1
        if 0 <= row - 1:
            target_time += 1

        if target_time % 2 == 0:
            result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:

def main():
    N,M =list(map( int,input().split()))
    arr = [[1 for i in range(N+2)] for j in range(M+3)]
    ans = 0
    
    for y in range(1,N+1):
        for x in range(1,M+1):
                arr[x][y] = 0 if arr[x][y] else 1 
                arr[x-1][y] = 0 if arr[x-1][y] else 1 
                arr[x][y-1] = 0 if arr[x][y-1] else 1 
                arr[x-1][y-1] = 0 if arr[x-1][y-1] else 1 
                arr[x+1][y] = 0 if arr[x+1][y] else 1 
                arr[x][y+1] = 0 if arr[x][y+1] else 1 
                arr[x+1][y+1] = 0 if arr[x+1][y+1] else 1
                arr[x+1][y-1] = 0 if arr[x+1][y-1] else 1
                arr[x-1][y+1] = 0 if arr[x-1][y+1] else 1
                
    
    for y in range(1,N+1):
        for x in range(1,M+1):
            if arr[x][y] == 0:
                ans = ans + 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from pprint import pprint
def uragaesi(x,y):
    global arr
    arr[x][y] = 0 if arr[x][y] else 1 
    arr[x-1][y] = 0 if arr[x-1][y] else 1 
    arr[x][y-1] = 0 if arr[x][y-1] else 1 
    arr[x-1][y-1] = 0 if arr[x-1][y-1] else 1 
    arr[x+1][y] = 0 if arr[x+1][y] else 1 
    arr[x][y+1] = 0 if arr[x][y+1] else 1 
    arr[x+1][y+1] = 0 if arr[x+1][y+1] else 1
    
def main():
    N,M =list(map( int,input().split()))
    arr = [[1 for i in range(N+2)] for j in range(M+3)]
    ans = 0
    
    for y in range(1,N+1):
        for x in range(1,M+1):
                arr[x][y] = 0 if arr[x][y] else 1 
                arr[x-1][y] = 0 if arr[x-1][y] else 1 
                arr[x][y-1] = 0 if arr[x][y-1] else 1 
                arr[x-1][y-1] = 0 if arr[x-1][y-1] else 1 
                arr[x+1][y] = 0 if arr[x+1][y] else 1 
                arr[x][y+1] = 0 if arr[x][y+1] else 1 
                arr[x+1][y+1] = 0 if arr[x+1][y+1] else 1
                arr[x+1][y-1] = 0 if arr[x+1][y-1] else 1
                arr[x-1][y+1] = 0 if arr[x-1][y+1] else 1
                
    
    for y in range(1,N+1):
        for x in range(1,M+1):
            if arr[x][y] == 0:
                ans = ans + 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    N, M = list(map(int, (input().split(' '))))
    ura = 0
    if N == 1 and M != 1:
        arr = [0 for i in range(N)]
        ura = M - 2
    elif N != 1 and M == 1:
        arr = [0 for i in range(M)]
        ura = N - 2
    elif N == 1 and M == 1:
        ura = 1
    else:
        arr = [[0 for i in range(M)] for j in range(N)]
        for i in range(N):
            for j in range(M):
                for buf_i in range(-1, 2):
                    for buf_j in range(-1, 2):
                        if i-buf_i < 0 or i+buf_i >= N or j-buf_j < 0 or j+buf_j >= M:
                            continue
                        if arr[i+buf_i][j+buf_j] == 0:
                            arr[i+buf_i][j+buf_j] = 1
                        else:
                            arr[i+buf_i][j+buf_j] = 0
        for i in range(N):
            for j in range(M):
                if arr[i][j] == 1:
                    ura += 1
    print(ura)

Submission status: Time Limit Exceeded

Solution code:
inp = input().split()
n = int(inp[0])
m = int(inp[1])
field = [[0 for i in range(m+2)] for j in range(n+2)]
virtual = [[0 for i in range(m+2)] for j in range(n+2)]

for i in range(1,n+1):
    for j in range(1,m+1):
        virtual[i-1][j-1] += 1
        virtual[i-1][j] += 1
        virtual[i-1][j+1] += 1
        virtual[i][j-1] += 1
        virtual[i][j] += 1
        virtual[i][j+1] += 1
        virtual[i+1][j-1] += 1
        virtual[i+1][j] += 1
        virtual[i+1][j+1] += 1
cnt = 0
for i in range(1,n+1):
    for j in range(1,m+1):
        if virtual[i][j]%2  == 1:
            cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:

N,M = map(int, input().split(' '))
num = 0
for n in range(N):
    if (n == 0 and n+1 == N) or (n > 0 and n < N-1):
        if M < 2:
            num += 1
        else:
            num += M - 2 
print(num)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
l = [[-1 for j in range(m)] for i in range(n)]
for i in range(n):
    for j in range(m):
        l[i][j] *= -1
        if (j-1) >= 0: l[i][j-1] *= -1
        if (j+1) < m: l[i][j+1] *= -1
        if (i-1) >= 0:
            l[i-1][j] *= -1
            if (j-1) >= 0: l[i-1][j-1] *= -1
            if (j+1) < m: l[i-1][j+1] *= -1
        if (i+1) < n:
            l[i+1][j] *= -1
            if (j-1) >= 0: l[i+1][j-1] *= -1
            if (j+1) < m: l[i+1][j+1] *= -1
ans = 0
for i in range(n):
    ans += l[i].count(1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
n, m = list(map(int, input().split()))
count = 0
for i in range(n):
    for j in range(m):
        around = 0
        for w in range(-1, 2):
            for h in range(-1, 2):
                if i+w >= 0 and i+w < n and j+h >= 0 and j+h < m:
                    around += 1
        if around % 2 == 1:
            count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int, input().split(' '))
mass = [[True for i in range(M)] for j in range(N)]
for n in range(N):
    for m in range(M):
        if n > 0:
            if m > 0:
                mass[n-1][m-1] = not mass[n-1][m-1]
            mass[n-1][m] = not mass[n-1][m]
            if m < M-1:
                mass[n-1][m+1] = not mass[n-1][m+1]
        if n < N-1:
            if m > 0:
                mass[n+1][m-1] = not mass[n+1][m-1]
            mass[n+1][m] = not mass[n+1][m]
            if m < M-1:
                mass[n+1][m+1] = not mass[n+1][m+1]
        if m > 0:
            mass[n][m-1] = not mass[n][m-1]
        mass[n][m] = not mass[n][m]
        if m < M-1:
            mass[n][m+1] = not mass[n][m+1]
num = 0
for n in range(N):
    for m in range(M):
        if not mass[n][m]:
            num += 1
print(num)

Submission status: Time Limit Exceeded

Solution code:
f = lambda :[int(i) for i in input().split()]

ans = 0
N,M = f()

stat = [[0 for i in range(M)] for j in range(N)]

for h in range(N):
    for k in range(M):
        for ypos in range(-1,2):
            for xpos in range(-1,2):
                if 0 <= h+ypos < N and 0 <= k +xpos < M:
                    stat[h+ypos][k+xpos] += 1

for a in stat:
    for b in a:
        if b % 2 == 1:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys

N, M = list(map(int, input().split()))

if N == M == 1:
    print(1)
    sys.exit()
elif N == 1 or M == 1:
    print(max(max(N, M)-2, 0))
    sys.exit()

N = max(0, N-2)
M = max(0, M-2)

print(N*M)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
print(abs(n-2)*abs(m-2))