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())

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

cnt = 0
path = []

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

for h in range(H-1):
    if A[h][-1]%2:
        A[h+1][-1] += 1
        cnt += 1
        path.append((h+1, W, h+2, W))


print(cnt)
for p in path:
    print(*p)

Solution (Python):
h,w = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(h)]
out = []
for i in range(h):
    for j in range(w):
        if A[i][j]&1:
            if j == w-1:
                if i != h-1:
                    A[i+1][j] += 1
                    A[i][j] -= 1
                    out.append((i+1,j+1,i+2,j+1))
            else:
                A[i][j+1] += 1
                A[i][j] -= 1
                out.append((i+1,j+1,i+1,j+2))
print(len(out))
for i,j,k,l in out:
    print(i,j,k,l)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



H, W = MAP()
a = [LIST() for _ in range(H)]

cnt = 0
move = []
for i in range(H):
	for j in range(W):
		if i == H-1 and j == W-1:
			print(cnt)
			for x in move:
				print(*x)
			exit()
		if i%2 == 0:
			if a[i][j]%2 and j == W-1:
				a[i][j] -= 1
				a[i+1][j] += 1	
				cnt += 1
				move.append((i+1, j+1, i+2, j+1))
			elif a[i][j]%2:
				a[i][j] -= 1
				a[i][j+1] += 1
				cnt += 1
				move.append((i+1, j+1, i+1, j+2))
		else:
			if a[i][W-j-1]%2 and j == W-1:
				a[i][W-j-1] -= 1
				a[i+1][W-j-1] += 1
				cnt += 1
				move.append((i+1, W-j, i+2, W-j))
			elif a[i][W-j-1]%2:
				a[i][W-j-1] -= 1
				a[i][W-j-2] += 1
				cnt += 1
				move.append((i+1, W-j, i+1, W-j-1))

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



H, W = MAP()
a = [LIST() for _ in range(H)]

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

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

print(len(op))
print(*[" ".join(map(str, x)) for x in op], sep = "\n")

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):
        if h==H-1 and w==W-1:break
        if a[h][w]%2==1:
            if w==W-1:
                ans.append([h+1,w+1,h+2,w+1])
                a[h][w]-=1
                a[h+1][w]+=1
            else:
                ans.append([h+1,w+1,h+1,w+2])
                a[h][w]-=1
                a[h][w+1]+=1
L=len(ans)
print(L)

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



H, W = MAP()

a = [LIST() for _ in range(H)]
cnt = 0

ans = []
for y in range(H):
	x = 0
	while x < W:
		if x < W-1 and a[y][x]%2:
			ans.append((y+1, x+1, y+1, x+2))
			cnt += 1
			a[y][x] -= 1
			a[y][x+1] += 1
		x += 1

y = 0
while y < H-1 :
	if a[y][-1]%2:
		ans.append((y+1, W, y+2, W))
		cnt += 1
		a[y][-1] -= 1
		a[y+1][-1] += 1
	y += 1

print(cnt)
for x in ans:
	print(*x)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def ggg(h,w):
	a=0
	b=0
	while b < h:
		if b%2==0 and a == w-1:
			na = a
			nb = b+1
		elif b %2==0:
				na = a+1
				nb = b
		elif a == 0:
			na=a
			nb = b+1
		else:
			na = a-1
			nb = b
		yield a, b, na, nb
		a=na
		b=nb


def main():
	
	h, w = tin()
	
	mp = [lin() for _ in range(h)]
	ret = []
	lg = ggg(h, w)
	oddf = False
	for a, b, na, nb in lg:
		if mp[b][a] %2 == 1:
			oddf = not oddf
		if oddf and nb < h:
			ret.append([b+1, a+1, nb+1,na+1])
	print(len(ret))
	for k in ret:
		print(*k)
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
h,w = map(int,input().split())
a = [list(map(int,input().split())) for _ in range(h)]
n = 0
ans = []
for i in range(h):
    for j in range(w):
        if a[i][j]%2==0:continue
        if j<w-1:
            a[i][j] -= 1
            a[i][j+1] += 1
            n += 1
            ans.append([i+1,j+1,i+1,j+2])
        elif i<h-1:
            a[i][j] -= 1
            a[i+1][j] += 1
            n += 1
            ans.append([i+1,j+1,i+2,j+1])
print(n)
for i in ans:
    print(*i)

