Problem p03574 (python)

Fixed submission:
import sys
import collections

size = sys.stdin.readline().split(' ')
S = int(size[0])
H = int(size[1])


data = sys.stdin.readlines()
for i in range(len(data)):
	data[i] = [j.replace('\n','') for j in data[i]]


if S == 1 and H == 1:
	if data[0][0] == ".":
		data[0][0] = "0"
elif S == 1 and H > 1:
	for j in range(H):
		if data[0][j] == "#":
			continue
		else:
			if j == 0:
				list = [data[0][j+1]]
				count = str(list.count("#"))
				data[0][j] = count
			elif j == (H-1):
				list = [data[0][j-1]]
				count = str(list.count("#"))
				data[0][j] = count
			else:
				list = [data[0][j-1], data[0][j+1]]
				count = str(list.count("#"))
				data[0][j] = count
elif S > 1 and H == 1:
	for i in range(S):
		if data[i][0] == "#":
			continue
		else:
			if i == 0:
				list = [data[i+1][0]]
				count = str(list.count("#"))
				data[i][0] = count
			elif i == (S-1):
				list = [data[i-1][0]]
				count = str(list.count("#"))
				data[i][0] = count	
			else:
				list = [data[i-1][0], data[i+1][0]]
				count = str(list.count("#"))
				data[i][0] = count					
else:
	for i in range(S):
		for j in range(H):
			if data[i][j] == "#":
				continue
			else:
				#上左
				if i == 0 and j == 0:
					list = [data[i+1][j], data[i+1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				#下右
				elif i == (S-1) and j == (H-1):
					list = [data[i-1][j], data[i-1][j-1], data[i][j-1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 上右
				elif i == 0 and j == (H-1):
					list = [data[i][j-1], data[i+1][j-1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 下左
				elif i == (S-1) and j == 0:
					list = [data[i-1][j], data[i-1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 左
				elif (1 <= i < (S-1)) and j == 0:
					list = [data[i-1][j], data[i-1][j+1], data[i][j+1], data[i+1][j+1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 右
				elif (1 <= i < (S-1)) and j == (H-1):
					list = [data[i-1][j], data[i-1][j-1], data[i][j-1], data[i+1][j-1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 上
				elif i == 0 and (1<= j < (H-1)):
					list = [data[i][j-1], data[i+1][j-1], data[i+1][j], data[i+1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 下
				elif i == (S-1) and (1<= j < (H-1)):
					list = [data[i][j-1], data[i-1][j-1], data[i-1][j], data[i-1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 真ん中
				else:
					list = [data[i][j-1], data[i-1][j-1],data[i-1][j],data[i-1][j+1],data[i][j+1],data[i+1][j+1],data[i+1][j],data[i+1][j-1]]
					count = str(list.count("#"))
					data[i][j] = count



for i in range(len(data)):
	ans = str(data[i]).replace('[', '').replace(']', '').replace(',', '').replace("'", '').replace(' ', '')
	print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import collections

size = sys.stdin.readline().split(' ')
S = int(size[0])
H = int(size[1])


data = sys.stdin.readlines()
for i in range(len(data)):
	data[i] = [j.replace('\n','') for j in data[i]]


if S == 1 and H == 1:
	if data[0][0] == ".":
		data[0][0] = "0"
elif S == 1 and H > 1:
	for j in range(H):
		if data[0][j] == "#":
			continue
		else:
			if j == 0:
				list = [data[0][j+1]]
				count = str(list.count("#"))
				data[0][j] = count
			elif j == (H-1):
				list = [data[0][j-1]]
				count = str(list.count("#"))
				data[0][j] = count
			else:
				list = [data[0][j-1], data[0][j+1]]
				count = str(list.count("#"))
				data[0][j] = count
elif S > 1 and H == 1:
	for i in range(S):
		if data[i][0] == "#":
			continue
		else:
			if i == 0:
				list = [data[i+1][0]]
				count = str(list.count("#"))
				data[i][0] = count
			elif i == (S-1):
				list = [data[i-1][0]]
				count = str(list.count("#"))
				data[i][0] = count	
			else:
				list = [data[i-1][0], data[i+1][0]]
				count = str(list.count("#"))
				data[0][j] = count					
else:
	for i in range(S):
		for j in range(H):
			if data[i][j] == "#":
				continue
			else:
				#上左
				if i == 0 and j == 0:
					list = [data[i+1][j], data[i+1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				#下右
				elif i == (S-1) and j == (H-1):
					list = [data[i-1][j], data[i-1][j-1], data[i][j-1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 上右
				elif i == 0 and j == (H-1):
					list = [data[i][j-1], data[i+1][j-1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 下左
				elif i == (S-1) and j == 0:
					list = [data[i-1][j], data[i-1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 左
				elif (1 <= i < (S-1)) and j == 0:
					list = [data[i-1][j], data[i-1][j+1], data[i][j+1], data[i+1][j+1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 右
				elif (1 <= i < (S-1)) and j == (H-1):
					list = [data[i-1][j], data[i-1][j-1], data[i][j-1], data[i+1][j-1], data[i+1][j]]
					count = str(list.count("#"))
					data[i][j] = count
				# 上
				elif i == 0 and (1<= j < (H-1)):
					list = [data[i][j-1], data[i+1][j-1], data[i+1][j], data[i+1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 下
				elif i == (S-1) and (1<= j < (H-1)):
					list = [data[i][j-1], data[i-1][j-1], data[i-1][j], data[i-1][j+1], data[i][j+1]]
					count = str(list.count("#"))
					data[i][j] = count
				# 真ん中
				else:
					list = [data[i][j-1], data[i-1][j-1],data[i-1][j],data[i-1][j+1],data[i][j+1],data[i+1][j+1],data[i+1][j],data[i+1][j-1]]
					count = str(list.count("#"))
					data[i][j] = count



for i in range(len(data)):
	ans = str(data[i]).replace('[', '').replace(']', '').replace(',', '').replace("'", '').replace(' ', '')
	print(ans)