Buggy python submission (verdict: Runtime Error):
H,W,K = map(int, input().split())
cnt = 0
ans = []
z = 1
for i in range(H):
    f = list(input())
    if f.count("#") == 0: 
        z += 1
    elif f.count("#") == 1:
        cnt += 1
        for i in range(z):
            ans.append([cnt]*W)
        z = 1
    else:
        _c = 0
        cnt += 1
        row = []
        for j,s in enumerate(f):
            if s == "#":
                if _c == 0:
                    _c += 1
                else:
                    _c += 1
                    cnt += 1
            row.append(cnt)
        for i in range(z):
            ans.append(row)
        z = 1

for z in range(z-1):
    ans.append(row)

for i in range(H):
    print(*ans[i])

Fixed submission:
H,W,K = map(int, input().split())
fields = [list(input()) for i in range(H)]
bins = []
hs = [] #区切る位置
ws = []
for i,f in enumerate(fields):
    if "#" in f:
        hs.append(i)
hs = hs+[H]
cnt = 0
ans = []
h_fr = 0
for i in range(1,len(hs)):
    h_to = hs[i]
    if h_fr - h_to == 0:continue
    f = fields[h_fr:h_to]
    # print(i,h_fr,h_to,f)
    ws = []
    for j in range(W):
        for i in range(h_fr,h_to):
             if fields[i][j] == "#":
                 ws.append(j)
    ws += [W]
    cnts = []
    w_fr = 0
    for j in range(1,len(ws)):
        w_to = ws[j]
        if w_to-w_fr > 0:
            cnt += 1
            cnts.extend([cnt]*(w_to-w_fr))
            w_fr = w_to
    for i in range(h_to-h_fr):
        ans.append(cnts)
    h_fr = h_to
# print(ans)
for f in ans:
    print(*f)

Buggy python submission (verdict: Runtime Error):
H,W,K = map(int, input().split())
fields = [list(input()) for i in range(H)]
bins = []
hs = [] #区切る位置
ws = []
for i,f in enumerate(fields):
    if "#" in f:
        hs.append(i)
hs = hs+[H]
ans = [[0]*(W+1) for i in range(H+1)]
cnt = 0
# print(hs)
for i in range(1,len(hs)):
    h_fr = hs[i-1] 
    h_to = hs[i]
    f = fields[h_fr:h_to]
    ws = []
    for j in range(W):
        for i in range(h_fr,h_to):
             if fields[i][j] == "#":
                 ws.append(j)
    ws += [W]
    # print(h_fr,h_to,ws)
    for j in range(1,len(ws)):
        cnt += 1
        w_fr = ws[j-1]
        w_to = ws[j]
        # print(cnt,[[cnt]*(w_to-w_fr) for i in range(h_to-h_fr)] )
        f = [[cnt]*(w_to-w_fr) for i in range(h_to-h_fr)]
        for i in range(h_fr,h_to):
            for j in range(w_fr,w_to):
              ans[i][j] = f[i-h_fr][j-w_to]


for i in range(H):
    prev = ans[i][-1]
    for j in range(W,-1,-1):
        if j == prev: continue
        if ans[i][j] == 0:
            ans[i][j] = prev
        if ans[i][j] != 0:
            prev = ans[i][j]

for i in range(H):
    print(*ans[1:])

Fixed submission:
H,W,K = map(int, input().split())
fields = [list(input()) for i in range(H)]
bins = []
hs = [] #区切る位置
ws = []
for i,f in enumerate(fields):
    if "#" in f:
        hs.append(i)
hs = hs+[H]
cnt = 0
ans = []
h_fr = 0
for i in range(1,len(hs)):
    h_to = hs[i]
    if h_fr - h_to == 0:continue
    f = fields[h_fr:h_to]
    # print(i,h_fr,h_to,f)
    ws = []
    for j in range(W):
        for i in range(h_fr,h_to):
             if fields[i][j] == "#":
                 ws.append(j)
    ws += [W]
    cnts = []
    w_fr = 0
    for j in range(1,len(ws)):
        w_to = ws[j]
        if w_to-w_fr > 0:
            cnt += 1
            cnts.extend([cnt]*(w_to-w_fr))
            w_fr = w_to
    for i in range(h_to-h_fr):
        ans.append(cnts)
    h_fr = h_to
# print(ans)
for f in ans:
    print(*f)