Problem:
 Score : 400 points    Problem Statement There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i -th row and the j -th column will be denoted as Cell (i, j) .  In Cell (i, j) , a_{ij} coins are placed.  You can perform the following operation any number of times:  Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.  Maximize the number of cells containing an even number of coins.      Constraints  All values in input are integers.  1 \leq H, W \leq 500  0 \leq a_{ij} \leq 9         Input Input is given from Standard Input in the following format:  H  W  a_{11}  a_{12}  ...  a_{1W}  a_{21}  a_{22}  ...  a_{2W}  :  a_{H1}  a_{H2}  ...  a_{HW}       Output Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:  N  y_1  x_1  y_1'  x_1'  y_2  x_2  y_2'  x_2'  :  y_N  x_N  y_N'  x_N'   That is, in the first line, print an integer N between 0 and H \times W (inclusive), representing the number of operations.  In the (i+1) -th line ( 1 \leq i \leq N ), print four integers y_i, x_i, y_i' and x_i' ( 1 \leq y_i, y_i' \leq H and 1 \leq x_i, x_i' \leq W ), representing the i -th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i') .  Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer .        Sample Input 1 2 3
1 2 3
0 1 1      Sample Output 1 3
2 2 2 3
1 1 1 2
1 3 1 2  Every cell contains an even number of coins after the following sequence of operations:   Move the coin in Cell (2, 2) to Cell (2, 3) .  Move the coin in Cell (1, 1) to Cell (1, 2) .  Move one of the coins in Cell (1, 3) to Cell (1, 2) .        Sample Input 2 3 2
1 0
2 1
1 0      Sample Output 2 3
1 1 1 2
1 2 2 2
3 1 3 2       Sample Input 3 1 5
9 9 9 9 9      Sample Output 3 2
1 1 1 2
1 3 1 4

Solution (Python):
h,w=map(int,input().split())
l=[]
for i in range(h):
    l.append(list(map(int,input().split())))

ans=[]
for i in range(h):
    for j in range(w):
        i2,j2=i,j
        if i2%2: j2=w-(j+1)

        o,p=i2,j2
        if l[i2][j2]%2:
            if i2%2==0 and j2<w-1: p+=1
            elif i2%2==0 and i2<h-1: o+=1
            elif i2%2 and j2>0: p-=1
            elif i2%2 and i2<h-1: o+=1
            if i2!=o or j2!=p:
                l[i2][j2]-=1
                l[o][p]+=1
                ans.append((i2+1,j2+1,o+1,p+1))

print(len(ans))
for i in range(len(ans)):
    print(*ans[i])

Solution (Python):
h, w = map(int, input().split())
T = [[] for i in range(h)]
S = [[0] * w for i in range(h)]
for i in range(h):
    a = list(map(int, input().split()))
    T[i] = a

for i in range(h):
    for j in range(w):
        T[i][j] = T[i][j] % 2

move = []
i = 0
j = 0
k = 0
flag = False
while True:
    px, py = i, j
    if T[i][j] == 1:
        flag = True
    if i % 2 == 0:
        j += 1
        if j == w:
            j -= 1
            i += 1
        if i == h:
            break
        if flag:
            move.append((px, py, i, j))
        if flag and T[i][j] == 1:
            T[i][j] = 0
            flag = False
        k += 1
    else:
        j -= 1
        if j == -1:
            j += 1
            i += 1
        if i == h:
            break
        if flag:
            move.append((px, py, i, j))
        if flag and T[i][j] == 1:
            T[i][j] = 0
            flag = False

print(len(move))
for i in range(len(move)):
    x, y, w, z = move[i][0] + 1, move[i][1] + 1, move[i][2] + 1, move[i][3] + 1
    print(x, y, w, z)

