Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
arr = list(map(int,input().split()))
arr.insert(0,0)
arr.append(0)
ans = 0
tmp = 0
for i in range(1,n + 1):
    for j in range(1,len(arr)):
        if i != j:
            ans += abs(arr[j] - tmp)
            tmp = arr[j]
    print(ans)
    ans = 0

Fixed submission:
n = int(input())
arr = [0] + list(map(int,input().split())) + [0]
tmp= [ abs(arr[i] - arr[i + 1]) for i in range(len(arr) - 1)]
_sum = sum(tmp)
for i in range(n):
    ans = _sum - tmp[i] - tmp[i + 1] + abs(arr[i + 2] - arr[i])
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
arr = list(map(int,input().split()))
arr.insert(0,0)
arr.append(0)
ans = 0
tmp = 0
for i in range(1,n + 1):
    for j in range(1,len(arr)):
        if i == j:
            pass
        else:
            ans += abs(arr[j] - tmp)
            tmp = arr[j]
    print(ans)
    ans = 0

Fixed submission:
n = int(input())
arr = [0] + list(map(int,input().split())) + [0]
tmp= [ abs(arr[i] - arr[i + 1]) for i in range(len(arr) - 1)]
_sum = sum(tmp)
for i in range(n):
    ans = _sum - tmp[i] - tmp[i + 1] + abs(arr[i + 2] - arr[i])
    print(ans)
Buggy python submission (verdict: Runtime Error):
n,y = map(int,input().split())

for i in range(n+1):
    for j in range(n+1):
        k = n - i - j
        if 10000 * i + 5000 * j + 1000 * k == y and y >= 0:
            print(i,j,k)
            exit()
print('-1 -1 -1'):

Fixed submission:
n,y = map(int,input().split())

for i in range(n+1):
    for j in range(n+1):
        k = n - i - j
        if (10000 * i + 5000 * j + 1000 * k) == y and k >= 0:
            print(i,j,k)
            exit()
print('-1 -1 -1')
Buggy python submission (verdict: Time Limit Exceeded):
import math
n = int(input())
num = 0
for i in range(n,0,-1):
    num = math.sqrt(i)
    if len(list(str(num))) == 3:
        print(i)
        break
    else:
        pass

Fixed submission:
n = int(input())
n = int(n ** 0.5)
print(int(n ** 2))
Buggy python submission (verdict: Runtime Error):
 s = input()
print(s.count("1"))

Fixed submission:
s = input()
print(s.count("1"))
Buggy python submission (verdict: Runtime Error):
n = int(input())
arr = {}
for i in range(n):
    num = int(input())
    if num in arr:
        arr.remove(num)
    else:
        arr.add(num)
print(len(arr))

Fixed submission:
n = input()
if "9" in n:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
arr = []
for i in range(n):
    num = int(input())
    if num in arr:
        arr.remove(num)
    else:
        arr.append(num)
print(len(arr))

Fixed submission:
n = int(input())
arr = set()
for i in range(n):
    num = int(input())
    if num in arr:
        arr.remove(num)
    else:
        arr.add(num)
print(len(arr))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
arr = []
count = 0
for i in range(n):
    num = int(input())
    if num not in arr:
        arr.append(num)
        count += 1
    else:
        arr.remove(num)
        count -= 1

print(count)


Fixed submission:
n = int(input())
arr = set()
for i in range(n):
    num = int(input())
    if num in arr:
        arr.remove(num)
    else:
        arr.add(num)
print(len(arr))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
arr = []
for i in range(n):
    num = input()
    if num in arr:
        arr.remove(num)
    else:
        arr.append(num)
print(len(arr))


Fixed submission:
n = int(input())
arr = set()
for i in range(n):
    num = int(input())
    if num in arr:
        arr.remove(num)
    else:
        arr.add(num)
print(len(arr))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
arr = []
for i in range(n):
    num = int(input())
    if num not in arr:
        arr.append(num)
    else:
        arr.remove(num)
print(len(arr))

Fixed submission:
n = int(input())
arr = set()
for i in range(n):
    num = int(input())
    if num in arr:
        arr.remove(num)
    else:
        arr.add(num)
print(len(arr))
Buggy python submission (verdict: Runtime Error):
n = int(input())
arr = input().split()
ans = []
for i in range(n):
    arr[i] = int(arr[i])
x = 0
y = sum(arr)
for i in range(n):
    if x <= y:
        x += arr[i]
        y -= arr[i]
        ans.append(x - y)
ans = map(abs,ans)
print(min(ans))

