Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import bisect
import collections
import sys
import math
from fractions import Fraction

sys.setrecursionlimit(1000000)
ACMOD = 1000000007
INF = 1 << 62

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]", B: "List[int]"):
    ab = collections.defaultdict(lambda: [0, 0])  # [posi][nega]
    zeros = 0
    for i in range(N):
        if A[i] == B[i] == 0:
            zeros += 1
        elif A[i] == 0:
            ab[0][1] += 1
        elif A[i] * B[i] >= 0:
            ab[Fraction(abs(B[i]), abs(A[i]))][0] += 1
        else:
            ab[Fraction(abs(A[i]), abs(B[i]))][1] += 1

    # check B[i] ==0
    ans = 1
    for x, y in ab.values():
        pattern = 1  # not select
        pattern += 2 ** x - 1
        pattern += 2 ** y - 1
        ans = ans * pattern % MOD
    print((ans - 1) % MOD + zeros)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)


if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import bisect
import collections
import sys
import math
from fractions import Fraction

sys.setrecursionlimit(1000000)
ACMOD = 1000000007
INF = 1 << 62

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]", B: "List[int]"):
    ab = collections.defaultdict(lambda: [0, 0])  # [posi][nega]
    zeros = 0
    for i in range(N):
        if A[i] == B[i] == 0:
            zeros += 1
        elif A[i] == 0:
            ab[0][1] += 1
        elif B[i] == 0:
            ab[0][0] += 1
        elif A[i] * B[i] >= 0:
            v = math.gcd(A[i], B[i])
            ab[(abs(B[i]) // v, abs(A[i]) // v)][0] += 1
        else:
            v = math.gcd(A[i], B[i])
            ab[(abs(A[i]) // v, abs(B[i]) // v)][1] += 1

    # check B[i] ==0
    ans = 1
    for x, y in ab.values():
        pattern = 1  # not select
        pattern += 2 ** x - 1
        pattern += 2 ** y - 1
        ans = ans * pattern % MOD
    print((ans - 1) % MOD + zeros)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int()] * (N)  # type: "List[int]"
    B = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        A[i] = int(next(tokens))
        B[i] = int(next(tokens))
    solve(N, A, B)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


def lmi():
    return list(map(int, input().split()))


def llmi(n):
    return [lmi() for _ in range(n)]


A, B, N = lmi()
# (A*(x%B) - (A*x)%B )/B

if N < B:
    ans = -INF
    for i in range(1, N + 1):
        ans = max(ans, (A * (i % B) - (A * i) % B) // B)
    print(ans)
    exit()

i = B - 1
print((A * (i % B) - (A * i) % B) // B)


Fixed submission:
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


def lmi():
    return list(map(int, input().split()))


def llmi(n):
    return [lmi() for _ in range(n)]


A, B, N = lmi()
# (A*(x%B) - (A*x)%B )/B

# if N < B:
#     ans = -INF
#     for i in range(1, N + 1):
#         ans = max(ans, (A * (i % B) - (A * i) % B) // B)
#     print(ans)
#     exit()
#
i = min(B - 1,N)
print((A * (i % B) - (A * i) % B) // B)
Buggy python submission (verdict: Runtime Error):
N = int(input())

ans = 0
for i in range(1,N+1):
    if i % 3==0 or i % 5 ==0:
        continue
    ans += i

print(ans)N = int(input())

ans = 0
for i in range(1,N+1):
    if i % 3==0 or i % 5 ==0:
        continue
    ans += i

print(ans)

Fixed submission:
N = int(input())

ans = 0
for i in range(1, N + 1):
    if i % 3 == 0 or i % 5 == 0:
        continue
    ans += i

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math

K = int(input())
ans = 0
for i in range(1, K + 1):
    for j in range(1, K + 1):
        for k in range(1, K + 1):
            ans += math.gcd(math.gcd(i, j),k)


print(ans)

Fixed submission:
import math

K = int(input())
# for K in range(201):
#     ans = 0
#     for i in range(1, K + 1):
#         for j in range(1, K + 1):
#             for k in range(1, K + 1):
#                 ans += math.gcd(math.gcd(i, j),k)
#     print(ans,end=',')

print([0,1,9,30,76,141,267,400,624,885,1249,1590,2208,2689,3411,4248,5248,6081,7485,8530,10248,11889,13687,15228,17988,20053,22569,25242,28588,31053,35463,38284,42540,46581,50893,55362,61824,65857,71247,76884,84388,89349,97881,103342,111528,120141,128047,134580,146316,154177,164817,174438,185836,194157,207927,218812,233268,245277,257857,268182,288216,299257,313635,330204,347836,362973,383709,397042,416448,434025,456967,471948,499740,515581,536073,559758,583960,604833,633651,652216,683712,709065,734233,754734,793188,818917,846603,874512,909496,933081,977145,1006126,1041504,1073385,1106467,1138536,1187112,1215145,1255101,1295142,1342852,1373253,1422195,1453816,1502376,1553361,1595437,1629570,1691292,1726717,1782111,1827492,1887772,1925853,1986837,2033674,2089776,2145333,2197483,2246640,2332104,2379085,2434833,2490534,2554600,2609625,2693919,2742052,2813988,2875245,2952085,3003306,3096024,3157249,3224511,3306240,3388576,3444609,3533637,3591322,3693924,3767085,3842623,3912324,4027884,4102093,4181949,4270422,4361548,4427853,4548003,4616104,4718640,4812789,4918561,5003286,5131848,5205481,5299011,5392008,5521384,5610705,5739009,5818390,5930196,6052893,6156139,6239472,6402720,6493681,6623853,6741078,6864016,6953457,7094451,7215016,7359936,7475145,7593865,7689630,7886244,7984165,8130747,8253888,8403448,8523897,8684853,8802826,8949612,9105537,9267595,9376656,9574704,9686065,9827097,9997134,10174780,10290813,10493367,10611772,10813692]
      [K])
Buggy python submission (verdict: Time Limit Exceeded):
H, W = list(map(int, input().split()))
S = [(input()) for _ in range(H)]

Mh = [[-1 for _ in range(W)] for _ in range(H)]
Mw = [[-1 for _ in range(W)] for _ in range(H)]

for h in range(H):
    w = 0
    while w < W:
        if S[h][w] == "#":
            w += 1
            continue
        start = w
        count = 0
        while w + count < W and S[h][w + count] == ".":
            count += 1
        for x in range(start, w + count):
            Mh[h][x] = count
        w = w + count


for w in range(W):
    h = 0
    while h < H:
        if S[h][w] == "#":
            h += 1
            continue
        start = h
        count = 0
        while h + count < H and S[h+count][w] == ".":
            count += 1
        for x in range(start, h + count):
            Mw[x][w] = count
        h = h + count

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, Mh[h][w]+Mw[h][w] -1)

print(ans)

Fixed submission:
H, W = list(map(int, input().split()))
S = [(input()) for _ in range(H)]

Mh = [[-10000000 for _ in range(W)] for _ in range(H)]
Mw = [[-10000000 for _ in range(W)] for _ in range(H)]

for h in range(H):
    w = 0
    while w < W:
        if S[h][w] == "#":
            w += 1
            continue
        start = w
        count = 0
        while w + count < W and S[h][w + count] == ".":
            count += 1
        for x in range(start, w + count):
            Mh[h][x] = count
        w = w + count


for w in range(W):
    h = 0
    while h < H:
        if S[h][w] == "#":
            h += 1
            continue
        start = h
        count = 0
        while h + count < H and S[h+count][w] == ".":
            count += 1
        for x in range(start, h + count):
            Mw[x][w] = count
        h = h + count

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, Mh[h][w]+Mw[h][w] -1)

print(ans)
Buggy python submission (verdict: Runtime Error):

r,D,x2000 = list(map(int, input().split()))

for i in  range(10):
    new =  r *x - D
    print(new)
    x = new


Fixed submission:
r,D,x = list(map(int, input().split()))

for i in  range(10):
    new =  r *x - D
    print(new)
    x = new
Buggy python submission (verdict: Runtime Error):
N,M  =  list(map(int, input().split()))
A =  list(map(int, input().split()))
BC = [ list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x:-x[1])

i = 0 
flag = True
for b,c in BC:
    if not flag:
        break
    for _ in range(b):
        if i >=N:
            flag = False
        if A[i] < c:
            A[i] = c
            i+=1 
        else:
            flag = False
            break
print(sum(A))


Fixed submission:
N,M  =  list(map(int, input().split()))
A =  list(map(int, input().split()))
BC = [ list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x:-x[1])

i = 0 
flag = True
for b,c in BC:
    if not flag:
        break
    for _ in range(b):
        if i >=N:
            flag = False
            break
        if A[i] < c:
            A[i] = c
            i+=1 
        else:
            flag = False
            break
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M  =  list(map(int, input().split()))
A =  list(map(int, input().split()))
BC = [ list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x:-x[1])

i = 0 
flag = True
for b,c in BC:
    if not flag:
        break
    for _ in range(b):
        if i >=N:
            flag = False
         if A[i] < c:
            A[i] = c
            i+=1 
        else:
            flag = False
            break
print(sum(A))


Fixed submission:
N,M  =  list(map(int, input().split()))
A =  list(map(int, input().split()))
BC = [ list(map(int, input().split())) for _ in range(M)]

A.sort()
BC.sort(key = lambda x:-x[1])

i = 0 
flag = True
for b,c in BC:
    if not flag:
        break
    for _ in range(b):
        if i >=N:
            flag = False
            break
        if A[i] < c:
            A[i] = c
            i+=1 
        else:
            flag = False
            break
print(sum(A))
Buggy python submission (verdict: Runtime Error):
# test for NO is correct 
N,A,B= list(map(int,input().split()))
B1 = A^B


from collections import defaultdict
d = defaultdict(list)
for i in range(2**N):
    bits_total = bin(i)[2:].count('1')
    d[bits_total].append(i)
def count_bits(n):
    return bin(n)[2:].count('1')

def int_to_bin(n):
    return list(reversed(list((map(lambda x:x=='1',bin(n)[2:])))))+[0]*N
def bin_to_int(V):
    ans = 0
    for i,val in enumerate(V):
        ans += val *(2 **i)
    return ans

assert bin_to_int([1])==1
assert bin_to_int([0,1])==2

def movable(i):
    res = set()
    for bit_num in range(N):
        if i>= 2**bit_num and bin(i)[2:][-bit_num-1]=='1':
            res.add(i - 2**bit_num)
        else:
            res.add(i+2**bit_num)
    return res

used = set()
i=0
next = -1
used.add(0)
order = [0]
try:
    while len(used) < 2**N:
        next = max(movable(i) - used)
        used.add(next)
        i=next
        order.append(i)
except:
    print("NO")
    exit()

if count_bits(next)!= count_bits(B1):
    print("NO")
    exit()
raise


def find_all(x):
    res = []
    for bit_num in range(N):
        if x>= 2**bit_num and bin(x)[2:][-bit_num-1]=='1':
            res.append(bit_num)
    return res
a= find_all(B1)
b= find_all(next)

def convert(x,p,q):
    V = int_to_bin(x)
    for i in range(len(p)):
        tmp = V[p[i]]
        V[p[i]]=V[q[i]]
        V[q[i]]= tmp
    return bin_to_int(V)

print("YES")
print(" ".join([str(convert(n,a,b) ^A) for n in order]))

Fixed submission:
from collections import defaultdict,Counter

N = int(input())
S=  input()

d=  defaultdict(int)
back = 0
front =1
ans = 1
c= Counter(S)

for i in c:
    ans *= c[i]+1
    ans = ans%(10**9+7)


print(ans-1)
Buggy python submission (verdict: Runtime Error):
s = input()

def print_index(x,y):
    if y > len(s):
        return
    print(x,y)

def solve(s,index):
    if not s:
        return
    if s[0]=="1":
        print_index(index,index+1)
        return solve(s[1:],index+1)
    else:
        print_index(index,index+1)
        print_index(index,index+2)
        return solve(s[1:],index+1)


(solve(s,1))

Fixed submission:
import bisect
s = input()
if s[-1] == "1" or s[0] == "0":
    print(-1)
    exit()

for i in range((len(s) - 1) // 2):
    # print(i,len(s)- i - 2)
    if s[i] != s[len(s) - i -2]:
        print(-1)
        exit()

sd = s[:-1] + '1'
plist = [i for i, v in enumerate(sd) if v == '1']
# print(plist)
for i,v in enumerate(s[:-1]):
    r = bisect.bisect(plist,i)
    print(i+1,plist[r] + 1)
Buggy python submission (verdict: Runtime Error):
K, N = list(map(int, input().split()))

dprint = lambda *x:x

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 998244353 #出力の制限
__N = 8000
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, __N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



def kumiawase(p, q):
    # dprint(p,q,cmb(p+q-1,q-1 ,mod))
    return cmb(p+q -1,q-1 ,mod)


def solve(n):
    assert n <= K+1
    # 組み合わせの組
    # abs(n-1)//2 + 偶数分 n//2?
    # 2 ->  1,1
    # 3 -> 1,2
    # 4 -> 1,3 2,2
    # 5 -> 1,4 2,3
    # 6 -> 1,5 2,4 3,3
    # 7  3通り

    # 組のうち片方だけ使用する数 p
    ans = 0
    kumi = (n - 1) // 2
    for p in range(kumi+1):
        # dprint("p",p,"n",n)
        if p>N:
            break
        # 使えるのは p個 + 組に関係ないやつ
        ans += 2**p *cmb(kumi,p,mod)* kumiawase(N - p, p+ (K-kumi*2- int(n%2==0) ) )
        # dprint("anstmp1",ans)
        if n % 2 == 0 and N - p -1 >= 0:
            ans += 2**p *cmb(kumi,p,mod) * kumiawase(N - p -1 , p+ (K-kumi*2- int(n%2==0) ) ) # K==n//2を使用
    # dprint("-------",ans)
    return ans


ans = []
for i in range(2, 2 * K +1):
    if i <= K+1:
        tmp = solve(i)%998244353
        ans.append(tmp)
        print(tmp)
    else:
        print(ans[-i + K])


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

if K==1:
    print(N**3)
    exit()
# if K==2:
#     print( ((N+1)//2)**3 +((N//2)**3))
#     exit()
if not K%2 == 0:
    print((N//K)**3)
    exit()

# print(N/K)
print((N//K)**3 +(int(N/K + 1/2)**3))
Buggy python submission (verdict: Runtime Error):



def n_snuke(n):
    return n /sum(map(int,str(n)))


# (100o+10x+y)/(o+x+y) > (100o+99)/(o+9+9)
#  100o*18+(10x+y)(o+18) > 100o(x+y) +99(o+x+y)


#
#1 - 10 , 19,29,39,49,59,69,79,89,99

#for i in range(1,1115):
#    print(i,n_snuke(i))


K = int(input())
tmp =0

tmp_list = []
for i in range(K*3):
    tmp = tmp+ 10**(len(str(tmp+1))-1)
    tmp_list.append(tmp)
    #print(tmp,n_snuke(tmp))
r_list = [tmp_list[-1]]
for i in reversed(range(len(tmp_list)-1)):
    if n_snuke(r_list[-1]) >= n_snuke(tmp_list[i]):
        r_list.append(tmp_list[i])

for i in reversed(r_list[-K:]):
    print(i)


Fixed submission:
import math
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

assert isinstance(A,list)
a = A.index(1)

print(math.ceil((N-1)/(K-1)))
Buggy python submission (verdict: Runtime Error):

S = input()

if S[-1]!="A" or S[-1]=="R":
    print("NO")
    exit(0)
i = 0
for s in "AKIHABARA":
    if S[i]==s:
        i+=1
        continue
    elif s=="A":
        continue
    else:
        print("NO")
        exit()

if i>=len(S)-1:
    print("YES")
else:
    print("NO")

Fixed submission:


import re


S = input()

if re.fullmatch(r"A?KIHA?BA?RA?",S):
    print("YES")
else:
    print("NO")
exit()
Buggy python submission (verdict: Runtime Error):

N= int(input())
D = list(map(int,input().split()))

if any((D+[0]).count(i) >2 for i in range(12)):
    print(0)
    exit(0)
raise

def check(i):
    return False

def get_close(l,d=0):
    return


Fixed submission:
import itertools

N= int(input())
D = list(map(int,input().split()))

if any((D+[0,0,12]).count(i) >2 for i in range(0,13)):
    print(0)
    exit(0)


l= [0]
if D.count(12):
    l.append(12)

can_select=[]
for i in range(1,12):
    if (D).count(i) ==2:
        l.append((i))
        l.append((24 - i))

    if D.count(i)==1:
        can_select.append(i)

def change12(v):
    return min(24-v,v)

def get_closest(l):
    min_val =24
    for i in range(len(l)-1):
        min_val= min(change12(l[i+1]-l[i]),min_val)

    return min(change12(l[-1] - l[0]),min_val )

tmp_ans=0
for seed in list(itertools.product(range(2), repeat=len(can_select))):
    tmp_list = list(l)
    for i,val in enumerate(can_select):
        if seed[i]==1:
            tmp_list.append(val)
        else:
            tmp_list.append(24-val)
    tmp_list.sort()
    tmp_ans =max(tmp_ans, get_closest(tmp_list))



print(tmp_ans)
Buggy python submission (verdict: Time Limit Exceeded):

N = int(input())
for h in range(1,3501):
    for n in range(1,3501):
        if (4*h*n-N*n-N*h) ==0:
            continue
        val =N*h*n/(4*h*n-N*n-N*h)
        if val == int(val) and val>0:
            print(h,n,int(val))
            exit(0)

Fixed submission:

N = int(input())
for h in range(1,3501):
    for n in range(1,3501):
        if 1/n + 1/h > 4/N:
            continue
        if (4*h*n-N*n-N*h) ==0:
            continue
        val =N*h*n/(4*h*n-N*n-N*h)
        if val == int(val) and val>0:
            print(h,n,int(val))
            exit(0)
Buggy python submission (verdict: Runtime Error):


N , K =  list(map(int,input().split()))
keta = 0
for i in range(1000):
    if 2**i > K:
       keta=i
       break

K_2 = bin(K)[2:]

max_kouho = [K_2,"1"* (len(K_2) -1) ]

for i in range(1,len(K_2)-1):
    if K_2[i] == "1" :
        #print(K_2[:i-1]+"00"+ ("1" * 100)[- (len(K_2)-i-1 if len(K_2)-i-1 > 0 else 100 ):] )
        max_kouho.append(
            (K_2[:i]+"0"+ ("1" * 100) )[:len(K_2)] )
#print(max_kouho)


AB  = [ list(map(int,input().split())) for _ in range(N)]
#ans =0

dp = {key:0 for key in max_kouho}
ans_use_top = 0     # 100111
ans_not_use_top = 0 # 011111
for a,b in AB:
    #use
    for key in max_kouho:
        if int(key,2) |a ==int(key,2):
            dp[key]+=b
    #print(bin(a),b,dp)

print(max(dp[key] for key in max_kouho ))

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


K_2 = bin(K)[2:]
max_kouho = [K_2,("1"* (len(K_2) -1) if len(K_2)>1 else "0") ]



for i in range(1,len(K_2)-1):
    if K_2[i] == "1"  and K_2.find("0") >-1:
        max_kouho.append(
            (K_2[:i]+"0"+ ("1" * 100) )[:len(K_2)] )
#print(max_kouho)

AB  = [ list(map(int,input().split())) for _ in range(N)]

if K==0:
    ans =0
    for a,b in (AB):
        if a==0:
            ans +=b
    print(ans)
    exit()
dp = {key:0 for key in max_kouho}
for a,b in AB:
    #use
    for key in max_kouho:
        if int(key,2) |a ==int(key,2):
            dp[key]+=b
    #print(bin(a),b,dp)

print(max(dp[key] for key in max_kouho ))
Buggy python submission (verdict: Runtime Error):
H, W, h, w =list(map(int,input().split()))
INF = 10**10
mat = [[1 for _ in range(H)] for _ in range(W)]
for i in range(h):
    for j in range(w):
        mat[i][j] =1
mat[h-1][w-1]  = -h*w

for i in range(H-h+1):
    for j in range(W-w+1):
        _sum = 0
        for p in range(h):
            for q in range(w):
                tmp = mat[j+q][i+p]
                if p==h-1 and q ==w-1: #corner
                    mat[j+q][i+p]=-_sum -1
                    #print(i+p,j+q)
                _sum +=tmp

#print(mat)
if (sum(sum(mat[j][i] for i in range(H)) for j in range(W)))< 0:
    print("No")
else:
    print("Yes")
    for i in range(H):
        for j in range(W):
            print(mat[j][i], end =" ")
        print("")

Fixed submission:
H, W, h, w =list(map(int,input().split()))
INF = 10**10
mat = [[1000 for _ in range(H)] for _ in range(W)]

val =-h*w*1000+ 999
for h_new in range(h-1,H,h):
    for w_new in range(w-1,W,w):
        mat[w_new][h_new] = val

if (sum(sum(mat[j][i] for i in range(H)) for j in range(W)))< 0:
    print("No")
else:
    print("Yes")
    for i in range(H):
        for j in range(W):
            print(mat[j][i], end =" ")
        print("")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = [ [str=="#" for str in input()]for _ in range(N)]
all_source = [(i,j) for j in range(N) for i in range(N)]

n =0

def f(i_,j_,A):
    global n
    n+=1
    A_copy =   [[ A[i][j] for j in range(N)]for i in range(N)]
    for i,val in enumerate([A[i_][j] for j in range(N) ]):
        A_copy[i][j_] =val
    return A_copy


def slow_solve(n,a):
    if n>5:
        return 99999
    if all(a[i][j]==True for i,j in all_source):
        return n
    min_val = 999999
    for i,j in all_source:
        a_copy = f(i,j,a)
        min_val = min(min_val,slow_solve(n+1,a_copy))
    return min_val


ans = (slow_solve(0,a))
if ans >100:
    ans = 6
if not any(a[i][j]==True for i,j in all_source):
    ans  = -1
print(ans)

Fixed submission:
N = int(input())
a = [ [str=="#" for str in input()]for _ in range(N)]
#all_source = [(i,j) for j in range(N) for i in range(N)]

n =0
ans =9999999999999
all_black_row = sum(all(a[i][j] for i in range(N)) for j in range(N))
black_count_line  = [(sum(a[i][j] for j in range(N))) for i in range(N)]
black_count_row = [(sum(a[i][j] for i in range(N))) for j in range(N)]
for line_no,blacks in enumerate(black_count_line):
    tmp_ans = N -  all_black_row
    if black_count_row[line_no] ==0:
        tmp_ans +=1
    tmp_ans += N-blacks
    ans = min(ans,tmp_ans)

if not any(black_count_line) and not any(black_count_row):
    ans = -1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = [ [str=="#" for str in input()]for _ in range(N)]
all_source = [(i,j) for j in range(N) for i in range(N)]

n =0

def f(i_,j_,A):
    global n
    n+=1
    A_copy =   [[ A[i][j] for j in range(N)]for i in range(N)]
    for i,val in enumerate([A[i_][j] for j in range(N) ]):
        A_copy[i][j_] =val
    return A_copy


def slow_solve(n,a):
    if n>5:
        return 99999
    if all(a[i][j]==True for i,j in all_source):
        return n
    min_val = 999999
    for i,j in all_source:
        a_copy = f(i,j,a)
        min_val = min(min_val,slow_solve(n+1,a_copy))
    return min_val


ans = (slow_solve(0,a))
if ans >100:
    ans = 6
if not any(a[i][j]==True for i,j in all_source):
    ans  = -1
print(ans)
#print(n)
"""


a =f(2,1,a)
a =f(2,0,a)
a =f(2,0,a)
a =f(2,1,a)
a =f(2,2,a)
print(a)

3
...
...
..#
"""

Fixed submission:
N = int(input())
a = [ [str=="#" for str in input()]for _ in range(N)]
#all_source = [(i,j) for j in range(N) for i in range(N)]

n =0
ans =9999999999999
all_black_row = sum(all(a[i][j] for i in range(N)) for j in range(N))
black_count_line  = [(sum(a[i][j] for j in range(N))) for i in range(N)]
black_count_row = [(sum(a[i][j] for i in range(N))) for j in range(N)]
for line_no,blacks in enumerate(black_count_line):
    tmp_ans = N -  all_black_row
    if black_count_row[line_no] ==0:
        tmp_ans +=1
    tmp_ans += N-blacks
    ans = min(ans,tmp_ans)

if not any(black_count_line) and not any(black_count_row):
    ans = -1
print(ans)