Solution (Python):

import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

H, W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(H)]

def main():
    count = 0
    ans = []
    for i in range(H):
        for j in range(W-1):
            if a[i][j] & 1:
                ans.append([i+1, j+1, i+1, j+2])
                a[i][j+1] += 1
                count += 1

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

    print(count)
    [print(*a) for a in ans]

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
fact = math.factorial
sys.setrecursionlimit(10**9)
INF = 10**9
mod = 10**9+7

H,W = I()
maze = []
for i in range(H):
    a = l()
    if i%2 == 1:
        a.reverse()
    for j in range(W):
        maze.append(a[j])
ans = []
zahyo = []
for i in range(H*W):
    b = i//W+1
    c = i%W+1
    if b%2 == 0:
        c = W-c+1
    zahyo.append([b,c])
for i in range(H*W-1):
    if maze[i]%2 == 1:
        ans.append(zahyo[i]+zahyo[i+1])
        maze[i+1] += 1
print(len(ans))
for s in ans:
    print(*s)

Solution (Python):
H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j]%2==0:
            A[i][j]=0
        else:
            A[i][j]=1
cur = [1,1,1,0]
cnt = 0
B = []
while cur[0]<=H:
    i = cur[0]-1
    j = cur[1]-1
    a = A[i][j]
    d,h = cur[2],cur[3]
    if a==0:
        if d==1 and j<W-1:
            if h==1:
                B.append((i+1,j+1,i+1,j+2))
                cnt += 1
            cur = [i+1,j+2,d,h]
        elif d==1 and j==W-1:
            if h==1 and i+2<=H:
                B.append((i+1,j+1,i+2,j+1))
                cnt += 1
            cur = [i+2,j+1,1-d,h]
        elif d==0 and j>0:
            if h==1:
                B.append((i+1,j+1,i+1,j))
                cnt += 1
            cur = [i+1,j,d,h]
        elif d==0 and j==0:
            if h==1 and i+2<=H:
                B.append((i+1,j+1,i+2,j+1))
                cnt += 1
            cur = [i+2,j+1,1-d,h]
    else:
        if d==1 and j<W-1:
            if h==0:
                B.append((i+1,j+1,i+1,j+2))
                cnt += 1
            cur = [i+1,j+2,d,1-h]
        elif d==1 and j==W-1:
            if h==0 and i+2<=H:
                B.append((i+1,j+1,i+2,j+1))
                cnt += 1
            cur = [i+2,j+1,1-d,1-h]
        elif d==0 and j>0:
            if h==0:
                B.append((i+1,j+1,i+1,j))
                cnt += 1
            cur = [i+1,j,d,1-h]
        elif d==0 and j==0:
            if h==0 and i+2<=H:
                B.append((i+1,j+1,i+2,j+1))
                cnt += 1
            cur = [i+2,j+1,1-d,1-h]
print(cnt)
for i in range(cnt):
    print(*B[i])

Solution (Python):
h,w=map(int,input().split())
a=[]
for i in range(h):
  a.append(list(map(int,input().split())))
carry=0
ans=[]
for i in range(h):
  for j in range(w-1):
    if i%2==0:
      if (carry+a[i][j])%2==1:
        ans.append([i+1,j+1,i+1,j+2])
        carry=1
      else:
        carry=0
    if i%2==1:
      if (carry+a[i][w-1-j])%2==1:
        ans.append([i+1,w-j,i+1,w-1-j])
        carry=1
      else:
        carry=0
  if i%2==0 and i!=h-1:
    if (carry+a[i][w-1])%2==1:
      ans.append([i+1,w,i+2,w])
      carry=1
    else:
      carry=0
  if i%2==1 and i!=h-1:
    if (carry+a[i][0])%2==1:
      ans.append([i+1,1,i+2,1])
      carry=1
    else:
      carry=0
print(len(ans))
for b in ans:
  t=[]
  for i in b:
    t.append(str(i))
  print(' '.join(t))

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

H,W = map(int,readline().split())

a = [list(map(int,readline().split())) for i in range(H)]

ans = []

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

    else:
        for j in range(W-1):
            if a[i][j]%2:
                ans.append((i+1,j+1,i+1,j+2))
                a[i][j] -= 1
                a[i][j+1] += 1
        if a[i][W-1]%2:
            ans.append((i+1,W,i+2,W))
            a[i][W-1] -= 1
            a[i+1][W-1] += 1
