Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03436 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
H,W = map(int,input().split())
move_x = [1,-1,0,0]
move_y = [0,0,1,-1]
Field = [[c for c in input()] for _ in range(H)]
seen = [[False for _ in range(W)] for _ in range(H)]
queue = []
black = 0
for i in range(H):
    for j in range(W):
        if Field[i][j] == '#':
            black += 1
dist = [[0 for _ in range(W)] for _ in range(H)]
res = -1 
def bfs(start_x,start_y,Field,seen,queue,dist):
    global res
    seen[start_x][start_y] = True
    for i in range(0,4):
        x = start_x + move_x[i]
        y = start_y + move_y[i]
        if x < 0 or y < 0 or x >= H or y >= W:
            continue #out of range
        if Field[x][y] == '#':
            continue #we don't visit black square.
        if seen[x][y]:
            continue #already visited
        if Field[x][y] in queue:
            continue #already in queue.
        dist[x][y] = dist[start_x][start_y] + 1
        if x == (H-1) and y == (W-1):
            res = H*W - dist[x][y] - black #H*W - counting(seen) - black 
        queue.append((x,y))
    for element in queue:
        x,y = element
        if seen[x][y]:
            continue
        seen[x][y] = True
        queue.remove(element)
        bfs(x,y,Field,seen,queue,dist)    
bfs(0,0,Field,seen,queue,dist)
if res != -1:
	print(res-1)
else:
  print(-1)

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
move_x = [1,-1,0,0]
move_y = [0,0,1,-1]
Field = [[c for c in input()] for _ in range(H)]
seen = [[False for _ in range(W)] for _ in range(H)]
queue = []
black = 0
for i in range(H):
    for j in range(W):
        if Field[i][j] == '#':
            black += 1
dist = [[0 for _ in range(W)] for _ in range(H)]
res = -1 
def bfs(start_x,start_y,Field,seen,queue,dist):
    global res
    seen[start_x][start_y] = True
    for i in range(0,4):
        x = start_x + move_x[i]
        y = start_y + move_y[i]
        if x < 0 or y < 0 or x >= H or y >= W:
            continue #out of range
        if Field[x][y] == '#':
            continue #we don't visit black square.
        if seen[x][y]:
            continue #already visited
        if Field[x][y] in queue:
            continue #already in queue.
        if x == (H-1) and y == (W-1):
            dist[x][y] = dist[start_x][start_y] + 1
            res = H*W - dist[x][y] - black #H*W - counting(seen) - black 
        queue.append((x,y))
        dist[x][y] = dist[start_x][start_y] + 1
    for element in queue:
        x,y = element
        if seen[x][y]:
            continue
        seen[x][y] = True
        queue.remove(element)
        bfs(x,y,Field,seen,queue,dist)    
bfs(0,0,Field,seen,queue,dist)
print(res-1)
Problem p03471 (python)

Fixed submission:
import sys
N,Y = map(int,input().split())
for x in range(0,N+1):
    for y in range(0,N+1-x):
        if 10000*x+5000*y + (N-x-y)* 1000 == Y:
            print('%d %d %d' % (x,y,N-x-y))
            sys.exit(0)
print('-1 -1 -1')

