Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def test():
    n = int(input())
    a = np.array([int(x) for x in input().split()])
    m = max(a)
    ans = 0
    for x in range(m+1):
        ans = max(ans,sum(a==x+1)+sum(a==x)+sum(a==x-1))
    print(ans)


if __name__ == "__main__":
    test()


Fixed submission:
import collections

def test():
    n = int(input())
    a = [int(x) for x in input().split()]
    b = collections.Counter(a)
    ans = 0
    for x in range(max(a)+1):
        ex = b[x]
        if x - 1 in b:
            ex += b[x-1]
        if x + 1 in b:
            ex += b[x+1]
        ans = max(ans,ex)

    print(ans)


if __name__ == "__main__":
    test()
Buggy python submission (verdict: Runtime Error):
def test():
    n = int(input())
    a = list(map(int,input().split()))
    a.sort()
    print(a)
    b = []
    i = 0
    while i + 1 < n:
        if a[i] == a[i+1]:
            b.append(a[i])
            i += 2
        else:
            i += 1
    print(b[-1]*b[-2])



if __name__ == "__main__":
    test()


Fixed submission:
def test():
    n = int(input())
    a = list(map(int,input().split()))
    a.sort()
    b = []
    i = 0
    while i + 1 < n:
        if a[i] == a[i+1]:
            b.append(a[i])
            i += 2
        else:
            i += 1
    print(b[-1]*b[-2] if len(b)>=2 else 0)


if __name__ == "__main__":
    test()
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
bnum = [n - bisect.bisect_right(c, b[i]) for i in range(n)]
ans = 0
for i in range(n):
  ai = a[i]
  ans += sum(bnum[bisect.bisect_right(b, ai):n])
print(ans)

Fixed submission:
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
ans = 0
for i in b:
  ai = bisect.bisect_left(a, i)
  ci = n - bisect.bisect_right(c, i)
  ans += ai*ci
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
bnum = [n - bisect.bisect_right(c, b[i]) for i in range(n)]
ans = 0
for i in range(n):
  ai = a[i]
  ans += sum(bnum[bisect.bisect_right(b, ai):n])
print(ans)

Fixed submission:
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
ans = 0
for i in b:
  ai = bisect.bisect_left(a, i)
  ci = n - bisect.bisect_right(c, i)
  ans += ai*ci
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
ans = 0
for i in range(n):
  ai = a[i]
  for j in range(bisect.bisect_right(b, ai), n):
    bj = b[j]
    ans += n - bisect.bisect_right(c, bj)
print(ans)

Fixed submission:
import bisect

n = int(input())
a = sorted(list(map(int, input().split())))
b = sorted(list(map(int, input().split())))
c = sorted(list(map(int, input().split())))
ans = 0
for i in b:
  ai = bisect.bisect_left(a, i)
  ci = n - bisect.bisect_right(c, i)
  ans += ai*ci
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for i in range(n)]
i = a[0]
num = [1, i]
for k in range(n):
    if 2 in num:
        print(len(num) - 1)
        break
    j = a[i-1]
    if j in num:
        print(-1)
        break
    num.append(j)
    if 2 in num:
        print(len(num) - 1)
        break
    i = a[j-1]
    if i in num:
        print(-1)
        break
    num.append(i)

Fixed submission:
n = int(input())
a = [int(input()) for i in range(n)]
i = a[0]
for j in range(n):
    if i == 2:
        print(j + 1)
        break
    i = a[i-1]
else:
	print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for i in range(n)]
i = a[0]
num = [1, i]
for j in range(n):
    if 2 in num:
        print(len(num) - 1)
        exit()
    i = a[i-1]
    if i in num:
        print(-1)
        exit()
    num.append(i)

Fixed submission:
n = int(input())
a = [int(input()) for i in range(n)]
i = a[0]
for j in range(n):
    if i == 2:
        print(j + 1)
        break
    i = a[i-1]
else:
	print(-1)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import functools