Fixed submission:
n = int(input())
arr = list(map(int,input().split()))
sum_arr = sum(arr)
ans=[]
x = 0
for i in range(n-1):
    x += arr[i]
    y = sum_arr - x
    ans.append(abs(x - y))
print(min(ans))

Buggy python submission (verdict: Runtime Error):
n = int(input())
arr = input().split()
ans = []
for i in range(n):
    arr[i] = int(arr[i])
x = 0
y = sum(arr)
for i in range(n):
    if x <= y:
        x += arr[i]
        y -= arr[i]
        ans.append(abs(x - y))
print(min(ans))


Fixed submission:
n = int(input())
arr = list(map(int,input().split()))
sum_arr = sum(arr)
ans=[]
x = 0
for i in range(n-1):
    x += arr[i]
    y = sum_arr - x
    ans.append(abs(x - y))
print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
arr = input().split()
x = 0
y = 0
for i in range(n):
    arr[i] = int(arr[i])
min_xy = abs(arr[0] - sum(arr[1:]))
for i in range(1,n):
    x = sum(arr[:i])
    y = sum(arr[i:])
    if min_xy > abs(x - y):
        min_xy = abs(x - y)
print(min_xy)



Fixed submission:
n = int(input())
arr = list(map(int,input().split()))
sum_arr = sum(arr)
ans=[]
x = 0
for i in range(n-1):
    x += arr[i]
    y = sum_arr - x
    ans.append(abs(x - y))
print(min(ans))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
b = []
for i in a:
    b.append(i)
    b.reverse()
for i in range(n):
    print(b[i],end=" ")


Fixed submission:
n = int(input())
a = input().split()
b = []
ans = []
even = a[1::2]
odd = a[0::2]
b.append(even)#even number
b.append(odd)#odd number
# print(b)
if len(a) % 2 == 0:
    for i in range(1,len(even) + 1):
        ans.append(even[-i])
    ans = ans + odd
else:
    for i in range(1,len(odd) + 1):
        ans.append(odd[-i])
    ans = ans + even
for i in range(len(ans)):
    if i != len(ans) -1:
        print(ans[i],end=" ")
    else:
        print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
b = []
for i in a:
    b.append(i)
    b.reverse()
n = ""
for i in b:
    n += i + " "
print(n)

Fixed submission:
n = int(input())
a = input().split()
b = []
ans = []
even = a[1::2]
odd = a[0::2]
b.append(even)#even number
b.append(odd)#odd number
# print(b)
if len(a) % 2 == 0:
    for i in range(1,len(even) + 1):
        ans.append(even[-i])
    ans = ans + odd
else:
    for i in range(1,len(odd) + 1):
        ans.append(odd[-i])
    ans = ans + even
for i in range(len(ans)):
    if i != len(ans) -1:
        print(ans[i],end=" ")
    else:
        print(ans[i])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = input().split()
b = []
for i in a:
    b.append(i)
    b.reverse()
c = ""
for i in b:
    c += i + " "
print(c)

Fixed submission:
n = int(input())
a = input().split()
b = []
ans = []
even = a[1::2]
odd = a[0::2]
b.append(even)#even number
b.append(odd)#odd number
# print(b)
if len(a) % 2 == 0:
    for i in range(1,len(even) + 1):
        ans.append(even[-i])
    ans = ans + odd
else:
    for i in range(1,len(odd) + 1):
        ans.append(odd[-i])
    ans = ans + even
for i in range(len(ans)):
    if i != len(ans) -1:
        print(ans[i],end=" ")
    else:
        print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int,input().split())
arr = []
for i in range(n):
    a,b = map(int,input().split())
    arr += [a] * b

arr.sort()
print(arr[k - 1])

Fixed submission:
n, k = map(int,input().split())
arr = []
for _ in range(n):
    a,b = map(int,input().split())
    arr.append([a,b])
arr.sort()
for i in arr:
    k -= i[1]
    if k <= 0:
        print(i[0])
        break
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
power = 1
for i in range(1,n + 1):
    power *= i
print(power % (10**9 + 7))

Fixed submission:
import math
n = int(input())
power = math.factorial(n)
print(power % (10**9 + 7))
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int,input().split())
ans = 0
while n >= 1 and m >= 2:
    n -= 1
    m -= 2
    ans += 1
