Problem p02855 (python)

Fixed submission:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

h,w,k = inpl()
s = [input() for i in range(h)]
a = [['' for i in range(w)] for j in range(h)]

cnt = 1
nl = [True] * h
for y in range(h):
    if s[y].count('.') == w:
        nl[y] = False
        continue
    stb = s[y].count('#')
    fl = False
    for x in range(w):
        if s[y][x] == '#':
            if not fl:
                fl = True
            else:
                cnt += 1
        a[y][x] = cnt
    cnt += 1
# print(nl)
# for y in range(h):
#     if not y in nl:
#         continue
#     st = [y]
#     tmp = y
#     while True:
#         if tmp + 1 in nl:
#             st.append(tmp+1)
#             tmp += 1
#         else:
#             for _ in st:
#                 for x in range(w):
#                     a[_][x] = a[tmp+1][x]
y = 0
st = []
while y < h:
    if nl[y]:
        y += 1
        continue
    st = [y]
    # print(st)
    if y+1 >= h:
        break
    while y+1< h:
        if nl[y+1]:
            for _ in st:
                for x in range(w):
                    a[_][x] = a[y+1][x]
            y += 1
            break
        else:
            st.append(y+1)
            y += 1
# print(nl)
if not nl[h-1]:
    st = [h-1]
    for y in range(h-1)[::-1]:
        if nl[y]:
            for _ in st:
                for x in range(w):
                    a[_][x] = a[y][x]
            break
        else:
            st.append(y)

for i in range(h):
    print(*a[i])

Buggy submission (verdict: Runtime Error):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

h,w,k = inpl()
s = [input() for i in range(h)]
a = [['' for i in range(w)] for j in range(h)]

cnt = 1
nl = [True] * h
for y in range(h):
    if s[y].count('.') == w:
        nl[y] = False
        continue
    stb = s[y].count('#')
    fl = False
    for x in range(w):
        if s[y][x] == '#':
            if not fl:
                fl = True
            else:
                cnt += 1
        a[y][x] = cnt
    cnt += 1
# print(nl)
# for y in range(h):
#     if not y in nl:
#         continue
#     st = [y]
#     tmp = y
#     while True:
#         if tmp + 1 in nl:
#             st.append(tmp+1)
#             tmp += 1
#         else:
#             for _ in st:
#                 for x in range(w):
#                     a[_][x] = a[tmp+1][x]
y = 0
st = []
while y < h:
    if nl[y]:
        y += 1
        continue
    st = [y]
    # print(st)
    if y+1 >= h:
        break
    while y+1< h:
        if nl[y+1]:
            for _ in st:
                for x in range(w):
                    a[_][x] = a[y+1][x]
            y += 1
            break
        else:
            st.append(y+1)
            t += 1
if st:
    st.sort()
    tmp = st[0] - 1
    for _ in st:
        for x in range(w):
            a[_][x] = a[tmp][x]
for i in range(h):
    print(*a[i])