Solution (Python):
h, w = map(int, input().split())
a = [list(map(int, input().split())) for i in range(h)]
ans = 0
ansl = []
for i in range(h):
    for j in range(w):
        if a[i][j] % 2 == 1 and a[i][j] >= 1:
            if j + 1 < w:
                a[i][j + 1] += 1
                a[i][j] -= 1
                ans += 1
                ansl.append([i + 1, j + 1, i + 1, j + 2])
for k in range(h):
    if a[k][-1] % 2 == 1:
        if k + 1 < h:
            a[k + 1][-1] += 1
            a[k][-1] -= 1
            ans += 1
            ansl.append([k + 1, w, k + 2, w])
print(ans)
for i in ansl:
    i = list(map(str, i))
    print(" ".join(i))

Solution (Python):
def solve():
  h, w = map(int, input().split())
  table = []
  for _ in range(h):
    table.append(list(map(int, input().split())))
  
  
  count = 0
  result = []

  for i in range(h):
    j = 0
    while j < w:
      if table[i][j] % 2 != 0:
        s = j
        j += 1
        while j < w:
          if table[i][j] % 2 != 0 or j == w - 1:
            table[i][s] -=1
            table[i][j] += 1
            for k in range(s, j):
              count += 1
              result.append(((i, k), (i, k + 1)))
            break
          j += 1
      j += 1
  i = 0
  while i < h:
    if table[i][-1] % 2 != 0:
      s = i
      i += 1
      while i < h:
        if table[i][-1] % 2 != 0 or i == h - 1:
          for k in range(s, i):
            count += 1
            result.append(((k, w-1), (k+1, w-1)))
          break
        i += 1
    i += 1
  print(count)
  for e in result:
    print(e[0][0] + 1, e[0][1] + 1, e[1][0] + 1, e[1][1]+ 1)



solve()

Solution (Python):
(H, W) = map(int, input().split())
a = []
for i in range(H):
    a.append(list(map(int, input().split())))



odd_flag = []
for i in range(len(a)):
    odd_flag.append(list())
    for j in range(len(a[i])):
        if a[i][j]%2 != 0:
            odd_flag[i].append(True)
        else:
            odd_flag[i].append(False)



odd_sum = 0
for i in range(len(odd_flag)):
    odd_sum += odd_flag[i].count(True)


def move(h, w):
    if h%2 == 0:
        if w == W-1:
            return (h+1, w)
        else:
            return (h, w+1)
    else:
        if w == 0:
            return (h+1, w)
        else:
            return (h, w-1)

s_h = 0
s_w = 0
N = 0
ope_list = []
move_flag = False
while odd_sum >= 2:
    
    if move_flag is True and odd_flag[s_h][s_w] is True:
        move_flag = False
        (s_h, s_w) = move(s_h, s_w)
        odd_sum -= 2
    
    elif move_flag is True and odd_flag[s_h][s_w] is False:
        ope = ""
        ope += str(s_h+1) + " " + str(s_w+1) + " "
        (s_h, s_w) = move(s_h, s_w)
        ope += str(s_h+1) + " " + str(s_w+1) + "\n"
        ope_list.append(ope)
        N += 1
    
    elif move_flag is False and odd_flag[s_h][s_w] is False:
        (s_h, s_w) = move(s_h, s_w)
    
    elif move_flag is False and odd_flag[s_h][s_w] is True:
        move_flag = True
        ope = ""
        ope += str(s_h+1) + " " + str(s_w+1) + " "
        (s_h, s_w) = move(s_h, s_w)
        ope += str(s_h+1) + " " + str(s_w+1) + "\n"
        ope_list.append(ope)
        N += 1

print(N)
for i in range(len(ope_list)):
    print(ope_list[i], end="")
    print("\n")

Solution (Python):
(H, W) = map(int, input().split())
a = []
for i in range(H):
    a.append(list(map(int, input().split())))



odd_flag = []
for i in range(len(a)):
    odd_flag.append(list())
    for j in range(len(a[i])):
        if a[i][j]%2 != 0:
            odd_flag[i].append(True)
        else:
            odd_flag[i].append(False)