if n < 1:
    ans += (m // 4)
print(ans)

Fixed submission:
n, m = map(int,input().split())
print((min(n * 2, m) + m) // 4)
Buggy python submission (verdict: Runtime Error):
if a == b:
    print("Draw")
elif a > b and b != 1:
    print("Alice")
elif b == 1 and a > b:
    print("Bob")
elif a == 1 and b > a:
    print("Alice")
else:
    print("Bob")

Fixed submission:
a,b = map(int,input().split())
if a == 1:
    a = 14
if b == 1:
    b = 14
if a == b:
    print("Draw")
elif a < b:
    print("Bob")
else:
    print("Alice")
Buggy python submission (verdict: Time Limit Exceeded):
k,s = map(int,input().split())
ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        for k in range(k + 1):
            if i + j + k == s:
                ans += 1
print(ans)

Fixed submission:
k,s = map(int,input().split())
ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        if 0 <= s - i - j <= k:
            ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
k,s = map(int,input().split())
ans = 0
x = [i for i in range(k + 1)]
y = [i for i in range(k + 1)]
z = [i for i in range(k + 1)]
arr = list(itertools.product(x,y,z))
for i in arr:
    if sum(i) == s:
        ans += 1
print(ans)

Fixed submission:
k,s = map(int,input().split())
ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        if 0 <= s - i - j <= k:
            ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
a,b = [int(i) for i in input().split()]

step = 2
towers = [1]
i = 1
while True:
    towers.append(towers[i-1] + step)
    
    if towers[i] >= 499500:
        break
    i += 1
    step += 1


depth = towers[(b-a)-1] - b
print(depth)



Fixed submission:
N = int(input())

if N < 1000:
    print("ABC")
else:
    print("ABD")
Buggy python submission (verdict: Time Limit Exceeded):
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))

##### main #####

def is_prime(n):
    i=2
    while i*i <= n:
        if n%i == 0:
            return False
        i+=1
    return n != 1


def main():
	n = I()
	memo = {}
	for i in range(n):
		l,r = LI()
		x=0
		for j in range(l,r+1,2):
			if j in memo:
				if memo[j] == True:
					x+=1
			else:
				jj = (j+1)/2
				if is_prime(j) and is_prime(jj):
					memo[j] = True
					x+=1
				else:
					memo[j] = False
		print(x)



if __name__ == '__main__':
	main()

Fixed submission:
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))

##### main #####

def primes(max_number):
    """
    指定値未満の素数を取得するジェネレータ
    https://qiita.com/Mokkeee/items/93b6b7d59fa256aa1677
    """
    if max_number <= 2:
        raise StopIteration
    yield 2
    is_prime = [1] * max_number
    square_root_of_max = max_number ** 0.5
    for number in range(3, max_number, 2):
        if is_prime[number] is 0:
            continue
        yield number
        if number <= square_root_of_max:
            for multiple in range(number * 2, max_number, number):
                is_prime[multiple] = 0

def main():
	P = list(primes(10**5))
	P = [p for p in P[1:] if (p+1)//2 == P[bisect_left(P, (p+1)//2)]]
	n = I()

	for i in range(n):
		l, r = LI()
		a = bisect_left(P,l)
		b = bisect_right(P,r)
		print(b-a)


if __name__ == '__main__':
	main()
Buggy python submission (verdict: Runtime Error):
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))

##### main #####



def main():
	x,y = LI()

	ans=
	while x<=y:
		x*=2
		ans+=1
		
	print(ans)

if __name__ == '__main__':
	main()

Fixed submission:
import math
import itertools
import heapq
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect, bisect_left, bisect_right
from collections import defaultdict, deque


# d = defaultdict(lambda: 0)
# setrecursionlimit(10**7)
# inf = float("inf")


##### stdin ####
def LM(t, r): return list(map(t, r))
def R(): return stdin.readline()
def RS(): return R().split()
def I(): return int(R())
def F(): return float(R())
def LI(): return LM(int,RS())
def LF(): return LM(float,RS())
def ONE_SL(): return list(input())
def ONE_IL(): return LM(int, ONE_SL())
def ALL_I(): return map(int, stdin)
def ALL_IL(): return LM(int,stdin)

##### tools #####
def ap(f): return f.append
def pll(li): print('\n'.join(LM(str,li)))
def pljoin(li, s): print(s.join(li))

##### main #####



def main():
	x,y = LI()

	ans=0
	while x<=y:
		x*=2
		ans+=1
		
	print(ans)

if __name__ == '__main__':
	main()
Buggy python submission (verdict: Runtime Error):
from heapq import heappush, heappop

def dijkstra(adjList, s):
    num = len(adjList)
    dist = [float("INF") for i in range(10)]
    queue = []
    heappush(queue,(0,s))
    dist[s] = 0
    
    while queue != []:
        v_cost, v = heappop(queue)
        if dist[v] < v_cost:
            continue
        for u_cost, u in adjList[v]:
            if u != v and u_cost + dist[v] < dist[u]:
                dist[u] = u_cost + dist[v]
                heappush(queue, (dist[u], u))
    return dist