Buggy submission (verdict: Runtime Error):
def diff4(x,y,z):
    temp1 = x
    temp2 = z
    z = temp2 - (diff//4)
    if z < 0:
        choge #RE CHECK
        sys.exit(0)
    x = temp1 + ((diff//4)*10)
    print('%d %d %d' % (x,y,z))
def diff9(x,y,z):
    temp1 = x
    temp2 = z
    z = temp2 - (diff//9)
    if z < 0:
        choge #RE CHECK
        sys.exit(0)
    x = temp1 + ((diff//9)*10)
    print('%d %d %d' % (x,y,z))
def diff1(x,y,z):
    temp1 = z
    temp2 = y
    z = temp1 - 1
    y = temp2 + 2

#C - Otoshidama
import sys
N,Y = map(int,input().split())
#10000yen,5000yen,1000yen
#N mai Y yen
if 10000 * N < Y:
    print('-1 -1 -1')
    sys.exit(0)
z = Y//10000
y = (Y-z*10000) // 5000
x = (Y-z*10000-y*5000) // 1000
#x = 4,y = 0,z = 123
diff = abs(N-(x+y+z))
if diff % 9 == 0:
    diff9(x,y,z)
    sys.exit(0)
elif diff % 4 == 0:
    diff4(x,y,z)
    sys.exit(0)
else:
    while diff % 4 == 0:
        diff1(x,y,z)
        diff = abs(N-(x+y+z))
    diff4(x,y,z)
    sys.exit(0)
Problem p03476 (python)

Fixed submission:
def mips():
    return map(int,input().split())
def ii():
    return int(input())
def is_prime(n):
    if n == 1:
        return False
    i = 2
    while i*i <= n: #sqrt(n)までで十分。
        if n % i == 0:
            return False
        i += 1
    return True
Q = ii()
S = [0 for i in range(10 ** 5 + 1)]
N = 0
for i in [j for j in range(1,10 ** 5 + 1)]:
    if is_prime(i) and is_prime((i+1)//2):
        N += 1
        S[i] = N
    else:
        S[i] = N
for i in range(Q):
    l,r = mips()
    print(S[r]-S[l-1])

Buggy submission (verdict: Runtime Error):
import sympy
def mips():
    return map(int,input().split())
def ii():
    return int(input())
Q = ii()
S = [0 for i in range(10 ** 5 + 1)]
N = 0
for i in [j for j in range(1,10 ** 5 + 1)]:
    if sympy.isprime(i) and sympy.isprime((i+1)//2):
        N += 1
        S[i] = N
    else:
        S[i] = N
for i in range(Q):
    l,r = mips()
    print(S[r]-S[l-1])
Problem p03523 (python)

Fixed submission:
import sys
s = [c for c in "AKIHABARA"]
L = []
for i in range(0, 2):
    for j in range(0, 2):
        for k in range(0, 2):
            for l in range(0, 2):
                if i == 0:
                    s[0] = "X"
                if j == 0:
                    s[4] = "X"
                if k == 0:
                    s[6] = "X"
                if l == 0:
                    s[8] = "X"
                while "X" in s:
                    s.remove("X")
                L.append(s)
                s = [c for c in "AKIHABARA"] #bitで書きたかった・・・。
S = [c for c in input()]
for el in L:
    if S == el:
        print("YES")
        sys.exit(0)
print("NO")

Buggy submission (verdict: Runtime Error):
s = [c for c in "AKIHABARA"]
L = []
for i in range(0, 2):
    for j in range(0, 2):
        for k in range(0, 2):
            for l in range(0, 2):
                if i == 0:
                    s[0] = "X"
                if j == 0:
                    s[4] = "X"
                if k == 0:
                    s[6] = "X"
                if l == 0:
                    s[8] = "X"
                while "X" in s:
                    s.remove("X")
                L.append(s)
                s = [c for c in "AKIHABARA"] #bitで書きたかった・・・。
S = [c for c in input()]
for el in L:
    if S == el:
        print("YES")
        sys.exit(0)
print("NO")
Problem p04043 (python)

Fixed submission:
Liste= list(map(str,input().split()))
P = Liste.count('5')
Q = Liste.count('7')
if P == 2 and Q == 1:
    print('YES')
else:
    print('NO')

Buggy submission (verdict: Runtime Error):
a,b,c = map(int,input().split())Liste= list(map(str,input().split()))
P = Liste.count('5')
Q = Liste.count('7')
if P == 2 and Q == 1:
    print('YES')
else:
    print('NO')
Problem p02584 (python)

Fixed submission:
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    x = abs(x)

    if(k*d <= x):
        print(x-k*d)
    else:
        if(k%2 == x//d%2):
            print(x%d)
        else:
            print(d-x%d)




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    l = 0
    r = k+1
    tmp_res = 0
    while(l+1!=r):
        mid = (l+r)//2
        tmp_res = copy.deepcopy(res)
        if((k-mid)%2==1):
         	res = min(res, abs(abs(x)-(mid+1)*d))
            res = min(res, abs(abs(x)-(mid-1)*d))
            mid+=1
        if((k-mid)%2==0):
            res = min(res, abs(abs(x)-mid*d))
            res = abs(res)
        if(abs(abs(x)-mid*d) > x):
            r = mid
        else:
            l = mid
        if tmp_res == res:
            break

    print(res)




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict,deque
import math
import copy
def main():
    x,k,d=map(int, input().split())
    res = float('inf')
    l = 0
    r = k+1
    tmp_res = 0
    while(l+1!=r):
        mid = (l+r)//2
        tmp_res = copy.deepcopy(res)
        if((k-mid)%2==1):
         	res = min(res, abs(abs(x)-(mid+1)*d))
            res = min(res, abs(abs(x)-(mid-1)*d))
            mid+=1
        if((k-mid)%2==0):
            res = min(res, abs(abs(x)-mid*d))
            res = abs(res)
        if(abs(abs(x)-mid*d) > x):
            r = mid
        else:
            l = mid
        if tmp_res == res:
            break

    print(res)




if __name__ == '__main__':
    main()
Problem p02606 (python)

Fixed submission:
def main():
    l,r,d=map(int, input().split())
    res = 0
    for i in range(l,r+1):
        if(i%d==0):
            res+=1
    print(res)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    n = int(input())
    res = [0]*(n+1)

    for i in range(1,100):
        for j in range(1,100):
            for k in range(1,100):
                if(i**2 + j**2 + k**2 +i*j+j*k + k*i<=n):
                    res[i**2 + j**2 + k**2 +i*j+j*k + k*i]+=1
                    
    for i in range(1,n+1):
        print(res[i])
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():

    n = int(input())
    table = [0]*(n+1)
    x = input()
    k = x.count("1")
    for i in range(1,n+1):
        tmp=str(bin(i)).count("1")
        table[i]=i%tmp
    tmp = 0
    j=n
    for i in range(n):
        if(x[i]=="1"):
            tmp += 2**(j-1)
        j-=1
    j=n
    res = [0]*n
    for i in range(n):
        tmp2=k#1の数
        tmp3 = tmp#１０進数表記
        if(x[i]=="1"):
            tmp2-=1
            tmp3 -= (2**(j-i-1))
        else:
            tmp2+=1
            tmp3 += 2**(j-i-1)
        next = tmp3%tmp2

        if(next==0):
            res[i]=1
        else:
            res[i]+=1
            while(next!=0):
                res[i]+=1
                next = table[next]

    for i in range(n):
        print(res[i])


if __name__ == '__main__':
    main()
Problem p02660 (python)

Fixed submission:
import math
from collections import defaultdict
def prime_decomposition(n):
    i = 2
    table = []
    while i * i <= n:
        while n % i == 0:
          n /= i
          table.append(i)
        i += 1
    if n > 1:
        table.append(n)
    return table
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr
def main():
    dd = defaultdict(int)
    n = int(input())
    pri = factorization(n)
    res = 0
    i  = len(pri)-1
    ans = 0

    for i in pri:
        k = i[1]
        i=1
        tmp=2
        cnt = 0
        while i<=k:
            i+=tmp
            tmp+=1
            cnt+=1
        ans+=cnt
    if len(pri)==1 and pri[0][1]==1:
        ans=1
    if n==1:
        ans = 0
    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
from collections import deque
from itertools import accumulate
import math
def main():
    a,b=map(float, input().split())
    print(int((a*(b*100))//100))


if __name__ == '__main__':
    main()
Problem p02690 (python)

Fixed submission:
x = int(input())
a = [0]*1000
for i in range(1000):
    tmp = i**5
    a[i] = tmp
for i in range(1000):
    for j in range(1000):
        if((a[i]-x) == (a[j])):
            print(i,j)
            exit()
        elif(x-a[i]==a[j]):
            print(i,-j)
            exit()

Buggy submission (verdict: Runtime Error):
import collections
x = int(input())
dd = collections.defaultdict(int)
for i in range(-100,100,1):
    dd[i**5]=i
for i in range(-sqrt(x),sqrt(x),1):
    if(dd[i**5-x]!=0):
        print(i, dd[(i**5-x)])
        exit()
Problem p02696 (python)

Fixed submission:
import math
import collections
def f(a,b,x):
    return math.floor((a*x)/b) - a*math.floor(x/b)
a,b,n=map(int, input().split())
dd = collections.defaultdict(int)

print(f(a,b,min(b-1,n)))

Buggy submission (verdict: Runtime Error):
import math
import collections
def f(a,b,x):
    return math.floor((a*x)/b) - a*math.floor(x/b)
a,b,n=map(int, input().split())
dd = collections.defaultdict(int)

print(f(a,b,min(b-1,x)))
Problem p02702 (python)

Fixed submission:
from collections import defaultdict
dd = defaultdict(int)
S = list(input().rstrip())
tmp = 0
dd[0]+=1
r=1
for s in S[::-1]:
    tmp+=int(s)*r
    r*=10

    tmp%=2019
    r%=2019
    dd[tmp]+=1
su=0
for i in dd.values():
    su+=(i*(i-1))//2
print(su)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
from collections import deque

s=input()
n=len(s)
dd = defaultdict(int)
q = deque()
ans=0
for i in range(4):
    q.append(s[i])
if(int(''.join(q))%2019)==0:
    ans+=1

for i in range(0, n-3):
    for j in range(5, n+1, 3):
        if i+j<n+1:
            dd[(i,i+j)]+=1
            if(int(s[i:i+j])%2019==0) and dd[(i,i+j)]==1:
                ans+=1
            if j>n-4:
                for k in range(3):
                    if i+j+k<n:
                        dd[(i,i+j+k)]+=1
                        if int(s[i:i+j+k])%2019==0 and dd[(i,i+j+k)]==1:
                            ans+=1

print(ans)
Problem p02797 (python)

Fixed submission:
n,k,s=map(int, input().split())
lis = [s]*k
if s==10**9:
    for i in range(n-k):
        lis.append(1)
else:
    for i in range(n-k):
        lis.append(s+1)
print(*lis)

Buggy submission (verdict: Runtime Error):
n,k,s=map(int, input().split())
lis = [s]*k
if s==10**9:for i in range(n-k):lis.append(1)
else:for i in range(n-k):lis.append(s+1)
print(*lis)
Problem p02814 (python)

Fixed submission:
from collections import deque,defaultdict
def gcd(a,b):
    return b if not a%b else gcd(b,a%b)
def lcm(a,b):
    return (a*b)//gcd(a,b)
n,m=map(int, input().split())
A=[int(i) for i in input().split()]
tmp = A[0]
for i in range(1,n):
    tmp = lcm(tmp,A[i])
ans = m//tmp
lis = set()
for i in A:
    r = i
    k=0
    while(r%2==0):
        k+=1
        r//=2
    lis.add(k)
if(m//(tmp)*tmp+tmp//2<=m):
    ans += 1
if(len(lis)!=1):
    ans=0

print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque,defaultdict
def gcd(a,b):
    return b if not a%b else gcd(b,a%b)
def lcm(a,b):
    return (a*b)//gcd(a,b)
n,m=map(int, input().split())
A=[int(i) for i in input().split()]
tmp = A[0]
for i in range(1,n):
    tmp = lcm(tmp,A[i])
#tmpの２分の１と1.5倍が何個入るかに帰着
#m//tmp +
ans = m//tmp
if(m//(tmp)*tmp+tmp//2<=m):
    ans += 1
if(tmp//2 in A or tmp*1.5 in A):
    ans=0
print(ans)
Problem p02845 (python)

Fixed submission:
n = int(input())
A=[int(i) for i in input().split()]
MOD = 1000000007
ans = 1
cnt = [0]*(n+1)
cnt[0]=3

for i in range(n):
    ans = ans * cnt[A[i]] %MOD
    cnt[A[i]]-=1
    cnt[A[i]+1]+=1
    if ans==0:
        break
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
A=[int(i) for i in input().split()]
MOD = 1000000007
ans = 1
cnt = [0]*n
cnt[0]=3

for i in range(n):
    ans = ans * cnt[A[i]] %MOD
    cnt[A[i]]-=1
    cnt[A[i]+1]+=1
    if ans==0:
        break
print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
A=[int(i) for i in input().split()]
MOD = 1000000007
ans = 1
cnt = [0]*n
cnt[0]=3

for i in range(n):
    ans = ans * cnt[A[i]] %MOD
    cnt[A[i]]-=1
    cnt[A[i]+1]+=1

print(ans if not 0 else 0)
Problem p02936 (python)

Fixed submission:
from collections import deque
n,q=map(int, input().split())
edges = [[]for _ in range(n)]
counter = [0]*n
visited=[-1]*n
que=deque()

for i in range(n-1):
    a,b=map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)



for i in range(q):
    a,b=map(int, input().split())
    counter[a-1]+=b


que.append(0)
while que:
    v=que.popleft()
    visited[v]=1
    for next_v in edges[v]:
        if visited[next_v]!=-1:continue
        counter[next_v]+=counter[v]
        que.append(next_v)
print(*counter)

Buggy submission (verdict: Runtime Error):
n,q=map(int, input().split())
edges = [[]for _ in range(n)]
counter = [0]*n
done = [0]*n

for i in range(n-1):
    a,b=map(int, input().split())
    #頂点a, bのどちらが親か調べる 頂点1が根
    edges[a-1].append(b-1)



for i in range(q):
    a,b=map(int, input().split())
    counter[a-1]+=b

def dfs(i: int):
    for j in edges[i]:
        counter[j]+=counter[i]
        dfs(j)

dfs(0)
print(*counter)
Problem p02947 (python)

Fixed submission:
import collections
def main4():
    n = int(input())
    l = []
    for i in range(n):
        s = input()
        l.append((sorted(s)))
    l.sort()
    dd = collections.defaultdict(int)
    k = []
    for i in range(n):
        k.append(''.join(l[i]))
    res = 0
    for i in range(n):
        dd[k[i]]+=1
    for i in dd.values():
        res += (i*(i-1))//2
    print(res)

if __name__ == '__main__':
    main4()

Buggy submission (verdict: Runtime Error):
import collections
def main4():
    dd = collections.defaultdict(int)
    n = int(input())
    res = 0
    for i in range(n):
        s = input()
        s.sort()
        dd[''.join(s)]+=1
    for i in dd.values():
        res += (i*(i-1))//2
    print(res)

if __name__ == '__main__':
    main4()
Problem p03032 (python)

Fixed submission:
import heapq
import itertools
def main():
    n,k=map(int, input().split())
    v=[int(i) for i in input().split()]
    #k回Vの両端から取り出したり詰めたりして持ってるものの価値の最大化をする
    v_1 = v[::-1]
    #左から取る回数をA、右から取る回数をBとしたとき、価値が負の宝石を捨てる操作は（k-(A+B)）回できる
    #これを利用して全探索
    res = 0
    for i in range(k+1):
        j = 0
        while(i+j<=min(n,k)):

            q = []
            tmp = 0
            if(i>0):
                for l in range(i):
                    if(v[l]<0):
                        heapq.heappush(q,v[l])
                    tmp += v[l]
            if(j>0):
                for l in range(j):
                    if(v_1[l]<0):
                        heapq.heappush(q,v_1[l])
                    tmp += v_1[l]
            for l in range(k-(i+j)):
                if(len(q)>0):
                    z = heapq.heappop(q)
                    tmp -= z
            j+=1
            res = max(res,tmp)

    print(res)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import heapq
import itertools
def main():
    n,k=map(int, input().split())
    v=[int(i) for i in input().split()]
    #k回Vの両端から取り出したり詰めたりして持ってるものの価値の最大化をする
    v_1 = v[::-1]
    #左から取る回数をA、右から取る回数をBとしたとき、価値が負の宝石を捨てる操作は（k-(A+B)）回できる
    #これを利用して全探索
    res = 0
    for i in range(k+1):
        j = 0
        while(i+j<=k):
            q = []
            tmp = 0
            if(i>0):
                for l in range(i):
                    if(v[l]<0):
                        heapq.heappush(q,v[l])
                    tmp += v[l]
            if(j>0):
                for l in range(j):
                    if(v_1[l]<0):
                        heapq.heappush(q,v_1[l])
                    tmp += v_1[l]
            for l in range(k-(i+j)):
                if(len(q)>0):
                    z = heapq.heappop(q)
                    tmp -= z
            j+=1
            res = max(res,tmp)

    print(res)
if __name__ == '__main__':
    main()
Problem p03044 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
n=int(input())
a = [[]*n for _ in range(n)]
visited = [0] * n
dist = [0]*n
col = [1]*n

def dfs(i:"int", c:"int"):
    for j in a[i]:
        if not visited[j[0]]:
            visited[j[0]]=1
            if j[1]%2==0:
                col[j[0]]=c
            else:
                col[j[0]]=c*-1
            dfs(j[0], col[j[0]])



if __name__ == '__main__':
    for i in range(n-1):
        u,v,d = map(int, input().split())
        a[u-1].append([v-1, d])
        a[v-1].append([u-1, d])
    visited[0]=1
    dfs(0, 1)
    for i in range(n):
        print(col[i] if col[i]>0 else 0)

Buggy submission (verdict: Runtime Error):
n = int(input())
edge = [[] for _ in range(n)]

for i in range(n-1):
    a,b,c=map(int, input().split())
    edge[a-1].append([b-1,c])
    edge[b-1].append([a-1,c])
#任意の点から行ける偶数の距離で行けるところを同じ色で塗る。
col = [-1]*n
visited = [0]*n

def dfs(i,c):
    visited[i]=1
    col[i] = c
    for j in edge[i]:
        if not visited[j[0]]:
            if j[1]%2==0:
                dfs(j[0],c)
            else:
                dfs(j[0],-1*c)


def main():
    dfs(0,1)
    for i in range(n):
        print(max(col[i],0))



if __name__ == '__main__':
    main()
Problem p03197 (python)

Fixed submission:
n = int(input())
f=True
for i in range(n):
  s = int(input())
  if s%2==1:
    f = False 
if not f:
  print("first")
else:
  print("second")

Buggy submission (verdict: Runtime Error):
n = int(input())
a = []
for i in range(n):
  a.append(int(input()%2))
k = sum(a)

if(k%2)or n==2:
  print("first")
else:
  print("second")

Buggy submission (verdict: Runtime Error):
n = int(input())
a = []
for i in range(n):
  a.append(int(input()%2))
k = sum(a)
if(k%2):
  print("first")
else:
  print("second")
Problem p03282 (python)

Fixed submission:
s = input()
n = int(input())
i = 0
while s[i]=="1":
    if n==i+1:
        print(1)
        exit()
    i+=1
    if i==len(s):
        break

for i in range(len(s)):
    if s[i]!="1":
        print(s[i])
        exit()

Buggy submission (verdict: Runtime Error):
s = input()
n = int(input())
i = 0
while s[i]=="1":
    if n==i:
        print(1)
        exit()
    i+=1

for i in range(len(s)):
    if s[i]!="1":
        print(s[i])
        exit()
Problem p03361 (python)

Fixed submission:
h,w = map(int, input().split())

grid = [[str(i) for i in input()]for _ in range(h)]
dxdy = [[1, 0], [0, 1], [-1, 0], [0, -1]]
for i in range(h):
    for j in range(w):
        if grid[i][j]=="#":
            f = False
            for d in dxdy:
                if(i+d[0]>-1 and j+d[1]>-1 and j+d[1]<w and i+d[0]<h):
                    if grid[i+d[0]][j+d[1]]=="#":
                        f = True
                        continue
            if f==False:
                print("No")
                exit()
print("Yes")

Buggy submission (verdict: Runtime Error):
h,w = map(int, input().split())

grid = [[str(i) for i in input()]for _ in range(h)]
dxdy = [[1, 0], [0, 1], [-1, 0], [0, -1]]

for i in range(h):
    for j in range(w):
        if grid[i][j]=="#":
            f = False
            for d in dxdy:
                if(i+d[0]>-1 and j+d[1]>-1 and i+d[0]<w and j+d[1]<h):
                    if grid[i+d[0]][j+d[1]]=="#":
                        f = True
                        continue
            if f==False:
                print("No")
                exit()


print("Yes")

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())

grid = [[str(i) for i in input()]for _ in range(a)]
dxdy = [[1, 0], [0, 1], [-1, 0], [0, -1]]

for i in range(b):
    for j in range(a):
        if grid[i][j]=="#":
            f = False
            for d in dxdy:
                if(i+d[0]>-1 and j+d[1]>-1 and i+d[0]<b and j+d[1]<a):
                    if grid[i+d[0]][j+d[1]]=="#":
                        f = True
                        continue
            if f==False:
                print("No")
                exit()


print("Yes")