Buggy python submission (verdict: Runtime Error):
import itertools
S,M,L = 5,9,13
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def minus(C,x,y,size):
	for i in range(size):
		try: C[y+dy[i]][x+dx[i]] -= 1
		except: pass

def canMinus(C,x,y,size):
	for i in range(size):
		try:
			if C[y+dy[i]][x+dx[i]] == 0:
				return False
		except: pass
	return True

def canMinusList(C,size):
	ls = []
	for y in range(10):
		for x in range(10):
			if canMinus(C,x,y,size):
				ls.append([x,y])
	return ls

def copy(C):
	C_copy = [C[i][:] for i in range(len(C))]
	return C_copy

def tryFindAns(Cloth,drop,p,Rslt):
	size,num = drop[p][0],drop[p][1]
	canList = canMinusList(Cloth,size)
	for comb in itertools.combinations(canList,num):
		rslt = copy(Rslt)
		C = copy(Cloth)
		for x,y in comb:
			if  canMinus(C,x,y,size):
				minus(C,x,y,size)
				rslt.append([x,y,size])
			else:
				break
		else:
			if p+1 == len(drop):
				return rslt
			else:
				return tryFindAns(C,drop,p+1,rslt)

def findAns(Cloth,drop):
	for i in range(2,-1,-1):
		if drop[i][1] == 0:
			drop.pop(i)
	p,rslt = 0,[]
	return tryFindAns(Cloth,drop,p,rslt)

n = int(raw_input())
Cloth = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(Cloth[i]) for i in range(10)])
dropList = []

for s in range(n):
	m4 = 13*n-sm-8*s
	if m4 < 0: break
	if m4%4 == 0:
		m = m4/4
		if s+m <= n:
			dropList.append([[13,n-s-m],[9,m],[5,s]])

for drop in dropList:
	ans = findAns(Cloth,drop)
	if ans is not None:
		break

for a in ans:
	print  a[0],a[1],ref[a[2]]

Fixed submission:
import itertools
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def isOn(x,y):
    return 0<=x<10 and 0<=y<10

def minus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] -= 1

def plus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] += 1

def canMinus(C,x,y,size):
    return all([isOn(x+dx[i],y+dy[i]) and C[y+dy[i]][x+dx[i]]>0 for i in range(size)])

def findAns(C,drops,sp):
    if len(drops) == 0:
        return []
    for i in range(sp,100):
        if C[i/10][i%10] > 0:
            break
        sp += 1
    for size in set(drops):
        for i in range(size):
            sx,sy = sp%10+dx[i],sp/10+dy[i]
            if isOn(sx,sy) and C[sy][sx] > 0:
                if canMinus(C,sx,sy,size):
                    minus(C,sx,sy,size)
                    drops.remove(size)
                    ans = findAns(C,drops,sp)
                    if ans != False:
                        ans.append([sx,sy,size])
                        return ans
                    drops.append(size)
                    plus(C,sx,sy,size)
    return False

n = int(raw_input())
C = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(C[i]) for i in range(10)])
dropList = [list(a) for a in itertools.combinations_with_replacement([0,13,9,5],n) if sum(a) == sm]
for drops in dropList:
    ans = findAns(C,drops,0)
    if ans != False:
        break
        
for a in ans:
    print  a[0],a[1],ref[a[2]]

Buggy python submission (verdict: Runtime Error):
import itertools
S,M,L = 5,9,13
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def minus(C,x,y,size):
	for i in range(size):
		try: C[y+dy[i]][x+dx[i]] -= 1
		except: pass

def canMinus(C,x,y,size):
	for i in range(size):
		try:
			if C[y+dy[i]][x+dx[i]] == 0:
				return False
		except: pass
	return True

def canMinusList(C,size):
	ls = []
	for y in range(10):
		for x in range(10):
			if canMinus(C,x,y,size):
				ls.append([x,y])
	return ls

def copy(C):
	C_copy = [C[i][:] for i in range(len(C))]
	return C_copy

def tryFindAns(Cloth,drop,p,Rslt):
	size,num = drop[p][0],drop[p][1]
	canList = canMinusList(Cloth,size)
	for comb in itertools.combinations(canList,num):
		rslt = copy(Rslt)
		C = copy(Cloth)
		for x,y in comb:
			if  canMinus(C,x,y,size):
				minus(C,x,y,size)
				rslt.append([x,y,size])
			else:
				break
		else:
			if p+1 == len(drop):
				return rslt
			else:
				rslt = tryFindAns(C,drop,p+1,rslt)
		if rslt is not None and len(rslt) == n: return rslt

