Problem:
There is a grid with H rows and W columns, where each square is painted black or white.  You are given H strings S_1, S_2, ..., S_H, each of length W. If the square at the i-th row from the top and the j-th column from the left is painted black, the j-th character in the string S_i is `#`; if that square is painted white, the j-th character in the string S_i is `.`.  Find the number of pairs of a black square c_1 and a white square c_2 that satisfy the following condition:    * There is a path from the square c_1 to the square c_2 where we repeatedly move to a vertically or horizontally adjacent square through an alternating sequence of black and white squares: black, white, black, white...

Incorrect submission (verdict: Wrong Answer):
h,w=map(int,input().split())
s=[[0]+list(input())+[0]for i in range(h)]
s.insert(0,[0]*(w+2))
s.append([0]*(w+2))
c=["#","."]
n=0
print(s)
for i in range(1,h+1):
    for j in range(1,w+1):
        g=[0,0]
        if s[i][j]!=0:
            p=c.index(s[i][j])
            g[c.index(s[i][j])]+=1
            k=[[i,j]]
            s[i][j]=0
            while len(k)>0:
                b=[]
                for f in range(len(k)):
                    if s[k[f][0]+1][k[f][1]]==c[(p+1)%2]:
                        b.append([k[f][0]+1,k[f][1]])
                        s[k[f][0]+1][k[f][1]]=0
                    if s[k[f][0]-1][k[f][1]]==c[(p+1)%2]:
                        b.append([k[f][0]-1,k[f][1]])
                        s[k[f][0]-1][k[f][1]]=0
                    if s[k[f][0]][k[f][1]+1]==c[(p+1)%2]:
                        b.append([k[f][0],k[f][1]+1])
                        s[k[f][0]][k[f][1]+1]=0
                    if s[k[f][0]][k[f][1]-1]==c[(p+1)%2]:
                        b.append([k[f][0],k[f][1]-1])
                        s[k[f][0]][k[f][1]-1]=0
                else:
                    k=b
                    g[(p+1)%2]+=len(b)
                    p+=1
            else:
                n+=g[0]*g[1]
else:
    print(n)

Fixed, accepted submission:
h,w=map(int,input().split())
s=[[0]+list(input())+[0]for i in range(h)]
s.insert(0,[0]*(w+2))
s.append([0]*(w+2))
c=["#","."]
n=0
for i in range(1,h+1):
    for j in range(1,w+1):
        g=[0,0]
        if s[i][j]!=0:
            p=c.index(s[i][j])
            g[c.index(s[i][j])]+=1
            k=[[i,j]]
            s[i][j]=0
            while len(k)>0:
                b=[]
                for f in range(len(k)):
                    if s[k[f][0]+1][k[f][1]]==c[(p+1)%2]:
                        b.append([k[f][0]+1,k[f][1]])
                        s[k[f][0]+1][k[f][1]]=0
                    if s[k[f][0]-1][k[f][1]]==c[(p+1)%2]:
                        b.append([k[f][0]-1,k[f][1]])
                        s[k[f][0]-1][k[f][1]]=0
                    if s[k[f][0]][k[f][1]+1]==c[(p+1)%2]:
                        b.append([k[f][0],k[f][1]+1])
                        s[k[f][0]][k[f][1]+1]=0
                    if s[k[f][0]][k[f][1]-1]==c[(p+1)%2]:
                        b.append([k[f][0],k[f][1]-1])
                        s[k[f][0]][k[f][1]-1]=0
                else:
                    k=b
                    g[(p+1)%2]+=len(b)
                    p+=1
            else:
                n+=g[0]*g[1]
else:
    print(n)