Buggy python submission (verdict: Time Limit Exceeded):
import sys

import numpy as np

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(s, n, sh, sw, gh, gw, c, ans):
    if n == 1:
        ans[sh:gh + 1, sw:gw + 1] = c
        c += 1
        return c
    # Horizontal division
    if s.shape[0] > 1:
        n_ = 0
        for h in range(gh - sh):
            n_ += np.sum(s[sh+h, sw:gw+1])
            if 0 < n_ < n:
                c = dfs(s, n_    , sh        , sw, sh + h, gw, c, ans)
                c = dfs(s, n - n_, sh + (h+1), sw, gh    , gw, c, ans)
                return c
    # Vertical division
    n_ = 0
    for w in range(gw - sw):
        n_ += np.sum(s[sh:gh+1, sw+w])
        if 0 < n_ < n:
            c = dfs(s, n_    , sh, sw        , gh, sw + w, c, ans)
            c = dfs(s, n - n_, sh, sw + (w+1), gh, gw    , c, ans)
            return c


def main():
    H, W, K = map(int, input().split())
    s = [None] * H
    s = np.full(shape=(H, W), fill_value=False, dtype=bool)
    for i in range(H):
        s[i] = [j == "#" for j in input().rstrip()]

    ans = np.zeros(shape=(H, W), dtype=int)
    _ = dfs(s, K, 0, 0, H - 1, W - 1, 1, ans)
    for i in range(H):
        print(" ".join(map(str, ans[i])))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

import numpy as np

input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    s = np.zeros(shape=(H, W), dtype=np.bool)
    for i in range(H):
        s[i] = tuple(map(lambda x: True if x == "#" else False, input().rstrip()))

    ans = np.zeros(shape=(H, W), dtype=np.int64)
    h_keep = 0
    k = 1
    for h in range(H):
        if s[h].sum() > 0:
            indices = np.where(s[h])[0]
            ans[h_keep:h + 1] = k
            k += 1
            for idx in reversed(indices[:-1]):
                ans[h_keep:h + 1, :idx + 1] = k
                k += 1
            h_keep = h + 1
        else:
            h_keep = min(h_keep, h)

    if h_keep < H:
        h_base = h_keep - 1
        for h in range(h_keep, H):
            ans[h] = ans[h_base]

    for h in range(H):
        print(" ".join(map(str, ans[h])))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

import numpy as np

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(s, n, sh, sw, gh, gw, c, ans):
    if n == 1:
        ans[sh:gh + 1, sw:gw + 1] = c
        c += 1
        return c
    # Horizontal division
    if s.shape[0] > 1:
        n_ = 0
        for h in range(gh - sh):
            n_ += np.sum(s[h, :])
            if 0 < n_ < n:
                c = dfs(s[:h+1, :], n_    , sh        , sw, sh + h, gw, c, ans)
                c = dfs(s[h+1:, :], n - n_, sh + (h+1), sw, gh    , gw, c, ans)
                return c
    # Vertical division
    n_ = 0
    for w in range(gw - sw):
        n_ += np.sum(s[:, w])
        if 0 < n_ < n:
            c = dfs(s[:, :w+1], n_    , sh, sw        , gh, sw + w, c, ans)
            c = dfs(s[:, w+1:], n - n_, sh, sw + (w+1), gh, gw    , c, ans)
            return c


def main():
    H, W, K = map(int, input().split())
    s = [None] * H
    s = np.full(shape=(H, W), fill_value=False, dtype=bool)
    for i in range(H):
        s[i] = [j == "#" for j in input().rstrip()]

    ans = np.zeros(shape=(H, W), dtype=int)
    _ = dfs(s, K, 0, 0, H - 1, W - 1, 1, ans)
    for i in range(H):
        print(" ".join(map(str, ans[i])))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

import numpy as np

input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    s = np.zeros(shape=(H, W), dtype=np.bool)
    for i in range(H):
        s[i] = tuple(map(lambda x: True if x == "#" else False, input().rstrip()))

    ans = np.zeros(shape=(H, W), dtype=np.int64)
    h_keep = 0
    k = 1
    for h in range(H):
        if s[h].sum() > 0:
            indices = np.where(s[h])[0]
            ans[h_keep:h + 1] = k
            k += 1
            for idx in reversed(indices[:-1]):
                ans[h_keep:h + 1, :idx + 1] = k
                k += 1
            h_keep = h + 1
        else:
            h_keep = min(h_keep, h)

    if h_keep < H:
        h_base = h_keep - 1
        for h in range(h_keep, H):
            ans[h] = ans[h_base]

    for h in range(H):
        print(" ".join(map(str, ans[h])))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

import numpy as np

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(s, sh, sw, gh, gw, c, ans):
    N = np.sum(s)  # num of strawberry
    if N == 1:
        ans[sh:gh + 1, sw:gw + 1] = c
        c += 1
        return c
    # Horizontal division
    for h in range(gh - sh + 1):
        if 0 < np.sum(s[:h, :]) < N:
            c = dfs(s[:h, :], sh, sw, sh + (h - 1), gw, c, ans)
            c = dfs(s[h:, :], sh + h, sw, gh, gw, c, ans)
            return c
    # Vertical division
    for w in range(gw - sw + 1):
        if 0 < np.sum(s[:, :w]) < N:
            c = dfs(s[:, :w], sh, sw, gh, sw + (w - 1), c, ans)
            c = dfs(s[:, w:], sh, sw + w, gh, gw, c, ans)
            return c


def main():
    H, W, K = map(int, input().split())
    s = [None] * H
    s = np.full(shape=(H, W), fill_value=False, dtype=bool)
    for i in range(H):
        s[i] = [j == "#" for j in input().rstrip()]

    ans = np.zeros(shape=(H, W), dtype=int)
    _ = dfs(s, 0, 0, H - 1, W - 1, 1, ans)
    for i in range(H):
        print(" ".join(map(str, ans[i])))


if __name__ == "__main__":
    main()


Fixed submission:
import sys

import numpy as np

input = sys.stdin.readline


def main():
    H, W, K = map(int, input().split())
    s = np.zeros(shape=(H, W), dtype=np.bool)
    for i in range(H):
        s[i] = tuple(map(lambda x: True if x == "#" else False, input().rstrip()))

    ans = np.zeros(shape=(H, W), dtype=np.int64)
    h_keep = 0
    k = 1
    for h in range(H):
        if s[h].sum() > 0:
            indices = np.where(s[h])[0]
            ans[h_keep:h + 1] = k
            k += 1
            for idx in reversed(indices[:-1]):
                ans[h_keep:h + 1, :idx + 1] = k
                k += 1
            h_keep = h + 1
        else:
            h_keep = min(h_keep, h)

    if h_keep < H:
        h_base = h_keep - 1
        for h in range(h_keep, H):
            ans[h] = ans[h_base]

    for h in range(H):
        print(" ".join(map(str, ans[h])))


if __name__ == "__main__":
    main()