odd_sum = 0
for i in range(len(odd_flag)):
    odd_sum += odd_flag[i].count(True)


def move(h, w):
    if h%2 == 0:
        if w == W-1:
            return (h+1, w)
        else:
            return (h, w+1)
    else:
        if w == 0:
            return (h+1, w)
        else:
            return (h, w-1)

s_h = 0
s_w = 0
N = 0
ope_list = []
move_flag = False
while odd_sum >= 2:
    
    if move_flag is True and odd_flag[s_h][s_w] is True:
        move_flag = False
        (s_h, s_w) = move(s_h, s_w)
        odd_sum -= 2
    
    elif move_flag is True and odd_flag[s_h][s_w] is False:
        ope = ""
        ope += str(s_h+1) + " " + str(s_w+1) + " "
        (s_h, s_w) = move(s_h, s_w)
        ope += str(s_h+1) + " " + str(s_w+1) + "\n"
        ope_list.append(ope)
        N += 1
    
    elif move_flag is False and odd_flag[s_h][s_w] is False:
        (s_h, s_w) = move(s_h, s_w)
    
    elif move_flag is False and odd_flag[s_h][s_w] is True:
        move_flag = True
        ope = ""
        ope += str(s_h+1) + " " + str(s_w+1) + " "
        (s_h, s_w) = move(s_h, s_w)
        ope += str(s_h+1) + " " + str(s_w+1) + "\n"
        ope_list.append(ope)
        N += 1

print(N)
for i in range(len(ope_list)):
    print(ope_list[i], end="")

Solution (Python):
na = lambda: list(map(int, input().split()))
h, w = na()
field = []
for i in range(h):
    field.append(list(map(int, input().split())))

ans = [] 
for i in range(h):
    for j in range(w-1):
        if field[i][j] % 2 == 1:
            ans.append([i+1, j+1, i+1, j+2])
            field[i][j] += 1
            field[i][j+1] -= 1
for i in range(h-1):
    if field[i][w-1] % 2 == 1:
        ans.append([i+1, w, i+2, w])
        field[i][-1] += 1
        field[i+1][-1] -= 1

print(len(ans))
for l in ans:
    print(" ".join(map(str, l)))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

def fprint(a,b,c,d):
	s = str(a) + " " + str(b) + " " + str(c) + " " + str(d)
	return s

def main():
	h, w = map(int, input().split())
	n = 0
	a = [[int(i) for i in input().split()] for _ in range(h)]
	ans = []
	for i in range(h):
		if i % 2 == 0:
			for j in range(w):
				if a[i][j] % 2 != 0 and j != w-1:
					ans+=[fprint(i+1,j+1,i+1,j+2)]
					a[i][j+1] += 1
					n+=1
				elif a[i][j] % 2 != 0 and i != h-1:
					ans += [fprint(i+1,j+1,i+2,j+1)]
					a[i+1][j] += 1
					n+=1
		else:
			for j in range(w-1,-1,-1):
				if a[i][j] % 2 != 0 and j != 0:
					ans += [fprint(i+1,j+1,i+1,j)]
					a[i][j-1] += 1
					n+=1
				elif a[i][j] % 2 != 0 and i != h-1:
					ans += [fprint(i+1,j+1, i+2,j+1)]
					a[i+1][j] += 1
					n+=1
	print(n)
	print("\n".join(ans))
	return
	
main()

Solution (Python):
(H, W) = map(int, input().split())
a = []
for i in range(H):
    a.append(list(map(int, input().split())))



odd_flag = []
for i in range(len(a)):
    odd_flag.append(list())
    for j in range(len(a[i])):
        if a[i][j]%2 != 0:
            odd_flag[i].append(True)
        else:
            odd_flag[i].append(False)



odd_sum = 0
for i in range(len(odd_flag)):
    odd_sum += odd_flag[i].count(True)


def move(h, w):
    if h%2 == 0:
        if w == W-1:
            return (h+1, w)
        else:
            return (h, w+1)
    else:
        if w == 0:
            return (h+1, w)
        else:
            return (h, w-1)