if (H-1)%2:
    for j in range(W-1,0,-1):
        if a[H-1][j]%2:
            ans.append((H,j+1,H,j))
            a[H-1][j] -= 1
            a[H-1][j-1] += 1
else:
    for j in range(W-1):
        if a[H-1][j]%2:
            ans.append((H,j+1,H,j+2))
            a[H-1][j] -= 1
            a[H-1][j+1] += 1
print(len(ans))
for (x,y,u,v) in ans:
    print(x,y,u,v)

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

ans = 0
L = []
for i in range(H):
  for j in range(W):
    if A[i][j]%2 == 1:
      if j <= W-2 and A[i][j+1]%2 == 1: 
        ans +=1
        A[i][j] -=1
        A[i][j+1] +=1
        L.append([i,j,i,j+1])
      elif i <= H-2 and A[i+1][j]%2 == 1: 
        ans +=1
        A[i][j]-=1
        A[i+1][j] += 1
        L.append([i,j,i+1,j])
      else: 
        if j <=W-2: 
          ans +=1
          A[i][j] -=1
          A[i][j+1] +=1
          L.append([i,j,i,j+1])
        elif i <= H-2: 
          ans +=1
          A[i][j]-=1
          A[i+1][j] += 1
          L.append([i,j,i+1,j])
print(ans)
for t in L:
  temp = []
  for x in t:
    temp.append(x+1)
  print(*temp)

Solution (Python):
h, w = map(int, input().split())
a = []
for _ in range(h):
    a.append(list(map(int, input().split())))
ans = []
for i in range(h):
    for j in range(w-1):
        if a[i][j]%2:
            a[i][j+1] += a[i][j]
            ans.append((i+1, j+1, i+1, j+2))
for i in range(h-1):
    if a[i][-1]%2:
        a[i+1][-1] += a[i][-1]
        ans.append((i+1, w, i+2, w))
print(len(ans))
for i in ans:
    print(*i)

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


serching_pair = False
pair_count = 0
ans = []

for i in range(H):
    if i%2 == 0:
        for j in range(0,W,1):
            if a[i][j]%2==1:
                if serching_pair:
                    ans[pair_count].append([i+1,j+1])
                    pair_count+=1
                    serching_pair=False
                else:
                    ans.append([[i+1,j+1]])
                    serching_pair=True
            else:
                if serching_pair:
                    ans[pair_count].append([i+1,j+1])

    else:
        for j in range(W-1,-1,-1):
            if a[i][j]%2==1:
                if serching_pair:
                    ans[pair_count].append([i+1,j+1])
                    pair_count+=1
                    serching_pair=False
                else:
                    ans.append([[i+1,j+1]])
                    serching_pair=True
            else:
                if serching_pair:
                    ans[pair_count].append([i+1,j+1])

ans_size=0
for i in range(0,pair_count,1):
    ans_size+=len(ans[i])-1
print(ans_size)

for i in range(0,pair_count,1):
    for j in range(1,len(ans[i])):
        print(" ".join(list(map(str,ans[i][j-1]+ans[i][j]))))

Solution (Python):
H, W = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(H)]
N = 0
A = []
for h in range(H):
  if h % 2 == 0:
    for w in range(W):
      if grid[h][w] % 2:
        if w != W-1:
          grid[h][w] -= 1
          grid[h][w+1] += 1
          A.append([h+1, w+1, h+1, w+2])
          N += 1
        else:
          if h == H-1:
            continue
          grid[h][w] -= 1
          grid[h+1][w] += 1
          A.append([h+1, w+1, h+2, w+1])
          N += 1
          
  else:
    for w in range(W-1, -1, -1):
      if grid[h][w] % 2:
        if w != 0:
          grid[h][w] -= 1
          grid[h][w-1] += 1
          A.append([h+1, w+1, h+1, w])
          N += 1
        else:
          if h == H-1:
            continue
          grid[h][w] -= 1
          grid[h+1][w] += 1
          A.append([h+1, w+1, h+2, w+1])
          N += 1
print(N)
for a in A:
  print(" ".join(map(str, a)))

