Problem p03263 (python)

Fixed submission:
import numpy as np
h,w = list(map(int, input().split()))
xs = []
for _ in range(h):
    xs.append(list(map(int, input().split())))
#     xs.append(np.random.binomial(10,0.5,w))

def move_yoko(k,x):
    temp = np.where(x[k] == 1)[0]
    ms = []
    if len(temp)%2 == 0:
        for j in range(int(len(temp)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
                ms.append([k,a+i,k,a+1+i])
        x[k] = 0
    else:
        for j in range(int((len(temp)-1)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
                ms.append([k,a+i,k,a+1+i])
        a = temp[-1]
        b = w - 1
        for i in range(b - a):
            ms.append([k,a+i,k,a+1+i])
        x[k] = 0
        x[k,-1] = 1
    return ms,x

def move_tate(k,x):
    temp = np.where(x[:,k] == 1)[0]
    ms = []
    if len(temp)%2 == 0:
        for j in range(int(len(temp)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
#                 ms.append([k,a+i,k,a+1+i])
                ms.append([a+i,k,a+1+i,k])
        x[:,k] = 0
    else:
        for j in range(int((len(temp)-1)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
                ms.append([a+i,k,a+1+i,k])
                
        a = temp[-1]
        b = h - 1
        for i in range(b - a):
            ms.append([a+i,k,a+1+i,k])
        x[:,k] = 0
        x[-1,k] = 1
    return ms,x

x = np.vstack(xs)
x = x%2

mss = []
for k in range(h):
    ms, x = move_yoko(k,x)
    mss += ms
ms, x = move_tate(w-1,x)
mss += ms
ms = mss

import sys
print(len(ms))
for a in ms:
    for k in range(len(a)):
        if k == len(a)-1:
            sys.stdout.write('{}'.format(a[k] + 1))
        else:
            sys.stdout.write('{} '.format(a[k] + 1))
    sys.stdout.write('\n')

Buggy submission (verdict: Runtime Error):
import numpy as np
h,w = list(map(int, input().split()))
xs = []
for _ in range(h):
    xs.append(list(map(int, input().split())))
#     xs.append(np.random.binomial(10,0.5,w))

def move_yoko(k,x):
    temp = np.where(x[k] == 1)[0]
    ms = []
    if len(temp)%2 == 0:
        for j in range(int(len(temp)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
                ms.append([k,a+i,k,a+1+i])
        x[k] = 0
    else:
        for j in range(int((len(temp)-1)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
                ms.append([k,a+i,k,a+1+i])
        a = temp[-1]
        b = w - 1
        for i in range(b - a):
            ms.append([k,a+i,k,a+1+i])
        x[k] = 0
        x[k,-1] = 1
    return ms,x

def move_tate(k,x):
    temp = np.where(x[:,k] == 1)[0]
    ms = []
    if len(temp)%2 == 0:
        for j in range(int(len(temp)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
#                 ms.append([k,a+i,k,a+1+i])
                ms.append([a+i,k,a+1+i,k])
        x[:,k] = 0
    else:
        for j in range(int((len(temp)-1)/2)):
            a = temp[2*j]
            b = temp[2*j+1]
            for i in range(b - a):
                ms.append([a+i,k,a+1+i,k])
                
        a = temp[-1]
        b = h - 1
        for i in range(b - a):
            ms.append([a+i,k,a+1+i,k])
        x[:,k] = 0
        x[-1,k] = 1
    return ms,x

x = np.vstack(xs)
x = x%2

mss = []
for k in range(h):
    ms, x = move_yoko(k,x)
    mss += ms
ms, x = move_tate(w-1,x)
mss += ms
ms = mss

import sys
print(len(ms))
for a in ms:
    for k in range(a):
        if k == len(a)-1:
            sys.stdout.write('{}'.format(a[k] + 1))
        else:
            sys.stdout.write('{} '.format(a[k] + 1))
    sys.stdout.write('\n')

Buggy submission (verdict: Runtime Error):
h,w = list(map(int, input().split()))
xs = []
for _ in range(h):
    xs.append(list(map(int, input().split())))

x = np.vstack(xs)
x = x%2
x

def move(x,m):
    x[m[0],m[1]] -= 1
    x[m[2],m[3]] += 1
    x = x%2
    return x

ms = []
k = 0

while True:
    temp = np.where(x[k] == 1)[0]
    if len(temp) == 0 :
        k += 1
    else:
        i = temp[0]
        if i < w - 1:
            m = [k,i,k,i+1]
            ms.append(m)
            x = move(x,m)
        
        else:
            k += 1
    if k >= h:
        break

k = 0

while True:
    temp = np.where(x[:,k] == 1)[0]
    if len(temp) == 0 :
        k += 1
    else:
        i = temp[0]
        if i < h - 1:
            m = [i,k,i+1,k]
            ms.append(m)
            x = move(x,m)        
        else:
            k += 1
    if k >= w:
        break

import sys

print(len(ms))
for a in ms:
    for aa in a:
        sys.stdout.write('{} '.format(aa + 1))
    sys.stdout.write('\n')