s_h = 0
s_w = 0
N = 0
ans = ""
move_flag = False
while odd_sum >= 2:
    
    if move_flag is True and odd_flag[s_h][s_w] is True:
        move_flag = False
        (s_h, s_w) = move(s_h, s_w)
        odd_sum -= 2
    
    elif move_flag is True and odd_flag[s_h][s_w] is False:
        if len(ans) > 0:
            ans += "\n"
        ans += str(s_h+1) + " " + str(s_w+1) + " "
        (s_h, s_w) = move(s_h, s_w)
        ans += str(s_h+1) + " " + str(s_w+1)
        N += 1
    
    elif move_flag is False and odd_flag[s_h][s_w] is False:
        (s_h, s_w) = move(s_h, s_w)
    
    elif move_flag is False and odd_flag[s_h][s_w] is True:
        move_flag = True
        if len(ans) > 0:
            ans += "\n"
        ans += str(s_h+1) + " " + str(s_w+1) + " "
        (s_h, s_w) = move(s_h, s_w)
        ans += str(s_h+1) + " " + str(s_w+1)
        N += 1

print(N)
print(ans)

Solution (Python):
def solve(H, W, field):
    ret = []
    for y in range(0, H):
        for x in range(0, W):
            if field[y][x] % 2 == 1:
                for (xi, yi) in [(x + 1, y), (x, y + 1)]:
                    if 0 <= xi < W and 0 <= yi < H:
                            ret.append(" ".join([str(y + 1), str(x + 1), str(yi + 1), str(xi + 1)]))
                            field[y][x] -= 1
                            field[yi][xi] += 1
                            break
    return [len(ret)] + ret


if __name__ == "__main__":
    l = input()
    H, W = int(l.split()[0]), int(l.split()[1])
    field = [[int(c) for c in input().split(" ")] for _ in range(0, H)]
    [print(l) for l in solve(H, W, field)]

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(H, W, A):
    ans = []
    X = []
    for i in range(H):
        for j in range(W) if i % 2 == 0 else range(W-1, -1, -1):
            X.append((i, j))

    for i in range(len(X)-1):
        x, y = X[i]
        if A[x][y] % 2 == 0:
            continue
        A[x][y] -= 1

        x1, y1 = X[i+1]
        A[x1][y1] += 1
        ans.append((x, y, x1, y1))

    for r in A:
        error_print(r)
    print(len(ans))
    for a, b, c, d in ans:
        print(a+1, b+1, c+1, d+1)


def main():
    H, W = read_int_n()
    A = [read_int_n() for _ in range(H)]
    slv(H, W, A)


if __name__ == '__main__':
    main()

Solution (Python):
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]

parity = [[i % 2 for i in row] for row in A]

odd_list = []
for i in range(H):
    for j in range(W):
        if parity[i][j] == 1:
            odd_list.append([i, j])

used = [[0 for _ in range(W)] for _ in range(H)]

ll = []
for h in range(H):
    for w in range(W):
        if h % 2 == 1:
            w = W - w - 1
        ll.append([h, w])

output = []
for i, c in enumerate(ll[:-1]):
    h = c[0]
    w = c[1]
    if parity[h][w] == 1:
        output.append([h+1, w+1, ll[i+1][0]+1, ll[i+1][1]+1])
        parity[ll[i+1][0]][ll[i+1][1]] = (parity[ll[i+1][0]][ll[i+1][1]] + 1) % 2

print(len(output))
for r in output:
    r = [str(i) for i in r]
    print(" ".join(r))

Solution (Python):
def print_map(li):
    for i in range(len(li)):
        print(" ".join([str(a) for a in li[i]]))
    

H,W = map(int, input().split(" "))
mapp = []
for i in range(H):
    temp = list(map(int, input().split(' ')))
    mapp.append(temp)