def findAns(Cloth,drop):
	for i in range(2,-1,-1):
		if drop[i][1] == 0:
			drop.pop(i)
	p,rslt = 0,[]
	return tryFindAns(Cloth,drop,p,rslt)

n = int(raw_input())
Cloth = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(Cloth[i]) for i in range(10)])
dropList = []

for s in range(n):
	m4 = 13*n-sm-8*s
	if m4 < 0: break
	if m4%4 == 0:
		m = m4/4
		if s+m <= n:
			dropList.append([[13,n-s-m],[9,m],[5,s]])

for drop in dropList:
	ans = findAns(Cloth,drop)
	if ans is not None:
		break

for a in ans:
	print  a[0],a[1],ref[a[2]]

Fixed submission:
import itertools
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def isOn(x,y):
    return 0<=x<10 and 0<=y<10

def minus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] -= 1

def plus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] += 1

def canMinus(C,x,y,size):
    return all([isOn(x+dx[i],y+dy[i]) and C[y+dy[i]][x+dx[i]]>0 for i in range(size)])

def findAns(C,drops,sp):
    if len(drops) == 0:
        return []
    for i in range(sp,100):
        if C[i/10][i%10] > 0:
            break
        sp += 1
    for size in set(drops):
        for i in range(size):
            sx,sy = sp%10+dx[i],sp/10+dy[i]
            if isOn(sx,sy) and C[sy][sx] > 0:
                if canMinus(C,sx,sy,size):
                    minus(C,sx,sy,size)
                    drops.remove(size)
                    ans = findAns(C,drops,sp)
                    if ans != False:
                        ans.append([sx,sy,size])
                        return ans
                    drops.append(size)
                    plus(C,sx,sy,size)
    return False

n = int(raw_input())
C = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(C[i]) for i in range(10)])
dropList = [list(a) for a in itertools.combinations_with_replacement([0,13,9,5],n) if sum(a) == sm]
for drops in dropList:
    ans = findAns(C,drops,0)
    if ans != False:
        break
        
for a in ans:
    print  a[0],a[1],ref[a[2]]

Buggy python submission (verdict: Runtime Error):
import itertools
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def isOn(x,y):
    return 0<=x<10 and 0<=y<10

def minus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] -= 1

def plus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] += 1

def canMinus(C,x,y,size):
    return all([isOn(x+dx[i],y+dy[i]) and C[y+dy[i]][x+dx[i]]>0 for i in range(size)])

def findAns(C,drops,k,sp):
    if k == len(drops):
        return []
    for i in range(sp,100):
        if C[i/10][i%10] > 0:
            break
        sp += 1
    for drop in drops:
        size = drop[0]
        for i in range(size):
            sx,sy = sp%10+dx[i],sp/10+dy[i]
            if isOn(sx,sy) and C[sy][sx] > 0:
                if canMinus(C,sx,sy,size):
                    minus(C,sx,sy,size)
                    drops[k][1] -= 1
                    pk=1 if drops[k][1] == 0 else 0
                    ans = findAns(C,drops,k+pk,sp)
                    if ans != False:
                        ans.append([sx,sy,size])
                        return ans
                    drops[k][1] += 1
                    plus(C,sx,sy,size)
    return False

n = int(raw_input())
C = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(C[i]) for i in range(10)])
dropList = []
for s in range(n):
    m4 = 13*n-sm-8*s
    if m4 < 0: break
    if m4%4 == 0:
        m = m4/4
        if s+m <= n:
            dropList.append([[13,n-s-m],[9,m],[5,s]])
for drops in dropList:
    for i in range(2,-1,-1):
        if drops[i][1] == 0:
            drops.pop(i)
    ans = findAns(C,drops,0,0)
    if ans != False:
        break
for a in ans:
    print  a[0],a[1],ref[a[2]]

Fixed submission:
import itertools
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def isOn(x,y):
    return 0<=x<10 and 0<=y<10

def minus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] -= 1

def plus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] += 1

def canMinus(C,x,y,size):
    return all([isOn(x+dx[i],y+dy[i]) and C[y+dy[i]][x+dx[i]]>0 for i in range(size)])