Solution (Python):
import collections
h,w = map(int, raw_input().split())
mat = [map(int, raw_input().split(' ')) for _ in range(h)]
elems = [(i,j) for i in range(len(mat)) for j in range(len(mat[0]))]
elems.sort(key = lambda x: x[0] + x[1])
q = collections.deque(elems)
moves = []
while(q):
	i,j = q.popleft()
	if mat[i][j] % 2:
		if j + 1 < len(mat[0]):
			mat[i][j] -= 1
			mat[i][j+1] += 1
			moves.append([(i+1,j+1), (i+1, j + 2)])
			
		elif i + 1 < len(mat):
			mat[i][j] -=1
			mat[i+1][j] += 1
			moves.append([(i+1,j+1), (i+2, j+1)])
			
print len(moves)
for u,v in moves: print u[0],u[1],v[0],v[1]

Solution (Python):
H, W = map(int, input().split())
A = []
total = 0
for _ in range(H):
    row = list(map(int, input().split()))
    A.append(row)
    total += sum(row)

lines = []
for y in range(H - 1):
    for x in range(W):
        if A[y][x] % 2 == 1:
            lines.append(f'{y+1} {x+1} {y+2} {x+1}')
            A[y][x] -= 1
            A[y + 1][x] += 1
y = H - 1
for x in range(W - 1):
    if A[H - 1][x] % 2 == 1:
        lines.append(f'{y+1} {x+1} {y+1} {x+2}')
        A[y][x] -= 1
        A[y][x + 1] += 1
print(len(lines))
for line in lines:
    print(line)

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

result = []
y = 0
while y < H:
    x = 0
    while x < W:
        if a[y][x] % 2 == 1:
            if x == W - 1:
                if y == H - 1:
                    break
                else:
                    result.append('%d %d %d %d' % (y + 1, x + 1, y + 2, x + 1))
                    a[y + 1][x] += 1
            else:
                result.append('%d %d %d %d' % (y + 1, x + 1, y + 1, x + 2))
                a[y][x + 1] += 1
        x += 1
    if y == H - 1:
        break
    y += 1
    x = W - 1
    while x >= 0:
        if a[y][x] % 2 == 1:
            if x == 0:
                if y == H - 1:
                    break
                else:
                    result.append('%d %d %d %d' % (y + 1, x + 1, y + 2, x + 1))
                    a[y + 1][x] += 1
            else:
                result.append('%d %d %d %d' % (y + 1, x + 1, y + 1, x))
                a[y][x - 1] += 1
        x -= 1
    y += 1

print(len(result))

print(*result, sep='\n')

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

result = []
y = 0
while y < H:
    x = 0
    while x < W:
        if a[y][x] % 2 == 1:
            if x == W - 1:
                if y == H - 1:
                    break
                else:
                    result.append('%d %d %d %d' % (y + 1, x + 1, y + 2, x + 1))
                    a[y + 1][x] += 1
            else:
                result.append('%d %d %d %d' % (y + 1, x + 1, y + 1, x + 2))
                a[y][x + 1] += 1
        x += 1
    if y == H - 1:
        break
    y += 1
    x = W - 1
    while x >= 0:
        if a[y][x] % 2 == 1:
            if x == 0:
                if y == H - 1:
                    break
                else:
                    result.append('%d %d %d %d' % (y + 1, x + 1, y + 2, x + 1))
                    a[y + 1][x] += 1
            else:
                result.append('%d %d %d %d' % (y + 1, x + 1, y + 1, x))
                a[y][x - 1] += 1
        x -= 1
    y += 1

print(len(result))
print('\n'.join(result))

Solution (Python):
h,w=map(int,input().split())
a=[list(map(int,input().split())) for _ in range(h)]
th,tw=0,0
mode=0
ans=[]
ope=0
while ope<h*w:
  if mode==0:
    if tw==w-1:
      if a[th][tw]%2==1 and th+1<h:
        a[th][tw]-=1
        a[th+1][tw]+=1
        ans.append([th+1,tw+1,th+2,tw+1])
      th+=1
      mode=1
    else:
      if a[th][tw]%2==1:
        a[th][tw]-=1
        a[th][tw+1]+=1
        ans.append([th+1,tw+1,th+1,tw+2])
      tw+=1
  elif mode==1:
    if tw==0:
      if a[th][tw]%2==1 and th+1<h:
        a[th][tw]-=1
        a[th+1][tw]+=1
        ans.append([th+1,tw+1,th+2,tw+1])
      th+=1
      mode=0
    else:
      if a[th][tw]%2==1:
        a[th][tw]-=1
        a[th][tw-1]+=1
        ans.append([th+1,tw+1,th+1,tw])
      tw-=1
  ope+=1