memo = []
for i in range(H):
    temp = [n%2 for n in mapp[i]]
    memo.append(temp)
num_odd = 0
for i in range(H):
    temp = sum(memo[i])
    num_odd = num_odd + temp





c=[1,1]
bc = [1,1]
ans = []
def pli(li):
    return(str(li[0])+" "+str(li[1]))
flag = 0
ans = []
while(1):
    if flag % 2 != 0 and flag != num_odd:
        ans.append(pli(bc)+" "+pli(c))
    if memo[c[0]-1][c[1]-1] == 1:
        flag = flag + 1
    bc[0] = c[0]
    bc[1] = c[1]
    if H % 2 == 0 and c[0] == H and c[1] == 1:
        break
    elif H % 2 != 0 and c[0] == H and c[1] == W:
        break
    elif c[0] % 2 != 0 and c[1] != W:
        c[1] = c[1] + 1
    elif c[0] % 2 != 0 and c[1] == W:
        c[0] = c[0] + 1
    elif c[0] % 2 == 0 and c[1] != 1:
        c[1] = c[1] - 1
    elif c[0] % 2 == 0 and c[1] == 1:
        c[0] = c[0] + 1
print(len(ans))
for l in ans:
    print(l)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v



def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk



def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result




H, W = None, None

def wfs(y, x, desty, destx, R):
    R[y][x] = 1
    
    nextL = [[y, x]]
        
    t = 2
    while len(nextL) != 0:
        for nl in nextL:
            cy, cx = nl
            new_nextL = []
            for dy, dx in zip([-1, 0, 0, 1], [0, 1, -1, 0]):
                    if 0 <= cy + dy < H and 0 <= cx + dx < W and R[cy + dy][cx + dx] == 0:
                        new_nextL.append([cy + dy, cx + dx])
                        R[cy + dy][cx + dx] = t

                        if cy + dy == desty and cx + dx == destx:
                            return R
        
        t += 1
        
        nextL = new_nextL


def main():
    global H
    global W

    H, W = li_input()
    A = [li_input() for _ in range(H)]

    ansL = []

    for i in range(H):
        if i % 2 == 0:
            rng_obj = range(0, W-1)
            is_right = True
        else:
            rng_obj = range(W - 1, 0, -1)
            is_right = False
        
        for j in rng_obj:
            if A[i][j] % 2 == 1:
                if is_right:
                    A[i][j] -= 1
                    A[i][j+1] += 1
                    ansL.append([i, j, i, j + 1])
                else:
                    A[i][j] -= 1
                    A[i][j-1] += 1
                    ansL.append([i, j, i, j - 1])
        
        if i != H - 1:
            if is_right:
                if A[i][-1] % 2 == 1:
                    A[i][-1] -= 1
                    A[i + 1][-1] += 1
                    ansL.append([i, W - 1, i + 1, W - 1])
            else:
                if A[i][0] % 2 == 1:
                    A[i][0] -= 1
                    A[i + 1][0] += 1
                    ansL.append([i, 0, i + 1, 0])
    
    print(len(ansL))

    for l in ansL:
        a, b, c, d = l
        print(a+1, b+1, c+1, d+1)
                
    
                

main()

Solution (Python):
H, W = map(int, input().split())
a = []
for i in range(H):
    a.append(list(map(int, input().split())))
count = 0
result = []
for i in range(H):
    j = 0
    while j < W:
        if a[i][j] % 2 != 0:
            s = j
            j += 1
            while j < W:
                if a[i][j] % 2 != 0 or j == W - 1:
                    a[i][s] -= 1
                    a[i][j] += 1
                    for k in range(s, j):
                        count += 1
                        result.append(((i, k), (i, k + 1)))
                    break
                j += 1
        j += 1

i = 0
while i < H:
    if a[i][-1] % 2 != 0:
        s = i
        i += 1
        while i < H:
            if a[i][-1] % 2 != 0 or i == H - 1:
                for k in range(s, i):
                    count += 1
                    result.append(((k, W - 1), (k + 1, W - 1)))
                break
            i += 1
    i += 1


