Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import numpy as np
X,Y,A,B,C = list(map(int, input().split()))
P = np.sort(np.array(list(map(int, input().split()))))
Q = np.sort(np.array(list(map(int, input().split()))))
R = np.sort(np.array(list(map(int, input().split()))))

label = np.array([1] * Q.shape[0] + [0] * R.shape[0])
QR = np.concatenate([Q,R])
QR = np.concatenate([label.reshape(1,-1), QR.reshape(1,-1)], axis=0)
QR = QR[:,np.argsort(QR[1,:])]

points = []
for k in range(0,X+1):
  if QR[0,:][-(X+Y-k):].sum() > Y:
    if X-k > C:
      points.append(0)
    else:
      if k == 0:
        score = Q[-Y:].sum()+R[-(X-k):].sum()
      else:
        score = Q[-Y:].sum()+R[-(X-k):].sum() + P[-k:].sum()
      points.append(score)

  else:
    if QR.shape[1] < X+Y-k:
      points.append(0)
    else:
      if k == 0:
        score = QR[1,-(X+Y-k):].sum()
      else:
        score = QR[1,-(X+Y-k):].sum() + P[-k:].sum()
      points.append(score)
print(max(points))

Fixed submission:
# coding: utf-8
X,Y,A,B,C = list(map(int, input().split()))
P = sorted(list(map(int, input().split())))[-X:]
Q = sorted(list(map(int, input().split())))[-Y:]
R = list(map(int, input().split()))

total = sorted(P + Q + R)[-X-Y:]
print(sum(total))
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
import itertools
A = [0]
N, S = list(map(int, input().split()))
A = A + list(map(int, input().split()))   
val = 0
for L in range(1, N+1):
  for R in range(L, N+1):
    A_ = A[L:R+1]
    lists = []
    for n in range(len(A_)):
      for comb in itertools.combinations(A_, n+1):
        lists.append(list(comb))
    for lis in lists:
      _sum = sum(lis)
      if _sum == S:
        val += 1
      if val == 998244353:
        val = 0
print(val)


Fixed submission:
N, M = input().split()
N, M = int(N), int(M)
if N < 2:
  print(int(M*(M-1)/2))
elif M < 2:
  print(int(N*(N-1)/2))
else:
  print(int(N*(N-1)/2 + M*(M-1)/2))

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
N = int(input())
A = input().split()
for k in range(N):
  A_ = A.copy()
  del A_[k]
  ans = 0
  while len(A_) > 0:
    val = A_[0]
    counts = A_.count(val)
    ans += int(counts * (counts - 1) / 2)
    while True:
      try:
        A_.remove(val)
      except:
        break

  print('{}'.format(ans))

Fixed submission:
N, M = input().split()
N, M = int(N), int(M)
if N < 2:
  print(int(M*(M-1)/2))
elif M < 2:
  print(int(N*(N-1)/2))
else:
  print(int(N*(N-1)/2 + M*(M-1)/2))

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
N = int(input())
A = input().split()
for k in range(N):
  A_ = A.copy()
  del A_[k]
  ans = 0
  while len(A_) > 0:
    val = A_[0]
    counts = A_.count(val)
    ans += int(counts * (counts - 1) / 2)
    while True:
      try:
        A_.remove(val)
      except:
        break

  print('{}'.format(ans))

Fixed submission:
N, M = input().split()
N, M = int(N), int(M)
if N < 2:
  print(int(M*(M-1)/2))
elif M < 2:
  print(int(N*(N-1)/2))
else:
  print(int(N*(N-1)/2 + M*(M-1)/2))

Buggy python submission (verdict: Runtime Error):
L = int(input())
print((L/3)**3)


Fixed submission:
N, M = input().split()
N, M = int(N), int(M)
if N < 2:
  print(int(M*(M-1)/2))
elif M < 2:
  print(int(N*(N-1)/2))
else:
  print(int(N*(N-1)/2 + M*(M-1)/2))
Buggy python submission (verdict: Runtime Error):
L = input()
print((L/3)**3)