print(len(ans))
for aa in ans:
  print(*aa)

Solution (Python):



def main():
    H, W = map(int, input().split())
    A = [list(map(int,input().split())) for _ in range(H)]
    WL = list(range(W))
    WLR = WL[::-1]

    path =[]
    for h in range(H):
        if h%2==0:
            for w in WL:
                path.append((h, w))
        else:
            for w in WLR:
                path.append((h, w))
    ret = []
    for i in range(H*W-1):
        h, w = path[i]
        if A[h][w]%2==1:
            ret.append(i)
            hn, wn = path[i+1]
            A[hn][wn]+=1

    print(len(ret))
    for i in ret:
        h,w = path[i]
        nh,nw = path[i+1]
        print(h+1,w+1,nh+1,nw+1)

if __name__ == '__main__':
    main()

Solution (Python):
import math
def main():
    h,w = map(int,input().split())
    Map = []
    ans = []
    for i in range(h):
        tmp = [int(i) for i in input().split()]
        Map.append(tmp)
    for i in range(h-1):
        for j in range(w):
            if Map[i][j]%2==1:
                Map[i][j]-=1
                Map[i+1][j] += 1
                ans.append([i+1,j+1,i+2,j+1])
    for j in range(w-1):
        if Map[h-1][j]%2==1:
            Map[h-1][j] -= 1
            Map[h-1][j+1] += 1
            ans.append([h,j+1,h,j+2])
    ans_l = len(ans)
    print(ans_l)
    for i in range(ans_l):
        print(*ans[i])
main()

Solution (Python):
H, W = 3, 2
ARR = [
    [1, 0],
    [2, 1],
    [1, 0]
]

H,W = map(int, input().split())
ARR = [list(map(int,input().split())) for i in range(H)]

def calculate(h, w, arr):
    ptt = []
    total = 0
    for i in range(h - 1):
        for j in range(w):
            if arr[i][j] % 2 == 1:
                arr[i][j] = arr[i][j] - 1
                arr[i + 1][j] = arr[i + 1][j] + 1
                total += 1
                ptt.append([str(i+1), str(j+1), str(i + 1 + 1), str(j + 1)])

    for j in range(w-1):
        if arr[-1][j] % 2 == 1:
            arr[-1][j] = arr[-1][j] - 1
            arr[-1][j+1] = arr[-1][j+1] + 1
            total += 1
            ptt.append([str(h-1 + 1), str(j + 1), str(h-1 + 1), str(j+1 + 1)])

    print(total)
    for pt in ptt:
        print(" ".join(pt))


calculate(H, W, ARR)

Solution (Python):
def main():
    H, W = map(int, input().split())
    field = []
    for _ in range(H):
        line = list(map(int, input().split()))
        field.append(line)
    
    ans = []
    
    for i in range(H):
        if i%2 == 0:
            for j in range(W):
                if field[i][j] % 2 == 1:
                    field[i][j] -= 1
                    if j != W-1:
                        field[i][j+1] += 1
                        ans.append((i+1, j+1, i+1, j+2))
                    elif i != H - 1:
                        field[i+1][j] += 1
                        ans.append((i+1, j+1, i+2, j+1))
                    
                    else:
                        field[i][j] += 1
        else:
            for j in range(W-1, -1, -1):
                if field[i][j] % 2 == 1:
                    field[i][j] -= 1
                    if j != 0:
                        field[i][j-1] += 1
                        ans.append((i+1, j+1, i+1, j))
                    elif i != H - 1:
                        field[i+1][j] += 1
                        ans.append((i+1, j+1, i+2, j+1))

                    else:
                        field[i][j] += 1
    print(len(ans))
    for line in ans:
        print(*line, sep=" ")
    