from sys import stdin
def IL():return list(map(int, stdin.readline().split()))

h,w = IL()
G = [[] for i in range(10)]

for i in range(10):
    l = IL()
    for j in range(10):
        G[i].append((l[j],j))


dist = dijkstra(G, 8)
ans = 0
memo={}
for i in range(h):
    for i in IL():
        if abs(i) == 1:
          pass
        elif i in memo:
          ans += memo[i]
        else:
            t = dijkstra(G, i)[1] 
            memo[i] = t
            ans += t

print(ans)from heapq import heappush, heappop

def dijkstra(adjList, s):
    num = len(adjList)
    dist = [float("INF") for i in range(10)]
    queue = []
    heappush(queue,(0,s))
    dist[s] = 0
    
    while queue != []:
        v_cost, v = heappop(queue)
        if dist[v] < v_cost:
            continue
        for u_cost, u in adjList[v]:
            if u != v and u_cost + dist[v] < dist[u]:
                dist[u] = u_cost + dist[v]
                heappush(queue, (dist[u], u))
    return dist


from sys import stdin
def IL():return list(map(int, stdin.readline().split()))

h,w = IL()
G = [[] for i in range(10)]

for i in range(10):
    l = IL()
    for j in range(10):
        G[i].append((l[j],j))


dist = dijkstra(G, 8)
ans = 0
memo={}
for i in range(h):
    for i in IL():
        if abs(i) == 1:
          pass
        elif i in memo:
          ans += memo[i]
        else:
            t = dijkstra(G, i)[1] 
            memo[i] = t
            ans += t

print(ans)

Fixed submission:
from heapq import heappush, heappop

def dijkstra(adjList, s):
    num = len(adjList)
    dist = [float("INF") for i in range(10)]
    queue = []
    heappush(queue,(0,s))
    dist[s] = 0
    
    while queue != []:
        v_cost, v = heappop(queue)
        if dist[v] < v_cost:
            continue
        for u_cost, u in adjList[v]:
            if u != v and u_cost + dist[v] < dist[u]:
                dist[u] = u_cost + dist[v]
                heappush(queue, (dist[u], u))
    return dist


from sys import stdin
def IL():return list(map(int, stdin.readline().split()))

h,w = IL()
G = [[] for i in range(10)]

for i in range(10):
    l = IL()
    for j in range(10):
        G[i].append((l[j],j))


ans = 0
memo={}
for i in range(h):
    for i in IL():
        if abs(i) == 1:
          pass
        elif i in memo:
          ans += memo[i]
        else:
            t = dijkstra(G, i)[1] 
            memo[i] = t
            ans += t

print(ans)
Buggy python submission (verdict: Runtime Error):
from sys import stdin
def readLine_int_list():return list(map(int, stdin.readline().split()))


def main():
	c1 = readLine_int_list()
	c2 = readLine_int_list()
	if c1[0] == c2[-1] and c1[1] == c2[1] and c1[-1] == c2[0]:
		print("YES")
	else:
		print("NO")
        
if __name__ == "__main__": main()

Fixed submission:
def main():
	c1 = input()
	c2 = input()
	
	if c1[0] == c2[-1] and c1[1] == c2[1] and c1[-1] == c2[0]:
	    print("YES")
	else:
	    print("NO")
        
if __name__ == "__main__": main()

Buggy python submission (verdict: Runtime Error):
from sys import stdin
def readLine_int_list():return list(map(int, stdin.readline().split()))


def main():
	c1 = readLine_int_list()
	c2 = readLine_int_list()
	
	if c1[0] == c2[-1] and c1[1] == c2[1] and c1[-1] == c2[0]:
	    print("YES")
    else:
        print("NO")
        
if __name__ == "__main__": main()

Fixed submission:
def main():
	c1 = input()
	c2 = input()
	
	if c1[0] == c2[-1] and c1[1] == c2[1] and c1[-1] == c2[0]:
	    print("YES")
	else:
	    print("NO")
        
if __name__ == "__main__": main()
Buggy python submission (verdict: Runtime Error):
from sys import stdin, stdout
from collections import defaultdict
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))

import re