def findAns(C,drops,sp):
    if len(drops) == 0:
        return []
    for i in range(sp,100):
        if C[i/10][i%10] > 0:
            break
        sp += 1
    for size in set(drops):
        for i in range(size):
            sx,sy = sp%10+dx[i],sp/10+dy[i]
            if isOn(sx,sy) and C[sy][sx] > 0:
                if canMinus(C,sx,sy,size):
                    minus(C,sx,sy,size)
                    drops.remove(size)
                    ans = findAns(C,drops,sp)
                    if ans != False:
                        ans.append([sx,sy,size])
                        return ans
                    drops.append(size)
                    plus(C,sx,sy,size)
    return False

n = int(raw_input())
C = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(C[i]) for i in range(10)])
dropList = [list(a) for a in itertools.combinations_with_replacement([0,13,9,5],n) if sum(a) == sm]
for drops in dropList:
    ans = findAns(C,drops,0)
    if ans != False:
        break
        
for a in ans:
    print  a[0],a[1],ref[a[2]]

Buggy python submission (verdict: Runtime Error):
import itertools
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def isOn(x,y):
    return 0<=x<10 and 0<=y<10

def minus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] -= 1

def plus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] += 1

def canMinus(C,x,y,size):
    return all([isOn(x+dx[i],y+dy[i]) and C[y+dy[i]][x+dx[i]]>0 for i in range(size)])

def findAns(C,drops,k,sp):
    if k == len(drops):
        return []
    for i in range(sp,100):
        if C[i/10][i%10] > 0:
            break
        sp += 1
    for drop in drops:
        size = drop[0]
        if drop[1] != 0:
            for i in range(size):
                sx,sy = sp%10+dx[i],sp/10+dy[i]
                if isOn(sx,sy) and C[sy][sx] > 0:
                    if canMinus(C,sx,sy,size):
                        minus(C,sx,sy,size)
                        drops[k][1] -= 1
                        pk=1 if drops[k][1] == 0 else 0
                        ans = findAns(C,drops,k+pk,sp)
                        if ans != False:
                            ans.append([sx,sy,size])
                            return ans
                        drops[k][1] += 1
                        plus(C,sx,sy,size)
    return False

n = int(raw_input())
C = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(C[i]) for i in range(10)])
dropList = []
for s in range(n):
    m4 = 13*n-sm-8*s
    if m4 < 0: break
    if m4%4 == 0:
        m = m4/4
        if n-s-m >= 0:
            dropList.append([[13,n-s-m],[9,m],[5,s]])
for drops in dropList:
    for i in range(2,-1,-1):
        if drops[i][1] == 0:
            drops.pop(i)
    ans = findAns(C,drops,0,0)
    if ans != False:
        break
for a in ans:
    print  a[0],a[1],ref[a[2]]

Fixed submission:
import itertools
ref = {5:1,9:2,13:3}
dx = [0, 0, 1, 0,-1, 1, 1,-1,-1, 2, 0,-2, 0]
dy = [0,-1, 0, 1, 0,-1, 1, 1,-1, 0, 2, 0,-2]

def isOn(x,y):
    return 0<=x<10 and 0<=y<10

def minus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] -= 1

def plus(C,x,y,size):
    for i in range(size):
        C[y+dy[i]][x+dx[i]] += 1

def canMinus(C,x,y,size):
    return all([isOn(x+dx[i],y+dy[i]) and C[y+dy[i]][x+dx[i]]>0 for i in range(size)])

def findAns(C,drops,sp):
    if len(drops) == 0:
        return []
    for i in range(sp,100):
        if C[i/10][i%10] > 0:
            break
        sp += 1
    for size in set(drops):
        for i in range(size):
            sx,sy = sp%10+dx[i],sp/10+dy[i]
            if isOn(sx,sy) and C[sy][sx] > 0:
                if canMinus(C,sx,sy,size):
                    minus(C,sx,sy,size)
                    drops.remove(size)
                    ans = findAns(C,drops,sp)
                    if ans != False:
                        ans.append([sx,sy,size])
                        return ans
                    drops.append(size)
                    plus(C,sx,sy,size)
    return False

n = int(raw_input())
C = [map(int, raw_input().split()) for i in range(10)]
sm = sum([sum(C[i]) for i in range(10)])
dropList = [list(a) for a in itertools.combinations_with_replacement([0,13,9,5],n) if sum(a) == sm]
for drops in dropList:
    ans = findAns(C,drops,0)
    if ans != False:
        break
        
for a in ans:
    print  a[0],a[1],ref[a[2]]