Problem p03263 (python)

Fixed submission:
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

h,w = li()
grid = [li() for _ in range(h)]

for hi in range(h):
    for wj in range(w):
        grid[hi][wj] %= 2

ans_list = []
# 奇数を左に流す
for hi in range(h):
    for wj in range(w-1):
        if grid[hi][wj] == 1:
            grid[hi][wj] = 0
            grid[hi][wj+1] = (grid[hi][wj+1]+1) % 2
            ans_list.append((hi+1, wj+1, hi+1, wj+2))




# 奇数を下に流す
for hi in range(h-1):
    if grid[hi][-1] == 1:
        grid[hi][-1] = 0
        grid[hi+1][-1] = (grid[hi+1][-1]+1) % 2
        ans_list.append((hi+1, w, hi+2, w))
        


# 結果を表示する
print(len(ans_list))
for ans in ans_list:
    print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

h, w = li()
grid = [list(li()) for _ in range(h)]

cnt = 0
ans = []

for hi in range(h):
    for wi in range(w-1):
        if grid[hi][wi] % 2:
            cnt += 1
            grid[hi][wi] -= 1
            grid[hi][wi+1] += 1
            ans.append((hi+1, wi+1, hi+1, wi+2))

for hi in range(h-1):
    if grid[hi][-1] % 2:
        cnt += 1
        grid[hi][wi] -= 1
        grid[hi+1][wi] += 1
        ans.append((hi+1, wi+1, hi+2, wi+1))

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

Buggy submission (verdict: Runtime Error):
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

h,w = li()
grid = [li() for _ in range(h)]

dxs = [0, -1, 1, 0]
dys = [-1, 0, 0, 1]

hasSearched = [[False for _ in range(w)] for _ in range(w)]

for hi in range(h):
    for wj in range(w):
        grid[hi][wj] %= 2
        
ans_list = []
# グリッド探索
for hi in range(h):
    for wj in range(w):
        isCoin = [0]*4
        for i, (dx,dy) in enumerate(zip(dxs,dys)):
            if 0 <= hi+dy < h and 0 <= wj+dx < w:
                isCoin[i] = grid[hi+dy][wj+dx]
                
        if grid[hi][wj] == 0:
            if sum(isCoin) == 4 or sum(isCoin) == 2:
                grid[hi][wj] = 0
                for i, (dx,dy) in enumerate(zip(dxs,dys)):
                    if isCoin[i] == 1:
                        grid[hi+dy][wj+dx] = 0
                        hasSearched[hi+dy][wj+dx] = True
                        
                        ans_list.append((hi,wj, hi+dy,wj+dx))
                    
            elif sum(isCoin) == 3:
                grid[hi][wj] = 0
                cnt = 0
                for i, (dx,dy) in enumerate(zip(dxs,dys)):
                    if isCoin[i] == 1:
                        grid[hi+dy][wj+dx] = 0
                        hasSearched[hi+dy][wj+dx] = True
                        ans_list.append((hi,wj, hi+dy,wj+dx))
                        
                        cnt += 1
                    if cnt == 2:
                        break
                    
        elif grid[hi][wj] == 1:
            if sum(isCoin) == 1 or sum(isCoin) == 3:
                grid[hi][wj] = 0
                for i, (dx,dy) in enumerate(zip(dxs,dys)):
                    if isCoin[i] == 1:
                        grid[hi+dy][wj+dx] = 0
                        hasSearched[hi+dy][wj+dx] = True
                        ans_list.append((hi,wj, hi+dy,wj+dx))
                        
            elif sum(isCoin) == 2:
                grid[hi][wj] = 0
                cnt = 0
                for i, (dx,dy) in enumerate(zip(dxs,dys)):
                    if isCoin[i] == 1:
                        grid[hi+dy][wj+dx] = 0
                        hasSearched[hi+dy][wj+dx] = True
                        ans_list.append((hi,wj, hi+dy,wj+dx))
                        cnt += 1
                    if cnt == 1:
                        break
                    
            elif sum(isCoin) == 4:
                grid[hi][wj] = 0
                cnt = 0
                for i, (dx,dy) in enumerate(zip(dxs,dys)):
                    if isCoin[i] == 1:
                        grid[hi+dy][wj+dx] = 0
                        hasSearched[hi+dy][wj+dx] = True
                        ans_list.append((hi,wj, hi+dy,wj+dx))
                        
                        cnt += 1
                    if cnt == 3:
                        break
                
# 出力
print(len(ans_list))
for y,x,yd,xd in ans_list:
    print(y+1, x+1, yd+1, xd+1)