print(count)
for e in result:
    print(e[0][0] + 1, e[0][1] + 1, e[1][0] + 1, e[1][1] + 1)

Solution (Python):
def main():
    """
    1 <= H,W <= 500
    0 <= a(i,j) <= 9
    """
    H, W = map(int, input().split())
    a = [
        list(map(int, input().split()))
        for _ in range(H)
    ]

    ans = editorial(H, W, a)

    print(len(ans))
    for y1, x1, x, y in ans:
        print(y1+1, x1+1, x+1, y+1)


def editorial(H, W, a):
    op = []
    for i in range(H):
        for j in range(W-1):
            if a[i][j] % 2 == 1:
                a[i][j] -= 1
                a[i][j+1] -= 1
                op.append((i, j, i, j+1))

    for i in range(H-1):
        if a[i][W-1] % 2 == 1:
            a[i][W-1] -= 1
            a[i+1][W-1] -= 1
            op.append((i, W-1, i+1, W-1))

    return op


if __name__ == '__main__':
    main()

Solution (Python):
import sys
H, W = [int(i) for i in input().split(' ')]


cou = 0
one_flag = False
result = []
sub_result = []
for line in sys.stdin:
    cou += 1
    xs = [1 if int(item) % 2 == 1 else 0 for item in line.strip().split(' ')]
    if cou % 2 == 0:
        xs = xs[::-1]
    for i, x in enumerate(xs):
        if one_flag:
            if i != 0:
                if cou % 2 == 0:
                    sub_result.append((cou, W-i+1, cou, W-i))
                else:
                    sub_result.append((cou, i, cou, i+1))
            else:
                if cou % 2 == 0:
                    sub_result.append((cou-1, W, cou, W))
                else:
                    sub_result.append((cou-1, 1, cou, 1))
        if x == 1:
            one_flag = not one_flag
            result.append(sub_result)
            sub_result = []
N = 0
for r in result:
    N += len(r)

print(N)
for r in result:
    for x in r:
        print(" ".join(map(str,x)))

Solution (Python):
H, W = map(int, input().split())		

M=[]
A=[0]*(W+1)
M.append(A)

for i in range(H):
	A=list( map(int, input().split()) )
	A=list(map(lambda x: x % 2, A))
	A=[0]+A
	M.append(A)



ANS=[]
A=[]
x=0
i=0
for h in range(1,H+1):
	x=0
	i=0
	for w in range(1,W+1):
		ans = "{} {} {} {}".format(h, w-1, h, w)
		i = M[h][w]

		
		
			
		if (x==1)and(i==0):

			A.append(ans)
			x=M[h][w-1]=0
			i=M[h][w]=1
		
		elif (x==1)and(i==1):

			A.append(ans)
			for a in A:
				ANS.append(a)
			A=[]
			x=M[h][w-1]=0
			i=M[h][w]=0
			
		x = i





w=W
x=0
i=0
for h in range(1, H+1):
	ans = "{} {} {} {}".format(h-1, w, h, w)
	i=M[h][w]

	if (x==1)and(i==0):
		A.append(ans)
		x=M[h-1][w]=0
		i=M[h][w]=1
	
	elif (x==1)and(i==1):
		A.append(ans)
		for a in A:
			ANS.append(a)
		A=[]
		x=M[h-1][w]=0
		i=M[h][w]=0
	x = i





print(len(ANS))
for a in ANS:
	print(a)

Solution (Python):
from collections import deque

H, W = map(int, input().split())
A = []
for _ in range(H):
    temp = list(map(int, input().split()))
    a = [t % 2 for t in temp]
    A.append(a)

operations = []