if __name__ == "__main__":
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    H, W = in_nn()
    a = [list(map(int, readline().split())) for _ in range(H)]

    odd_count = 0
    for y in range(H):
        for x in range(W):
            if a[y][x] % 2 == 1:
                odd_count += 1

    if odd_count % 2 == 1:
        odd_count -= 1

    count = 0
    x, y = 0, 0
    px, py = -1, -1
    odd_f = False

    ans = []
    while True:

        if odd_f:
            ans.append((py + 1, px + 1, y + 1, x + 1))
            if a[y][x] % 2 == 1:
                odd_f = False
        else:
            if a[y][x] % 2 == 1:
                odd_f = True

        px, py = x, y
        if a[y][x] % 2 == 1:
            count += 1
            if count >= odd_count:
                break

        if y % 2 == 0:
            if x == W - 1:
                y += 1
            else:
                x += 1
        else:
            if x == 0:
                y += 1
            else:
                x -= 1

        if y >= H:
            break

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    H, W = in_nn()
    a = np.array(read().split(), np.int32).reshape((H, W))

    odd_count = np.count_nonzero(a % 2 == 1)
    if odd_count % 2 == 1:
        odd_count -= 1

    count = 0
    x, y = 0, 0
    px, py = -1, -1
    odd_f = False

    ans = []
    while True:

        if odd_f:
            ans.append((py + 1, px + 1, y + 1, x + 1))
            if a[y][x] % 2 == 1:
                odd_f = False
        else:
            if a[y][x] % 2 == 1:
                odd_f = True

        px, py = x, y
        if a[y][x] % 2 == 1:
            count += 1
            if count >= odd_count:
                break

        if y % 2 == 0:
            if x == W - 1:
                y += 1
            else:
                x += 1
        else:
            if x == 0:
                y += 1
            else:
                x -= 1

        if y >= H:
            break

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


if __name__ == '__main__':
    main()

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

res = []
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            if a[i][j] % 2 == 1:
                if j < w - 1:
                    a[i][j+1] += 1
                    res.append([i+1, j+1, i+1, j+2])
                elif i < h - 1:
                    a[i+1][j] += 1
                    res.append([i+1, j+1, i+2, j+1])
    else:
        for j in range(w-1, -1, -1):
            if a[i][j] % 2 == 1:
                if j > 0:
                    a[i][j-1] += 1
                    res.append([i+1, j+1, i+1, j])
                elif i < h - 1:
                    a[i+1][j] += 1
                    res.append([i+1, j+1, i+2, j+1])

print(len(res))
for y1, x1, y2, x2 in res:
    print(y1, x1, y2, x2)

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

cnt = 0
for i in range(h):
    for j in range(w):
        if a[i][j] % 2 == 1:
            cnt += 1

res = []
c = 0
move_flag = False
for i in range(h):
    if i % 2 == 0:
        for j in range(w):
            if a[i][j] % 2 == 1:
                c += 1
                if move_flag:
                    move_flag = False
                else:
                    if cnt % 2 == 0 or c != cnt:
                        move_flag = True
            if move_flag:
                if j < w - 1:
                    res.append([i+1, j+1, i+1, j+2])
                else:
                    res.append([i+1, j+1, i+2, j+1])
    else:
        for j in range(w-1, -1, -1):
            if a[i][j] % 2 == 1:
                c += 1
                if move_flag:
                    move_flag = False
                else:
                    if cnt % 2 == 0 or c != cnt:
                        move_flag = True
            if move_flag:
                if j > 0:
                    res.append([i+1, j+1, i+1, j])
                else:
                    res.append([i+1, j+1, i+2, j+1])

print(len(res))
for y1, x1, y2, x2 in res:
    print(y1, x1, y2, x2)

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

for i in range(H):
    a.append([int(x) for x in input().split()])

n = 0
ans = []
for y in range(H):
    if y % 2 == 0:
        for x in range(W - 1):
            if a[y][x] & 1:
                a[y][x + 1] += 1
                n += 1
                ans.append("{} {} {} {}".format(y + 1, x + 1, y + 1, x + 2))
        if y != H - 1 and (a[y][W - 1] & 1):
            a[y + 1][W - 1] += 1
            n += 1
            ans.append("{} {} {} {}".format(y + 1, W, y + 2, W))

    else:
        for x in range(W - 1, 0, -1):
            if a[y][x] & 1:
                a[y][x - 1] += 1
                n += 1
                ans.append("{} {} {} {}".format(y + 1, x + 1, y + 1, x))
        if y != H - 1 and (a[y][0] & 1):
            a[y + 1][0] += 1
            n += 1
            ans.append("{} {} {} {}".format(y + 1, 1, y + 2, 1))

print(n)
print("\n".join(ans))