Problem p02855 (python)

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

T = []
back_n = -1
count = 1
la = []
for i in range(H):
    if "#" in S[i][0]:
        a = []
        c = 0
        if back_n == 0:
            for j in range(W):
                if S[i][0][j] == "#":
                    c += 1
                if S[i][0][j] == "#" and c >= 2:
                    count += 1

                a.append(count)
            if back_n == -1:
                T.append(a)
            else:
                for k in range(i - back_n + 1):
                    T.append(a)
            back_n = -1
            la = a
            count += 1
        else:
            for j in range(W):
                if S[i][0][j] == "#":
                    c += 1
                if S[i][0][j] == "#" and c >= 2:
                    count += 1

                a.append(count)

            if back_n == -1:
                T.append(a)
            else:
                for k in range(i - back_n + 1):
                    T.append(a)
            back_n = -1
            la = a
            count += 1

    elif back_n == -1 and not ("#" in S[i][0]):
        back_n = i

for k in range(H - len(T)):
    T.append(la)

for i in range(H):
    for j in range(W):
        print(T[i][j], end=" ")
    print(end="\n")

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append(input().split())

T = []
back_n = -1
count = 1
la = []
for i in range(H):
    if "#" in S[i][0]:
        a = []
        c = 0
        if back_n == 0:
            for j in range(W):
                if S[i][0][j] == "#":
                    c += 1
                if S[i][0][j] == "#" and c >= 2:
                    count += 1

                a.append(count)
            if back_n == -1:
                T.append(a)
            else:
                for k in range(i - back_n + 1):
                    T.append(a)
            back_n = 0
            la = a
            count += 1
        else:
            for j in range(W):
                if S[i][0][j] == "#":
                    c += 1
                if S[i][0][j] == "#" and c >= 2:
                    count += 1

                a.append(count)

            if back_n == -1:
                T.append(a)
            else:
                for k in range(i - back_n + 1):
                    T.append(a)
            back_n = 0
            la = a
            count += 1

    else:
        back_n = i

for k in range(H - len(T) - 1):
    T.append(la)

for i in range(H):
    for j in range(W):
        print(T[i][j], end=" ")
    print(end="\n")

Buggy submission (verdict: Runtime Error):
H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append(input().split())

T = []
back_n = 0
count = 1
la = []
for i in range(H):
    if "#" in S[i][0]:
        a = []
        c = 0
        if back_n == 0:
            for j in range(W):
                if S[i][0][j] == "#":
                    c += 1
                if S[i][0][j] == "#" and c >= 2:
                    count += 1

                a.append(count)
            T.append(a)
            back_n = 0
            la = a
            count += 1
        else:
            for j in range(W):
                if S[i][0][j] == "#":
                    c += 1
                if S[i][0][j] == "#" and c >= 2:
                    count += 1

                a.append(count)
            for k in range(i - back_n + 1):
                T.append(a)
            back_n = 0
            la = a
            count += 1

    else:
        back_n = i

for k in range(H - len(T) - 1):
    T.append(la)

for i in range(H):
    for j in range(W):
        print(T[i][j], end=" ")
    print(end="\n")