for i in range(H):
    y = i + 1
    if i % 2 == 0:
        for j in range(W):
            x = j + 1
            if A[i][j] == 1:
                if x != W:
                    operations.append((y, x, y, x+1))
                    A[i][j+1] = (A[i][j+1] + 1) % 2
                elif y != H:
                    operations.append((y, x, y+1, x))
                    A[i+1][j] = (A[i+1][j] + 1) % 2
    else:
        for j in reversed(range(W)):
            x = j + 1
            if A[i][j] == 1:
                if x != 1:
                    operations.append((y, x, y, x-1))
                    A[i][j-1] = (A[i][j-1] + 1) % 2
                elif y != H:
                    operations.append((y, x, y+1, x))
                    A[i+1][j] = (A[i+1][j] + 1) % 2

N = len(operations)
print(N)
if N != 0:
    for op in operations:
        print(*op, sep=' ')

Solution (Python):
H,W = map(int, input().split())
a = []
for h in range(H):
  a.append(list(map(int, input().split())))
ans = []
for h in range(H):
  for w in range(W-1):
    if a[h][w]&1:
      a[h][w  ] -= 1
      a[h][w+1] += 1
      ans.append([h+1,w+1,h+1,w+2])
for h in range(H-1):
  if a[h][W-1]&1:
    a[h  ][W-1] -= 1
    a[h+1][W-1] += 1
    ans.append([h+1,W,h+2,W])
print(len(ans))
for s in ans:
  print("{} {} {} {}".format(*s))

Solution (Python):
import sys
fin = sys.stdin.readline
























































H, W = [int(elem) for elem in input().split(' ')]
a_list = [[int(elem) for elem in fin().split(' ')] for _ in range(H)]
assert len(a_list) == H
assert len(a_list[0]) == W