def cmp1(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (t1 - a0) - (t0 - a1)

def cmp2(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (a1 - t0) - (a0 - t1)

n = int(input())
AB = [tuple([i] + [int(x) for x in input().split()]) for i in range(n)]
# hey atcoder Python 3.4 is too old

A = sorted(AB, key=functools.cmp_to_key(cmp1))
B = sorted(AB, key=functools.cmp_to_key(cmp2))
used = {-1}
a, b = -1, -1
ans = 0

for _ in range(n // 2):
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

    while b in used:
        b, _, s = B.pop(0)
    used.add(b)
    ans -= s

if n % 2 == 1:
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

print(ans)


Fixed submission:
#!/usr/bin/env python3

n = int(input())
A = [tuple(int(x) for x in input().split()) for i in range(n)]
A.sort(key=lambda ab: -(ab[0] + ab[1]))
print(sum(a for a, _ in A[::2]) - sum(b for _, b in A[1::2]))

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3

import functools

def cmp1(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (t1 - a0) - (t0 - a1)

def cmp2(p0, p1):
    (_, t0, a0), (_, t1, a1) = p0, p1
    return (a1 - t0) - (a0 - t1)

n = int(input())
AB = [tuple((i, *(int(x) for x in input().split()))) for i in range(n)]

A = sorted(AB, key=functools.cmp_to_key(cmp1))
B = sorted(AB, key=functools.cmp_to_key(cmp2))
used = {-1}
a, b = -1, -1
ans = 0

for _ in range(n // 2):
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

    while b in used:
        b, _, s = B.pop(0)
    used.add(b)
    ans -= s

if n % 2 == 1:
    while a in used:
        a, s, _ = A.pop(0)
    used.add(a)
    ans += s

print(ans)


Fixed submission:
#!/usr/bin/env python3

n = int(input())
A = [tuple(int(x) for x in input().split()) for i in range(n)]
A.sort(key=lambda ab: -(ab[0] + ab[1]))
print(sum(a for a, _ in A[::2]) - sum(b for _, b in A[1::2]))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

d, g = [int(x) for x in input()[:-2].split()]
pc = [[int(x) for x in input()[:-2].split()] for _ in range(d)]

def dfs(choice):
    if len(choice) == d:
        if sum(x * (i + 1) + (ci if x == pi else 0) for i, (x, (pi, ci)) in enumerate(zip(choice, pc))) >= g:
            return sum(choice)
        else:
            return sys.maxsize
    else:
        return min(dfs(choice[:] + [x]) for x in range(pc[len(choice)][0] + 1))

print(dfs([]))


Fixed submission:
import itertools
import math
import sys

d, g = [int(x) for x in input()[:-2].split()]
pc = [[int(x) for x in input()[:-2].split()] for _ in range(d)]

def search(choice):
    total = sum(((i + 1) * p + c) * chosen for i, ((p, c), chosen) in enumerate(zip(pc, choice)))
    count = sum(p * chosen for (p, _), chosen in zip(pc, choice))

    if total < g:
        partial = d - list(reversed(choice)).index(0)
        partial_count = math.ceil((g - total) / partial)
        if partial_count <= pc[partial - 1][0]:
            count += partial_count
        else:
            count = sys.maxsize

    return count

print(min(search(choice) for choice in itertools.product(*([0, 1] for _ in range(d)))))

Buggy python submission (verdict: Time Limit Exceeded):
import sys

d, g = [int(x) for x in input()[:-2].split()]
pc = [[int(x) for x in input()[:-2].split()] for _ in range(d)]

def dfs(choice):
    if len(choice) == d:
        if sum(x * (i + 1) + (ci if x == pi else 0) for i, (x, (pi, ci)) in enumerate(zip(choice, pc))) >= g:
            return sum(choice)
        else:
            return sys.maxsize
    else:
        return min(dfs(choice[:] + [x]) for x in range(pc[len(choice)][0] + 1))

print(dfs([]))


Fixed submission:
import itertools
import math
import sys

d, g = [int(x) for x in input()[:-2].split()]
pc = [[int(x) for x in input()[:-2].split()] for _ in range(d)]

def search(choice):
    total = sum(((i + 1) * p + c) * chosen for i, ((p, c), chosen) in enumerate(zip(pc, choice)))
    count = sum(p * chosen for (p, _), chosen in zip(pc, choice))

    if total < g:
        partial = d - list(reversed(choice)).index(0)
        partial_count = math.ceil((g - total) / partial)
        if partial_count <= pc[partial - 1][0]:
            count += partial_count
        else:
            count = sys.maxsize

    return count

print(min(search(choice) for choice in itertools.product(*([0, 1] for _ in range(d)))))
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

x = int(input())
S = {0}
i = 0
while x not in S:
    i += 1
    S = S | {s - i for s in S} | {s + i for s in S}
print(i)


Fixed submission:
#!/usr/bin/env python3

import math

print(math.ceil((-1 + (8 * int(input()) + 1) ** 0.5) / 2))
Buggy python submission (verdict: Runtime Error):
import itertools

n, m = [int(x) for x in input().split()]
edges = set(tuple(int(x) for x in input().split()) for _ in range(m))
print(sum(1 if all(p in edges or tuple(reversed(p)) in edges for p in [(1, path[0]), *zip(path, path[1:])]) else 0 for path in itertools.permutations(range(2, n + 1))))


Fixed submission:
import itertools

n, m = [int(x) for x in input().split()]
edges = set(tuple(int(x) for x in input().split()) for _ in range(m))
print(sum(1 if all(p in edges or tuple(reversed(p)) in edges for p in [(1, path[0])] + list(zip(path, path[1:]))) else 0 for path in itertools.permutations(range(2, n + 1))))
Buggy python submission (verdict: Time Limit Exceeded):
import collections

class UnionFind:
    def __init__(self, n):
        self._id = list(range(n))
        self._sz = [1] * n

    def _root(self, i):
        j = i
        while (j != self._id[j]):
            self._id[j] = self._id[self._id[j]]
            j = self._id[j]
        return j

    def find(self, p, q):
        return self._root(p) == self._root(q)

    def union(self, p, q):
        i = self._root(p)
        j = self._root(q)
        if (self._sz[i] < self._sz[j]):
            self._id[i] = j
            self._sz[j] += self._sz[i]
        else:
            self._id[j] = i
            self._sz[i] += self._sz[j]

n, k, l = [int(x) for x in input().split()]
pqs = set(tuple(int(x) - 1 for x in input().split()) for _ in range(k))
rss = set(tuple(int(x) - 1 for x in input().split()) for _ in range(l))

road = UnionFind(n)
for pq in pqs:
    road.union(*pq)

rail = UnionFind(n)
for rs in rss:
    rail.union(*rs)

roadset = collections.defaultdict(set)
railset = collections.defaultdict(set)
for i in range(n):
    roadset[road._root(i)].add(i)
    railset[rail._root(i)].add(i)

print(' '.join([str(len(roadset[road._root(i)] & railset[rail._root(i)])) for i in range(n)]))


Fixed submission:
import collections

class UnionFind:
    def __init__(self, n):
        self._id = list(range(n))
        self._sz = [1] * n

    def _root(self, i):
        j = i
        while (j != self._id[j]):
            self._id[j] = self._id[self._id[j]]
            j = self._id[j]
        return j

    def find(self, p, q):
        return self._root(p) == self._root(q)

    def union(self, p, q):
        i = self._root(p)
        j = self._root(q)
        if (self._sz[i] < self._sz[j]):
            self._id[i] = j
            self._sz[j] += self._sz[i]
        else:
            self._id[j] = i
            self._sz[i] += self._sz[j]

n, k, l = [int(x) for x in input().split()]
pqs = set(tuple(int(x) - 1 for x in input().split()) for _ in range(k))
rss = set(tuple(int(x) - 1 for x in input().split()) for _ in range(l))

road = UnionFind(n)
for pq in pqs:
    road.union(*pq)

rail = UnionFind(n)
for rs in rss:
    rail.union(*rs)

roadrail = [(road._root(i), rail._root(i)) for i in range(n)]
cnt = collections.Counter(roadrail)

print(' '.join([str(cnt[r]) for r in roadrail]))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
P = [int(x) for x in input().split()]

b = [None] * (n + 1)
b[P[0]] = 1

for p, pprev in zip(P[1:], P):
    bp = b[pprev] + (pprev - p) * 20000 + 1
    while bp in b:
        bp += 1
    b[p] = bp

offset = 1 - min(b[1:])
b = [i + offset for i in b[1:]]

print(' '.join([str(20000 * i) for i in range(1, n + 1)]))
print(' '.join([str(i) for i in b]))


Fixed submission:
n = int(input())
P = [int(x) for x in input().split()]

b = [None] * (n + 1)
b[P[0]] = 1
bs = {1}

for p, pprev in zip(P[1:], P):
    bp = b[pprev] + (pprev - p) * 20000 + 1
    while bp in bs:
        bp += 1
    b[p] = bp
    bs.add(bp)

offset = 1 - min(b[1:])
b = [i + offset for i in b[1:]]

print(' '.join([str(20000 * i) for i in range(1, n + 1)]))
print(' '.join([str(i) for i in b]))
Buggy python submission (verdict: Time Limit Exceeded):
import collections

input()
a = [int(x) for x in input().split()]
print(max(collections.Counter([max(a[i + 1:]) - x for i, x in enumerate(a[:-1])]).items())[1])


Fixed submission:
input()
a = [int(x) for x in input().split()]

lowest = a[0]
lowests = {a[0]}
diff = -1
pairs = []

for x in a[1:]:
    if lowest > x:
        lowests = {x}
        lowest = x
    elif lowest == x:
        lowests.add(x)
    elif diff == x - lowest:
        pairs += [(x, y) for y in lowests]
    elif diff < x - lowest:
        pairs = [(x, y) for y in lowests]
        diff = x - lowest

a, b = zip(*pairs)
print(min(len(set(a)), len(set(b))))
Buggy python submission (verdict: Time Limit Exceeded):
def solve(a, b, c):
    sm = 0
    while True:
        if (c - b * sm) % (a - b) == 0:
            x = (c - b * sm) // (a - b)
            if 0 <= x <= sm:
                return x, sm - x
        sm += 1

n = int(input())
t, a = [int(x) for x in input().split()]
for i in range(1, n):
    t2, a2 = [int(x) for x in input().split()]
    x, y = solve(a2, -t2, a * t2 - t * a2)
    t, a = t + x, a + y

print(t + a)


Fixed submission:
import math
from fractions import Fraction

n = int(input())
t, a = [int(x) for x in input().split()]
for i in range(1, n):
    t2, a2 = [int(x) for x in input().split()]
    x = math.ceil(max(Fraction(t, t2), Fraction(a, a2)))
    t, a = x * t2, x * a2

print(t + a)

Buggy python submission (verdict: Time Limit Exceeded):
def solve(a, b, c):
    sm = 0
    while True:
        for x in range(sm + 1):
            if x * a + (sm - x) * b == c:
                return x, (sm - x)
        sm += 1

n = int(input())
t, a = [int(x) for x in input().split()]
for i in range(1, n):
    t2, a2 = [int(x) for x in input().split()]
    x, y = solve(a2, -t2, a * t2 - t * a2)
    t, a = t + x, a + y

print(t + a)


Fixed submission:
import math
from fractions import Fraction

n = int(input())
t, a = [int(x) for x in input().split()]
for i in range(1, n):
    t2, a2 = [int(x) for x in input().split()]
    x = math.ceil(max(Fraction(t, t2), Fraction(a, a2)))
    t, a = x * t2, x * a2

print(t + a)
Buggy python submission (verdict: Runtime Error):
n, l = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
dp = [[None] * n for _ in range(n)]

Aacc = [A[0]]
for i in range(1, n):
    Aacc.append(Aacc[i - 1] + A[i])

def length(a, b):
    return Aacc[b] - (Aacc[a - 1] if a > 0 else 0)

def search(a, b):
    if dp[a][b] is None:
        if a == b:
            ans = []
        elif length(a, b) >= l:
            for cut in range(a, b):
                left = search(a, cut)
                if left is not False:
                    right = search(cut + 1, b)
                    if right is not False:
                        ans = [cut] + left + right
                        break
            else:
                ans = False
        else:
            ans = False

        dp[a][b] = ans

    return dp[a][b]

ans = search(0, n - 1)
if ans is False:
    print('Impossible')
else:
    print('Possible')
    for a in ans:
        print(a + 1)


Fixed submission:
a, b = [int(x) for x in input().split()]

if a > 0:
    print('Positive')
elif a <= 0 and 0 <= b:
    print('Zero')
elif (b - a) % 2 == 0:
    print('Negative')
else:
    print('Positive')
Buggy python submission (verdict: Time Limit Exceeded):
C = int(1e9) + 7
h, w, a, b = [int(x) for x in input().split()]

# 1000000005 = 2^29 + 2^28 + 2^27 + 2^25 + 2^24 + 2^23 + 2^20 + 2^19 + 2^17 + 2^15 + 2^14 + 2^11 + 2^9 + 2^2 + 1

def pow(n):
    ans = n
    for i in range(1, 30):
        n = (n * n) % C
        if i in {29, 28, 27, 25, 24, 23, 20, 19, 17, 15, 14, 11, 9, 2}:
            ans = (ans * n) % C
    return ans

fact = [1]
factinv = [1] # dummy
for x in range(1, h + w - 2):
    fact.append((x * fact[x - 1]) % C)
    # factinv.append(pow(x)) # <- this won't work, why?
    factinv.append((pow(x) * factinv[x - 1]) % C)

def nCr(n, r):
    return (fact[n] * factinv[r] * factinv[n - r]) % C

ans = 0
for i in range(b, w):
    ans = (ans + nCr(i + h - a - 1, h - a - 1) * nCr(a - 1 + w - i - 1, a - 1)) % C

print(ans)


Fixed submission:
C = int(1e9) + 7
h, w, a, b = [int(x) for x in input().split()]

# 1000000005 = 2^29 + 2^28 + 2^27 + 2^25 + 2^24 + 2^23 + 2^20 + 2^19 + 2^17 + 2^15 + 2^14 + 2^11 + 2^9 + 2^2 + 1

def pow(n):
    ans = n
    for i in range(1, 30):
        n = (n * n) % C
        if i in {29, 28, 27, 25, 24, 23, 20, 19, 17, 15, 14, 11, 9, 2}:
            ans = (ans * n) % C
    return ans

fact = [1]
factinv = [1] # dummy
for x in range(1, h + w - 2):
    fact.append((x * fact[x - 1]) % C)
    # factinv.append(pow(x)) # <- this won't work, why?
    factinv.append((pow(x) * factinv[x - 1]) % C)

def nCr(n, r):
    return (fact[n] * factinv[r] * factinv[n - r]) % C

ans = 0
for i in range(b, w):
    ans = (ans + nCr(i + h - a - 1, h - a - 1) * nCr(a - 1 + w - i - 1, a - 1)) % C

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
num = int(input())
i = list(map(int, input().split())) 
#b=min(i)-num
#b=max(i)-1

b=min(i)-num
mini = float('inf')
while(b!=max(i)):
  sum=0
  for j  in range(num):
     sum+=abs(i[j]-(b+j+1))
  mini = min(sum,mini)
  b+=1
  
print(mini)

Fixed submission:
num = int(input())
list_i = list(map(int, input().split())) 
#b=min(i)-num
#b=max(i)-1
for j in range(num):
  list_i[j]=list_i[j]-j-1
list_i.sort()
b=list_i[int(num/2)]
sum=0
for i in range(num):
  sum +=abs(list_i[i]-b)
print(sum)
Buggy python submission (verdict: Runtime Error):
L, R = map(int,input().split())
print((100**L)*(int((R-1)/99)+R)

Fixed submission:
L, R = map(int,input().split())
print((100**L)*(int((R-1)/99)+R))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))

dp = [float("inf") for _ in range(N)]
dp[0] = 0
for i in range(N - 1):
    for j in range(1, min(K + 1, N - i)):
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))
print(dp[N - 1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))

dp = [float("inf") for _ in range(N)]
dp[0] = 0
for i in range(N - 1):
    for j in range(1, min(K + 1, N - i)):
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]))
print(dp[N - 1])
Buggy python submission (verdict: Time Limit Exceeded):
height, width = map(int, input().split(" "))
target = []
queue = []
WHITE = "."
BLACK = "#"
for h in range(height):
    temp = [n for n in input()]
    for w in range(width):
        if temp[w] == BLACK:
            queue.append([w, h])
    target.append(temp)
