Problem p03402 (python)

Fixed submission:
A,B=map(int,input().split())
C=[[0 for i in range(100)] for i in range(100)]
for i in range(50,100):
    for j in range(100):
        C[i][j]=1
k=0
for i in range(50):
    for j in range(100):
        if (i*j)%2 ==1:
            if k<A-1:
                C[i][j]=1
                k+=1
            else:
                break
k=0
for i in range(50,100):
    for j in range(100):
        if (i*j)%2 ==1:
            if k<B-1:
                C[i][j]=0
                k+=1
            else:
                break
print(100,100)
for i in range(100):
    st=""
    for j in range(100):
        if C[i][j]==0:
            st+="#"
        else:
            st+="."
    print(st)

Buggy submission (verdict: Runtime Error):
#1A:white
#0B:black
import math
I,J=map(int,input().split())
B=max(I,J)
A=min(I,J)
X=A+B
K=(B-A)//3
P=(B-A)%3
if P==0:
    X+=K
else:
    X+=K+1
n=int(math.sqrt(X))
p,q=0,0
for i in range(1,n+1):
    if X%i==0:
        p=i
        q=X//i
C=[[(i+j)%2 for i in range(p)] for j in range(q)]
N=0;M=0
for i in range(p):
    for j in range(q):
        if (i+j)%2==0:
            M+=1
        else:
            N+=1
flag=0
for i in range(1,p-1):
    for j in range(1,q-1):
        if C[i][j]==0:
            if A<N-3:
                C[i][j]=1
            else:
                flag=1
                break
    if flag==1:
            break
if P==1:
    for i in range(1,p-1):
        if C[0][i]==0:
            C[0][i]=1
            break
elif P==2:
    flag2=0
    for i in range(1,p-1):
        if C[0][i]==0:
            C[0][i]=1
            flag2+=1
        if flag2==2:
            break
print(p,q)
for i in range(p):
    st=""
    for j in range(q):
        if (C[j][i]==0 and J>I) or (C[j][i]==1 and I>=J):
            st+="#"
        else:
            st+="."
    print(st)

Buggy submission (verdict: Runtime Error):
#1A:white
#0B:black
import math
I,J=map(int,input().split())
B=max(I,J)
A=min(I,J)
X=A+B
K=(B-A)//3
P=(B-A)%3
if P==0:
    X+=K
else:
    X+=K+1
n=int(math.sqrt(X))
p,q=0,0
for i in range(1,n+1):
    if X%i==0:
        p=i
        q=X//i
C=[[(i+j)%2 for i in range(p)] for j in range(q)]
N=0;M=0
for i in range(p):
    for j in range(q):
        if (i+j)%2==0:
            M+=1
        else:
            N+=1
flag=0
for i in range(1,p-1):
    for j in range(1,q-1):
        if C[i][j]==0:
            if A<N-3:
                C[i][j]=1
            else:
                flag=1
                break
    if flag==1:
            break
if P==1:
    for i in range(1,p-1):
        if C[0][i]==0:
            C[0][i]=1
            break
elif P==2:
    flag2=0
    for i in range(1,p-1):
        if C[0][i]==0:
            C[0][i]=1
            flag2+=1
        if flag2==2:
            break
print(p,q)
for i in range(p):
    st=""
    for j in range(q):
        if (C[j][i]==0 and J>I) or (C[j][i]==1 and I>=J):
            st+="#"
        else:
            st+="."
        print(st)

Buggy submission (verdict: Runtime Error):
#1A:white
#0B:black
import math
I,J=map(int,input().split())
B=max(I,J)
A=min(I,J)
X=A+B
K=(B-A)//3
P=(B-A)%3
if P==0:
    X+=K
else:
    X+=K+1
n=int(math.sqrt(X))
p,q=0,0
for i in range(1,n+1):
    if X%i==0:
        p=i
        q=X//i
C=[[(i+j)%2 for i in range(p)] for j in range(q)]
N=0;M=0
for i in range(p):
    for j in range(q):
        if (i+j)%2==0:
            M+=1
        else:
            N+=1
flag=0
for i in range(1,p-1):
    for j in range(1,q-1):
        if C[i][j]==0:
            if A<N-3:
                C[i][j]=1
            else:
                flag=1
                break
    if flag==1:
            break
if P==1:
    for i in range(1,p-1):
        if C[0][i]==0:
            C[0][i]=1
            break
elif P==2:
    flag2=0
    for i in range(1,p-1):
        if C[0][i]==0:
            C[0][i]=1
            flag2+=1
        if flag2==2:
            break
print(p,q)
for i in range(p):
    st=""
    for j in range(q):
        if (C[i][j]==0 and J>I) or (C[i][j]==1 and I>=J):
            st+="#"
        else:
            st+="."
        print(st)