strategy = []
used = [[False] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if a_list[i][j] % 2 == 1 and (not used[i][j]):
            a_list[i][j] -= 1
            if j < W - 1:
                a_list[i][j+1] += 1
                strategy.append((i+1, j+1, i+1, j+2))
                used[i][j] = True
            elif i < H - 1:
                a_list[i+1][j] += 1
                strategy.append((i+1, j+1, i+2, j+1))
                used[i][j] = True
            else:
                continue

print(len(strategy))
for tup in strategy:
    print(' '.join([str(elem) for elem in tup]))

Solution (Python):
def myfun(c):
	return c[0];

h,w = map(int,input().split())

arr = [];
ans = [];

for i in range(h):

	temp = list(map(int,input().split()))
	arr.append(temp)

count=0;
for i in range(h):
	for j in range(w):

		if arr[i][j]%2==1:
			if j < w-1:
				count+=1
				ans.append( [i+1,j+1,i+1,j+2])
				arr[i][j+1]+=1;
				arr[i][j]-=1;
			elif i < h-1:
				count+=1;
				ans.append( [i+1,j+1,i+2,j+1])
				arr[i][j]-=1;
				arr[i+1][j]+=1;

print(count)
for i in range(count):
	print(*ans[i]);

Solution (Python):
h, w = [int(i) for i in input().split()]
a = [[int(i) for i in input().split()] for j in range(h)]
count, ans = [0,""]
for i,ai in enumerate(a):
    for j,aij in enumerate(ai):
        if aij % 2 != 0:
            if j != w-1:
                count += 1
                a[i][j+1] += 1
                ans += "{} {} {} {}\n".format(i+1,j+1,i+1,j+2)
            elif i != h-1:
                count += 1
                a[i+1][j] += 1
                ans += "{} {} {} {}\n".format(i+1,j+1,i+2,j+1)
print(count)
print(ans)

Solution (Python):



h,w= list(map(int,input().split()))	
B = []
for _ in range(h):
    B.append([int(i) for i in input().split()])

def hh(j):
    return j//w

def ww(j):
    if j//w%2==0:
        return j%w
    else:
        return w-1-j%w

ans = []
for i in range(h*w-1):

    if B[hh(i)][ww(i)]%2 ==1:
        B[hh(i)][ww(i)]-=1
        B[hh(i+1)][ww(i+1)]+=1
        ans.append([hh(i)+1,ww(i)+1,hh(i+1)+1,ww(i+1)+1])

print(len(ans))
for a in ans:



    print(*a)

Solution (Python):
DEBUG=0

H, W = map(int, input().split())
coins = []
odds = []

for y in range(H):
    line = list(map(int, input().split()))
    coins.append(line)
    tmp = [x for x in range(W) if line[x] % 2 == 1]
    odds.append(tmp)

if DEBUG: print(odds)

hands = []
prev = None

for y, o in enumerate(odds):
    if y % 2 == 0:
        if prev is not None:
            for x in range(prev, 0, -1):
                hands.append((x, y-1, x-1, y-1))
            hands.append((0, y-1, 0, y))
            for x in range(o[0]):
                hands.append((x, y, x+1, y))
            del o[0]
        n = len(o)
        for i in range(n//2):
            for x in range(o[2*i], o[2*i+1]):
                hands.append((x, y, x+1, y))
        if(n%2==1):
            prev = o[-1]
        else:
            prev = None
    else:
        o = o[::-1]
        if prev is not None:
            for x in range(prev, W-1):
                hands.append((x, y-1, x+1, y-1))
            hands.append((W-1, y-1, W-1, y))
            for x in range(W-1, o[0], -1):
                hands.append((x, y, x-1, y))
            del o[0]
        n = len(o)
        for i in range(n//2):
            for x in range(o[2*i], o[2*i+1], -1):
                hands.append((x, y, x-1, y))
        if(n%2==1):
            prev = o[-1]
        else:
            prev = None

print(len(hands))
for h in hands:
    x, y, x_, y_ = [_ + 1 for _ in map(int, h)]
    print(y, x, y_, x_)

Solution (Python):
def iin(): return int(input())
def nl(): return list(map(int, input().split()))

h, w = nl()
a = [nl() for _ in range(h)]

ans = []
flg = False
for y in range(h):
    for x in range(w):
        if flg:
            ans.append([y + 1, x - 1 + 1, y + 1, x + 1])
            if a[y][x] % 2 == 1:
                flg = False
        else:
            if a[y][x] % 2 == 1:
                flg = True
    if flg:
        if y + 1 < h:
            ans.append([y + 1, w - 1 + 1, y + 1 + 1, w - 1 + 1])
            a[y + 1][w - 1] += 1
        flg = False
    
print(len(ans))
for x in ans:
    print(*x)

Solution (Python):
def is_even(num):
    if num == 0:
        return True
    return num % 2 == 0


height, width = list(map(int, input().split()))
coin_table = [list(map(is_even, map(int, input().split()))) for i in range(height)]

move_list = list()

for h in range(height):
    for w in range(width):
        if not(coin_table[h][w]):
            coin_table[h][w] = True
            if w == width-1:
                if h == height-1:
                    break
                coin_table[h + 1][w] = not coin_table[h + 1][w]
                move_list.append([h + 1, w + 1, h + 2, w + 1])
            else:
                coin_table[h][w+1] = not coin_table[h][w+1]
                move_list.append([h + 1, w + 1, h + 1, w + 2])

print(len(move_list))
for i in range(len(move_list)):
    print("{0} {1} {2} {3}".format(move_list[i][0], move_list[i][1], move_list[i][2], move_list[i][3]))

Solution (Python):



h,w= list(map(int,input().split()))	
B = []
for _ in range(h):
    B.append([int(i) for i in input().split()])

def hh(j):
    return j//w

def ww(j):
    if j//w%2==0:
        return j%w
    else:
        return w-1-j%w

ans = []
for i in range(h*w-1):

    if B[hh(i)][ww(i)]%2 ==1:
        B[hh(i)][ww(i)]-=1
        B[hh(i+1)][ww(i+1)]+=1
        ans.append([hh(i)+1,ww(i)+1,hh(i+1)+1,ww(i+1)+1])

print(len(ans))
for a in ans:



    print(a[0],a[1],a[2],a[3])