all_move = [[0, 1], [0, -1], [1, 0], [-1, 0]]

answer = 0
while queue:
    next_queue = []
    for q in queue:
        here_x, here_y = q
        for x, y in all_move:
            next_x, next_y = here_x + x, here_y + y
            if 0 <= next_x < width and 0 <= next_y < height:
                if target[next_y][next_x] == WHITE:
                    next_queue.append([next_x, next_y])
                    target[next_y][next_x] = BLACK
    if not next_queue:
        print(answer)
        break
    queue = next_queue
    answer += 1
                    

Fixed submission:
height, width = map(int, input().split(" "))
target = []
queue = []
WHITE = "."
BLACK = "#"
for h in range(height):
    temp = [n for n in input()]
    for w in range(width):
        if temp[w] == BLACK:
            queue.append([w, h])
    target.append(temp)
all_move = [[0, 1], [0, -1], [1, 0], [-1, 0]]

answer = 0
while queue:
    next_queue = []
    for q in queue:
        here_x, here_y = q
        for x, y in all_move:
            next_x, next_y = here_x + x, here_y + y
            if 0 <= next_x < width and 0 <= next_y < height:
                if target[next_y][next_x] == WHITE:
                    next_queue.append([next_x, next_y])
                    target[next_y][next_x] = BLACK
    if not next_queue:
        print(answer)
        break
    queue = next_queue
    answer += 1
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict

nesw = [[0, 1], [0, -1], [1, 0], [-1, 0]]
height, width = map(int, input().split(" "))
root = []
black = 0
for i in range(height):
    temp = [n for n in input()]
    root.append(temp)
    black += temp.count("#")
start_y, start_x = 0, 0
end_y, end_x = height - 1, width - 1

queue = [(start_y, start_x)]
used_white = 1
flag = 0
while True:
    new_queue = []
    used_white += 1
    for q in queue:
        h, w = q
        for t in nesw:
            new_h, new_w = h + t[0], w + t[1]
            if 0 <= new_h < height and 0 <= new_w < width:
                if new_h == end_y and new_w == end_x:
                    flag = 1
                    break
                elif root[new_h][new_w] != "#":
                    root[new_h][new_w] = "#"
                    new_queue.append((new_h, new_w))
    if flag:
        break
    queue = new_queue

print(height * width - black - used_white)


Fixed submission:
import sys
from collections import defaultdict

nesw = [[0, 1], [0, -1], [1, 0], [-1, 0]]
height, width = map(int, input().split(" "))
root = []
black = 0
for i in range(height):
    temp = [n for n in input()]
    root.append(temp)
    black += temp.count("#")
start_y, start_x = 0, 0
end_y, end_x = height - 1, width - 1

queue = [(start_y, start_x)]
used_white = 1
flag = 0
while queue:
    new_queue = []
    used_white += 1
    for q in queue:
        h, w = q
        for t in nesw:
            new_h, new_w = h + t[0], w + t[1]
            if 0 <= new_h < height and 0 <= new_w < width:
                if new_h == end_y and new_w == end_x:
                    flag = 1
                    break
                elif root[new_h][new_w] != "#":
                    root[new_h][new_w] = "#"
                    new_queue.append((new_h, new_w))
    if flag:
        break
    queue = new_queue
if flag:
    print(height * width - black - used_white)
else:
    print(-1)