Fixed submission:
# coding: utf-8
L = int(input())
print((L/3)**3.0)
Buggy python submission (verdict: Runtime Error):
S = input()
flg = False
if len(S) < 2:
  print('No')
else:
  for i in range(len(S)-1):
    if S[i:i+2] = 'hi':
      print('Yes')
      flg = True
      break
  if not flg:
    print('No')
  

Fixed submission:
S = input()
flg = True
if len(S)%2 == 1:
  print('No')
else:
  for i in range(0, len(S), 2):
    if S[i:i+2] != 'hi':
      print('No')
      flg = False
      break
  if flg:
    print('Yes')
Buggy python submission (verdict: Runtime Error):
S = list(input())
if S[0] == S[1] & S[1] == S[2]:
  print('No')
else:
  print("Yes")

Fixed submission:
S = list(input())
if (S[0] == S[1]) & (S[1] == S[2]):
  print('No')
else:
  print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
N,P = list(map(int, input().split()))
S = '0'+input()

dp = [[0 for _ in range(P)] for i in range(N+1)]
mod_tab = [[0 for _ in range(10)] for i in range(P)]

for p in range(P):
    for a in range(10):
        mod_tab[p][a] = (p*10+a)%P

dp[1][int(S[1])%P]=1
for i in range(2,N+1):
    dp[i][int(S[i])%P] += 1
    for p in range(P):
        target = mod_tab[p][int(S[i])]
        dp[i][target] += dp[i-1][p]
sum=0
for i in range(1,N+1):
    sum+=dp[i][0]
print(sum)

Fixed submission:
# coding: utf-8
import numpy as np
N,P = list(map(int, input().split()))
S = '0'+input()

#N=200000
#P=9893
#S= '0'+'1234567890' * 20000

if (P==2)|(P==5):
    sum=0
    for i in range(N, 0, -1):
        if int(S[i]) % P == 0:
            sum+= i
    print(sum)


