Problem p03417 (python)

Fixed submission:
N,M=input().split()
N,M=[int(N),int(M)]

def countCard(j,i):
	count = 0
	left=0
	right=0
	up =0
	down=0

	if j-1 >= 0:
		left = 1

	if j+1 < M:
		right = 1

	if i-1 >= 0:
		up = 1

	if i+1 < N:
		down = 1

	a= left + right + up + down
	if a == 4:
		return 0	#9
	elif a == 3:
		return 1	#6
	elif a == 2:
		if left + right == 0 or up+down==0:
			return 0	#3
		else:
			return 1	#4
	elif a == 1:
		return 1	#2
	elif a == 0:
		return 0

ans = 0

if M >= 3 and N >=3:
	ans = (M-2)*(N-2)
elif M == 1 and N ==1:
	ans = 1
elif N == 1 and M > 2:
	ans = M -2
elif M ==1 and N>2:
	ans = N-2
else:
	status = [[1]*M]*N
	for i in range(N):
		for j in range(M):
			status[i][j] = countCard(j,i)
			ans += status[i][j]
	ans = (M*N) - ans

print(ans)

Buggy submission (verdict: Runtime Error):
N,M=input().split()
N,M=[int(N),int(M)]

status = [[1]*M]*N

def countCard(j,i):
	count = 0
	left=0
	right=0
	up =0
	down=0

	if j-1 >= 0:
		left = 1

	if j+1 < M:
		right = 1

	if i-1 >= 0:
		up = 1

	if i+1 < N:
		down = 1

	a= left + right + up + down
	if a == 4:
		return 0	#9
	elif a == 3:
		return 1	#6
	elif a == 2:
		if left + right == 0 or up+down==0:
			return 0	#3
		else:
			return 1	#4
	elif a == 1:
		return 1	#2
	elif a == 0:
		return 0

ans = 0

if M >= 3 and N >=3:
	ans = (M-2)*(N-2)
else:
	for i in range(N):
		for j in range(M):
			status[i][j] = countCard(j,i)
			ans += status[i][j]
	ans = (M*N) - ans

print(ans)

Buggy submission (verdict: Runtime Error):
N,M=input().split()
N,M=[int(N),int(M)]
#M ga retu j
#N ga gyou i
status = [[1]*M]*N

#0 0 0 0 0 0 0
#print(status)

def countCard(j,i):
	#print("-------------")
	#print("status[{}][{}]={}".format(i,j,status[i][j]))
	count = 0
	left=0
	right=0
	up =0
	down=0

	if j-1 >= 0:
		left = 1

	if j+1 < M:
		right = 1

	if i-1 >= 0:
		up = 1

	if i+1 < N:
		down = 1

	a= left + right + up + down
	#print("left:{},right:{},up:{},down:{}".format(left,right,up,down))
	if a == 4:
		return 0	#9
	elif a == 3:
		return 1	#6
	elif a == 2:
		if left + right == 0 or up+down==0:
			return 0	#3
		else:
			return 1	#4
	elif a == 1:
		return 1	#2
	elif a == 0:
		return 0
	else:
		print('error')
		quit()

ans = 0
for i in range(N):
	for j in range(M):
		status[i][j] = countCard(j,i)
		ans += status[i][j]
		#print("status[{}][{}]={}".format(i,j,status[i][j]))


#print(status)

#for i in range(N):
#	ans += sum(status[i])
ans = (M*N) - ans
print(ans)

Buggy submission (verdict: Runtime Error):
N,M=input().split()
N,M=[int(N),int(M)]
#M ga retu i
#N ga gyou j
status = [[1]*M]*N

#print(status)

def countCard(j,i):
	#print("-------------")
	#print("status[{}][{}]={}".format(i,j,status[i][j]))
	count = 0
	left=0
	right=0
	up =0
	down=0
	tmp = j-1
	if tmp >= 0:
		left = 1
	tmp = j+1
	if tmp < M:
		right = 1
	tmp=i-1
	if tmp >= 0:
		up = 1
	tmp=i+1
	if tmp < N:
		down = 1
	a= left + right + up + down
	#print("left:{},right:{},up:{},down:{}".format(left,right,up,down))
	if a == 4:
		return 0	#9
	elif a == 3:
		return 1	#6
	elif a == 2:
		if left + right == 0 or up+down==0:
			return 0	#3
		else:
			return 1	#4
	elif a == 1:
		return 1	#2
	elif a == 0:
		return 0
	else:
		print('error')
		quit()


for i in range(M):
	for j in range(N):
		status[j][i] = countCard(j,i)
		#print("status[{}][{}] = {}".format(i,j,status[i][j]))


#print(status)
ans = 0
for i in range(N):
	ans += sum(status[i])
print(ans)