def main():
	_s = input()
	t = input()
	pattern=""
	for i in t:
	    pattern += "[?{}]".format(i)

	ans = ""
	f = False
	for i in range(len(_s)):
	    if _s[i] == '?' or _s[i] == t[0]:
	        tmp = _s[i:i+len(t)]
	        match = re.match(pattern , tmp)
	        if match:
	            f = True
	            ans += 'X'
	        else:
	            ans += 'a'
	    else:
	        ans += _s[i]
	
	
	for i in range(len(ans)):
	    if ans[i] == "X":
	       _ans = [_ for _ in ans]
	       _ans[i:i+len(t)] = t
	       for i in range(len(_ans)):
	           if _ans[i] == "X":
	               _ans[i] = 'a'
	       
	ans = ''.join(_ans)
	if f:
	    print(ans)
	else:
	    print("UNRESTORABLE")
	    
if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin, stdout
from collections import defaultdict
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readLine_int_list_reverse(): return list(map(int, stdin.readline().split())).reverse()
def readAll_int(): return list(map(int, stdin))
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readAll_str(): return list(map(str, stdin))

import re

def main():
	_s = input()
	t = input()
	pattern=""
	for i in t:
	    pattern += "[?{}]".format(i)
	l=[]
	ans = ""
	f = False
	ff = False
	for i in range(len(_s)):
	    if _s[i] == '?' or _s[i] == t[0]:
	        tmp = _s[i:i+len(t)]
	        match = re.match(pattern , tmp)
	        if match:
	            f = True
	            if _s[i] == t:
	                ans += _s[i]
	                ff = True
	            else:
	                ans += 'X'
	        else:
	            ans += 'a'
	    else:
	        ans += _s[i]
	    
# 	print(ans)
	for i in range(len(ans)):
	    if ans[i] == "X":
	       _ans = [_ for _ in ans]
	       
	       if ff:
	           l.append(''.join(_ans).replace("X","a"))
	       else:
	           _ans[i:i+len(t)] = t
	           for i in range(len(_ans)):
	               if _s[i] != '?':
	                   _ans[i] = _s[i]
	           l.append(''.join(_ans).replace("X","a"))
	       
	if f:
	    ans = ''.join(min(l))
	    print(ans)
	else:
	    print("UNRESTORABLE")
	    
if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from sys import stdin, stdout
from collections import defaultdict
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readAll_str(): return list(list(map(str,i.split())) for i in stdin.read().split("\n"))
def g_twoD_list(p,q): return [[0 for i in range(p)] for j in range(q)]


def main():
    h,w = readLine_int_list()
    a = g_twoD_list(h,w)
    b = defaultdict(lambda: 0)
    
    _h = 0
    _w = 0
    for l in readAll_str():
        for i in l:
            _w = 0
            for j in i:
                b[(_h,_w)] = j
                _w += 1
            _h += 1
            
    dir_b = [(-1,0), (1,0), (0,-1), (0,1), (-1,-1), (-1,1), (1,-1), (1,1)]
    for i in range(h):
        for j in range(w):
            cnt = 0
            for d in dir_b:
                r = b[(i+d[0], j+d[1])]
                if r == '#':
                    cnt += 1
            if b[(i,j)] == '.':
                b[(i,j)] = str(cnt)
    # print(b)
    for i in b:
        if 0 <= i[0] < h and 0 <= i[1] < w:
            a[i[0]][i[1]] = b[i]
            
    for i in a:
        print(''.join(i))
if __name__ == "__main__":
    main()


Fixed submission:
from sys import stdin, stdout
from collections import defaultdict
def readLine_str_list():return list(map(str, stdin.readline().split()))
def readLine_int_list():return list(map(int, stdin.readline().split()))
def readAll_str(): return list(list(map(str,i.split())) for i in stdin.read().split("\n"))
def g_twoD_list(p,q): return [[0 for i in range(p)] for j in range(q)]


def main():
    h,w = readLine_int_list()
    a = g_twoD_list(w,h)
    b = defaultdict(lambda: 0)
    _h = 0
    _w = 0
    for l in readAll_str():
        for i in l:
            _w = 0
            for j in i:
                b[(_h,_w)] = j
                _w += 1
            _h += 1
            
    dir_b = [(-1,0), (1,0), (0,-1), (0,1), (-1,-1), (-1,1), (1,-1), (1,1)]
    for i in range(h):
        for j in range(w):
            cnt = 0
            for d in dir_b:
                r = b[(i+d[0], j+d[1])]
                if r == '#':
                    cnt += 1
            if b[(i,j)] == '.':
                b[(i,j)] = str(cnt)
    # print(b)
    for i in b:
        if 0 <= i[0] < h and 0 <= i[1] < w:
            a[i[0]][i[1]] = b[i]
            
    for i in a:
        print(''.join(i))
if __name__ == "__main__":
    main()