else:
    dp1 = np.zeros(N + 1)
    dp2 = np.zeros(N + 1)
    dp3 = np.zeros(P)
    dp1[N] = int(S[N]) % P
    dp3[int(dp1[N])] += 1
    k = 1
    for i in range(N - 1, 0, -1):
        dp1[i] = (dp1[i + 1] + (int(S[i]) * 10 * k)) % P
        dp3[int(dp1[i])] += 1
        k = (k * 10) % P

    sum = 0

    for i in range(P):
        if i == 0:
            sum += (dp3[i] * (dp3[i] - 1)) // 2 + dp3[i]
            continue
        if dp3[i] == 0: continue
        sum += (dp3[i] * (dp3[i] - 1)) // 2
    print(int(sum))
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
from itertools import combinations, product
#import bisect# lower_bound etc
#import numpy as np
def run():
    r1, c1, r2, c2 = map(int, read().split())
    mod = 10 ** 9 + 7

    def generate_inv(n, mod):
        """
        逆元行列
        n >= 2
        Note: mod must bwe a prime number
        """
        ret = [0, 1]
        for i in range(2, n + 1):
            next = -ret[mod % i] * (mod // i)
            next %= mod
            ret.append(next)
        return ret

    inv = generate_inv(2 * 10 ** 6, mod)

    def comb_mod2(n, a, mod):
        """
        return: [n, a] % mod
        Note: mod must be a prime number
        """
        if a == 0 or n == 0:
            return 1
        ret = 1
        for i in range(1, a + 1):
            ret *= n - i + 1
            ret %= mod
            ret *= inv[i]
            ret %= mod
        return ret


    def calc(i,j):
        ret = 0
        agg = comb_mod2(j, 0, mod)
        for k in range(1, i+2):
            agg *= (j+k)%mod
            agg *= inv[k]
            agg %= mod
            ret += agg
        return ret


    ret = 0
    ret += calc(r2, c2)
    ret -= calc(r2, c1-1)
    ret -= calc(r1-1, c2)
    ret += calc(r1-1, c1-1)
    ret %= mod
    print(ret)

if __name__ == "__main__":
    run()

Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
from itertools import combinations, product
#import bisect# lower_bound etc
#import numpy as np
def run():
    r1, c1, r2, c2 = map(int, read().split())
    mod = 10 ** 9 + 7

    def generate_inv(n, mod):
        """
        逆元行列
        n >= 2
        Note: mod must bwe a prime number
        """
        ret = [0, 1]
        for i in range(2, n + 1):
            next = -ret[mod % i] * (mod // i)
            next %= mod
            ret.append(next)
        return ret

    inv = generate_inv(10 ** 6 + 1, mod)



    def calc(i,j):
        ret = 0
        agg = 1
        for k in range(1, i+2):
            agg *= (j+k)%mod
            agg *= inv[k]
            agg %= mod
            ret += agg
        return ret


    ret = 0
    ret += calc(r2, c2)
    ret -= calc(r2, c1-1)
    ret -= calc(r1-1, c2)
    ret += calc(r1-1, c1-1)
    ret %= mod
    print(ret)

if __name__ == "__main__":
    run()
Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque

def run():
    N = input()
    S = input()
    dp = [0] * 1000
    ans = 0
    for n in S:
        n = int(n)
        #print(dp)
        for i in range(1000):
            if i // 100 == n and dp[i] == 0:
                dp[i] = 1
                #print(f'{i} -> 1')
            elif (i // 10) % 10 == n and dp[i] == 1:
                dp[i] = 2
                #print(f'{i} -> 2')
            elif i % 10 == n and dp[i] == 2:
                dp[i] = 3
                #print(f'{i} -> 3')
                ans += 1
    print(ans)

    #print(factorials)
if __name__ == "__main__":
    run()

Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque

def run():
    N = input()
    S = input()
    dp = [0] * 1000
    ans = 0
    for n in S:
        n = int(n)
        #print(dp)
        for i in range(1000):
            if i // 100 == n and dp[i] == 0:
                dp[i] = 1
                #print(f'{i} -> 1')
            elif (i // 10) % 10 == n and dp[i] == 1:
                dp[i] = 2
                #print(f'{i} -> 2')
            elif i % 10 == n and dp[i] == 2:
                dp[i] = 3
                #print(f'{i} -> 3')
                ans += 1
    print(ans)

    #print(factorials)
if __name__ == "__main__":
    run()
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
# from heapq import heappop, heappush
# from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
# import math
# from itertools import product, accumulate, combinations, product
# import bisect
# import numpy as np
# from copy import deepcopy
from collections import deque

# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())

def run():
    H, W = mapline()
    A = []
    for i in range(H):
        A.append(list(mapline()))

    output = []
    for i in range(H):
        for j in range(W - 1):
            if A[i][j] % 2:
                A[i][j] -= 1
                A[i][j + 1] += 1
                output.append((i, j, i, j + 1))
    for i in range(H - 1):
        if A[i][H - 1] % 2:
            A[i][H - 1] -= 1
            A[i + 1][H - 1] += 1
            output.append((i, H - 1, i + 1, H - 1))

    print(len(output))
    for i1, j1, i2, j2 in output:
        print(f'{i1 + 1} {j1 + 1} {i2 + 1} {j2 + 1}')


if __name__ == "__main__":
    run()


Fixed submission:
# coding: utf-8
import sys

# from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
printout = sys.stdout.write
sprint = sys.stdout.flush
# from heapq import heappop, heappush
# from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
# import math
# from itertools import product, accumulate, combinations, product
# import bisect
# import numpy as np
# from copy import deepcopy
from collections import deque

# from decimal import Decimal
# from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7


def intread():
    return int(sysread())
def mapline(t=int):
    return map(t, sysread().split())
def mapread(t=int):
    return map(t, read().split())

def run():
    H, W = mapline()
    A = []
    for i in range(H):
        A.append(list(mapline()))

    output = []
    for i in range(H):
        for j in range(W - 1):
            if A[i][j] % 2:
                A[i][j] -= 1
                A[i][j + 1] += 1
                output.append((i, j, i, j + 1))
    for i in range(H - 1):
        if A[i][W - 1] % 2:
            A[i][W - 1] -= 1
            A[i + 1][W - 1] += 1
            output.append((i, W - 1, i + 1, W - 1))

    print(len(output))
    for i1, j1, i2, j2 in output:
        print(f'{i1 + 1} {j1 + 1} {i2 + 1} {j2 + 1}')


if __name__ == "__main__":
    run()
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8


def run():
    n, *A = map(int, read().split())
    v = 0
    acum = []
    for a in A:
        v += a
        acum.append(v)
    # greedy
    cums = 0
    count = 0
    V = A[0] // abs(A[0])

    for a in acum[1:]:
        #print(a, '---------')
        V *= -1
        if (a + cums) * V > 0:
            continue
        else:
            update = abs(a + cums) + 1
            cums += (update) * V
            count += update
            #print(V, cums, count)
    print(count)



if __name__ == "__main__":
    run()


Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8


def run():
    n, *A = map(int, read().split())
    v = 0
    acum = []
    for a in A:
        v += a
        acum.append(v)

    # greedy
    ans = INF
    for V in (-1, 1):
        cums = 0
        count = 0
        for a in acum:
            #print(a, '---------')
            V *= -1
            if (a + cums) * V > 0:
                continue
            else:
                update = abs(a + cums) + 1
                cums += (update) * V
                count += update
                #print(V, cums, count)
        ans = min(ans, count)
    print(ans)



if __name__ == "__main__":
    run()
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def dfs(seq):
    if seq in ['dreamer', 'dream', 'erase', 'eraser']:
        return True

    if seq[:7] == 'dreamer':
        if dfs[5:] or dfs[7:]:
            return True
        else:
            return False

    elif seq[:5] == 'dream':
        return dfs(seq[5:])

    elif seq[:6] == 'eraser':
        return dfs(seq[6:])

    elif seq[:5] == 'erase':
        return dfs(seq[5:])
    else:
        return False


def run():
    S = input()
    if dfs(S):
        print('YES')
    else:
        print('NO')


if __name__ == "__main__":
    run()

Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque

INF = 1 << 50

def dfs(n,seq):
    if seq[n:] in ['dreamer', 'dream', 'erase', 'eraser']:
        return True

    if seq[n:n+7] == 'dreamer':
        if dfs(n+5, seq):
            return True
        elif dfs(n+7, seq):
            return True
        else:
            return False

    elif seq[n:n+5] == 'dream':
        return dfs(n+5, seq)

    elif seq[n:n+6] == 'eraser':
        return dfs(n+6, seq)

    elif seq[n:n+5] == 'erase':
        return dfs(n+5, seq)
    else:
        return False


def run():
    S = input()
    if dfs(0,S):
        print('YES')
    else:
        print('NO')


if __name__ == "__main__":
    run()
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque


def run():
    a,b,x = map(int, input().split())
    ans = 0
    if a == 0:
        a += 1
        ans = 1
    ans += b // x - (a-1) // x
    print(ans)

if __name__ == "__main__":

Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque


def run():
    a,b,x = map(int, input().split())
    ans = 0
    if a == 0:
        a += 1
        ans = 1
    ans += b // x - (a-1) // x
    print(ans)

if __name__ == "__main__":
    run()
Buggy python submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
from collections import deque
#from decimal import Decimal

INF = 1 << 50

def run():
    K, T, *A = map(int, read().split())
    A = sorted(A)
    A = deque(A)
    high, low = 0, 0
    last = None
    while A:
        #print(A)
        if high == 1:
            A.appendleft(1)
            high = A.pop()

        if not high:
            high = A.pop()
            if high == 1:break
        elif not low:
            low = A.popleft()
        #print(high, low)       
        
        val = min(low, high-1)
        if not val:
            last = None
        else:
            last = 'LOW'
        high -= val
        low -= val
        #print('-->', high, low)
    if high == 1
        if last == None:
            print(max(low-high-1, 0))
            return
        else:
            print(max(low - 1, 0)))
            return
    print(high - 1 + max(low - 1, 0))

if __name__ == "__main__":
    run()

Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
from collections import deque
#from decimal import Decimal

INF = 1 << 50

def run():
    K, T, *A = map(int, read().split())
    max_a = max(A)
    print(max(max_a-1-K+max_a, 0))

if __name__ == "__main__":
    run()
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
lis = list(map(int,input().split()))
num = 0
for i in range(n):
  while lis[i] % 2 == 0:
    num += 1
    lis[i] // 2
print(num)

Fixed submission:
n = int(input())
lis = list(map(int,input().split()))
num = 0
for nu in lis:
  while nu % 2 == 0:
    num += 1
    nu = nu // 2
print(num)
Buggy python submission (verdict: Runtime Error):
a,b = map(int,input())
if a != 1:
  a -= 2
if b != 1:
  b -= 2
print(a * b)

Fixed submission:
a,b = map(int,input().split())
if a != 1:
  a -= 2
if b != 1:
  b -= 2
print(a * b)
Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

char S[20010];
int main() {
  cin >> S;
  int len = strlen(S);
  int sum=0;
  for(int i=0; i<len; i++) {
    sum += S[i]-'0';
  }
  if(sum%9==0) cout << "Yes" << endl;
  else cout << "No" << endl;
}

Fixed submission:
N = int(input()) 
if N%9==0:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
mod = 10**9 + 7
n = int(input())
A = list(map(int,input().split()))
S = sum(A)%mod
ans = 0
for a in A:
    ans += a * (S - a)
print(ans*pow(2,-1,mod)%mod)


Fixed submission:
mod = 10**9 + 7
n = int(input())
A = list(map(int,input().split()))
S = sum(A)%mod
ans = 0
for a in A:
    ans += a * (S - a)
print(ans*pow(2,-1,mod)%mod)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline

h,w = map(int,input().split())
ch,cw = map(int,input().split())
dh,dw = map(int,input().split())
S = [list(input()[:-1]) for i in range(h)]

d = [[10**20]*w for i in range(h)]

dq = deque([(ch-1,cw-1,0)])
d[ch-1][cw-1] = 0
dx = [0,-1,0,1]
dy = [-1,0,1,0]

while dq:
    nx,ny,nd = dq.popleft()
    if nd < d[nx][ny]:continue
    for delta in range(4):
        tx,ty = nx+dx[delta], ny + dy[delta]
        if 0 <= tx < h and 0 <= ty < w and S[tx][ty] == "." and d[tx][ty] > d[nx][ny]:
            d[tx][ty] = d[nx][ny]
            dq.appendleft((tx,ty,d[tx][ty]))
    for deltax in range(-2,3):
        for deltay in range(-2,3):
            tx,ty = nx+deltax, ny + deltay
            if 0 <= tx < h and 0 <= ty < w and S[tx][ty] == "." and d[tx][ty] > d[nx][ny]+1:
                d[tx][ty] = d[nx][ny] + 1
                dq.appendleft((tx,ty,d[tx][ty]))

print(d[dh-1][dw-1] if d[dh-1][dw-1] != 10**20 else -1)


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

h,w = map(int,input().split())
ch,cw = map(int,input().split())
dh,dw = map(int,input().split())
S = [list(input()[:-1]) for i in range(h)]

d = [[10**20]*w for i in range(h)]

dq = deque([(ch-1,cw-1,0)])
d[ch-1][cw-1] = 0
dx = [0,-1,0,1]
dy = [-1,0,1,0]

while dq:
    nx,ny,nd = dq.popleft()
    if nd < d[nx][ny]:continue
    for delta in range(4):
        tx,ty = nx+dx[delta], ny + dy[delta]
        if 0 <= tx < h and 0 <= ty < w and S[tx][ty] == "." and d[tx][ty] > d[nx][ny]:
            d[tx][ty] = d[nx][ny]
            dq.appendleft((tx,ty,d[tx][ty]))
    for deltax in range(-2,3):
        for deltay in range(-2,3):
            tx,ty = nx+deltax, ny + deltay
            if 0 <= tx < h and 0 <= ty < w and S[tx][ty] == "." and d[tx][ty] > d[nx][ny]+1:
                d[tx][ty] = d[nx][ny] + 1
                dq.append((tx,ty,d[tx][ty]))

print(d[dh-1][dw-1] if d[dh-1][dw-1] != 10**20 else -1)