Buggy python submission (verdict: Runtime Error):
from collections import deque
H,W,K = list(map(int, input().split()))
A = [[0] * W for _ in range(H)]
n = 1
P = []
B = []
for i in range(H):
    B.append(input())

for i in range(H):
    for j in range(W):
        if B[i][j] == "#":
            A[i][j] = n
            n += 1
            P.append([i,j])
def change(A,x,y,z):
    if y-1 >= 0:
        if A[x][y-1] == 0:
            A[x][y-1] = z
            change(A,x,y-1,z)
    if y + 1 <= (W-1):
        if A[x][y+1] == 0:
            A[x][y+1] = z
            change(A,x,y+1,z)
for i in P:
    change(A,i[0],i[1],A[i[0]][i[1]])
next_ = []
next_ = deque(next_)
for k in range(H):
    if 0 in A[k]:
        next_.append(k)
while len(next_) > 0:
    l = next_.popleft()
    if l == H-1:
        A[l] = A[l-1]
        if 0 in A[l]:
            next_.append(next_.popleft())
    else:
        A[l] = A[l+1]
        if 0 in A[l]:
            next_.append(next_.popleft())
for w in range(H):
    print(*A[w])

Fixed submission:
from collections import deque
H,W,K = list(map(int, input().split()))
A = [[0] * W for _ in range(H)]
n = 1
P = []
B = []
for i in range(H):
    B.append(input())

for i in range(H):
    for j in range(W):
        if B[i][j] == "#":
            A[i][j] = n
            n += 1
            P.append([i,j])
def change(A,x,y,z):
    if y-1 >= 0:
        if A[x][y-1] == 0:
            A[x][y-1] = z
            change(A,x,y-1,z)
    if y + 1 <= (W-1):
        if A[x][y+1] == 0:
            A[x][y+1] = z
            change(A,x,y+1,z)
for i in P:
    change(A,i[0],i[1],A[i[0]][i[1]])
next_ = []
next_ = deque(next_)
for k in range(H):
    if 0 in A[k]:
        next_.append(k)
while len(next_) > 0:
    l = next_.popleft()
    if l == H-1:
        A[l] = A[l-1]
        if 0 in A[l]:
            next_.append(l)
    elif l == 0:
        A[l] = A[l+1]
        if 0 in A[l]:
            next_.append(l)
    else:
        if l+1 in next_ and l - 1 in next_:
            next_.append(l)
        elif l + 1 not in next_:
            A[l] = A[l + 1]
        else:
            A[l] = A[l - 1]
for w in range(H):
    print(*A[w])

Buggy python submission (verdict: Runtime Error):
from collections import deque
H,W,K = list(map(int, input().split()))
A = [[0] * W for _ in range(H)]
n = 1
P = []
B = []
for i in range(H):
    B.append(input())

for i in range(H):
    for j in range(W):
        if B[i][j] == "#":
            A[i][j] = n
            n += 1
            P.append([i,j])
def change(A,x,y,z):
    if y-1 >= 0:
        if A[x][y-1] == 0:
            A[x][y-1] = z
            change(A,x,y-1,z)
    if y + 1 <= (W-1):
        if A[x][y+1] == 0:
            A[x][y+1] = z
            change(A,x,y+1,z)
for i in P:
    change(A,i[0],i[1],A[i[0]][i[1]])
next_ = []
next_ = deque(next_)
for k in range(H):
    if 0 in A[k]:
        next_.append(k)
while len(next_) > 0:
    l = next_.popleft()
    if l == H-1:
        A[l] = A[l-1]
        if 0 in A[l]:
            next_.append(next_.popleft())
    else:
        A[l] = A[l+1]
        if 0 in A[l]:
            next_.append(next_.popleft())
for w in range(H):
    print(*A[w])

Fixed submission:
from collections import deque
H,W,K = list(map(int, input().split()))
A = [[0] * W for _ in range(H)]
n = 1
P = []
B = []
for i in range(H):
    B.append(input())

for i in range(H):
    for j in range(W):
        if B[i][j] == "#":
            A[i][j] = n
            n += 1
            P.append([i,j])
def change(A,x,y,z):
    if y-1 >= 0:
        if A[x][y-1] == 0:
            A[x][y-1] = z
            change(A,x,y-1,z)
    if y + 1 <= (W-1):
        if A[x][y+1] == 0:
            A[x][y+1] = z
            change(A,x,y+1,z)
for i in P:
    change(A,i[0],i[1],A[i[0]][i[1]])
next_ = []
next_ = deque(next_)
for k in range(H):
    if 0 in A[k]:
        next_.append(k)
while len(next_) > 0:
    l = next_.popleft()
    if l == H-1:
        A[l] = A[l-1]
        if 0 in A[l]:
            next_.append(l)
    elif l == 0:
        A[l] = A[l+1]
        if 0 in A[l]:
            next_.append(l)
    else:
        if l+1 in next_ and l - 1 in next_:
            next_.append(l)
        elif l + 1 not in next_:
            A[l] = A[l + 1]
        else:
            A[l] = A[l - 1]
for w in range(H):
    print(*A[w])