Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections
import itertools
import numpy


def main():
  h, w, _ = map(int, input().split())
  values = numpy.array([[int(v == '#') for v in input().strip()] for _ in range(h)])
  flags = numpy.zeros_like(values)
  areas = []

  for y, x in itertools.product(range(h), range(w)):
    if values[y][x] == 1:
      flags[y][x] = 1
      areas.append([x, y, x, y])

  for area in areas:
    sx, sy, ex, ey = area

    while True:
      if sx > 0 and numpy.sum(flags[sy:ey + 1, sx - 1]) == 0:
        flags[sy:ey + 1, sx - 1] += 1
        sx -= 1
      elif sy > 0 and numpy.sum(flags[sy - 1, sx:ex + 1]) == 0:
        flags[sy - 1, sx:ex + 1] += 1
        sy -= 1
      elif ex + 1 < w and numpy.sum(flags[sy:ey + 1, ex + 1]) == 0:
        flags[sy:ey + 1, ex + 1] += 1
        ex += 1
      elif ey + 1 < h and numpy.sum(flags[ey + 1, sx:ex + 1]) == 0:
        flags[ey + 1, sx:ex + 1] += 1
        ey += 1
      else:
        break

    area[0], area[1], area[2], area[3] = sx, sy, ex, ey

  rets = numpy.zeros_like(values)

  for i, (sx, sy, ex, ey) in enumerate(areas, start=1):
    for x, y in itertools.product(range(sx, ex + 1), range(sy, ey + 1)):
      rets[y, x] = i

  for r in rets:
    print(' '.join(map(str, r)))


if __name__ == '__main__':
  main()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools


def main():
  h, w, _ = map(int, input().split())
  values = [[int(v == '#') for v in input().strip()] for _ in range(h)]
  flags = [[0] * w for _ in range(h)]
  areas = []

  for y, x in itertools.product(range(h), range(w)):
    if values[y][x] == 1:
      flags[y][x] = 1
      areas.append([x, y, x + 1, y + 1])

  for area in areas:
    sx, sy, ex, ey = area

    while True:
      if sx > 0 and sum(flags[y][sx - 1] for y in range(sy, ey)) == 0:
        for y in range(sy, ey):
          flags[y][sx - 1] += 1
        sx -= 1
      elif sy > 0 and sum(flags[sy - 1][x] for x in range(sx, ex)) == 0:
        for x in range(sx, ex):
          flags[sy - 1][x] += 1
        sy -= 1
      elif ex < w and sum(flags[y][ex] for y in range(sy, ey)) == 0:
        for y in range(sy, ey):
          flags[y][ex] += 1
        ex += 1
      elif ey < h and sum(flags[ey][x] for x in range(sx, ex)) == 0:
        for x in range(sx, ex):
          flags[ey][x] += 1
        ey += 1
      else:
        break

    area[0], area[1], area[2], area[3] = sx, sy, ex, ey

  rets = [[0] * w for _ in range(h)]

  for i, (sx, sy, ex, ey) in enumerate(areas, start=1):
    for x, y in itertools.product(range(sx, ex), range(sy, ey)):
      rets[y][x] = i

  for r in rets:
    print(' '.join(map(str, r)))


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections
import itertools
import numpy


def main():
  h, w, _ = map(int, input().split())
  values = numpy.array([[int(v == '#') for v in input().strip()] for _ in range(h)])
  flags = numpy.zeros_like(values)
  areas = []

  for y, x in itertools.product(range(h), range(w)):
    if values[y][x] == 1:
      flags[y][x] = 1
      areas.append([x, y, x, y])

  for area in areas:
    sx, sy, ex, ey = area

    while True:
      if sx > 0 and numpy.sum(flags[sy:ey + 1, sx - 1]) == 0:
        flags[sy:ey + 1, sx - 1] += 1
        sx -= 1
      elif sy > 0 and numpy.sum(flags[sy - 1, sx:ex + 1]) == 0:
        flags[sy - 1, sx:ex + 1] += 1
        sy -= 1
      elif ex + 1 < w and numpy.sum(flags[sy:ey + 1, ex + 1]) == 0:
        flags[sy:ey + 1, ex + 1] += 1
        ex += 1
      elif ey + 1 < h and numpy.sum(flags[ey + 1, sx:ex + 1]) == 0:
        flags[ey + 1, sx:ex + 1] += 1
        ey += 1
      else:
        break

    area[0], area[1], area[2], area[3] = sx, sy, ex, ey

  rets = numpy.zeros_like(values)

  for i, (sx, sy, ex, ey) in enumerate(areas, start=1):
    for x, y in itertools.product(range(sx, ex + 1), range(sy, ey + 1)):
      rets[y, x] = i

  for r in rets:
    print(' '.join(map(str, r)))


if __name__ == '__main__':
  main()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools


def main():
  h, w, _ = map(int, input().split())
  values = [[int(v == '#') for v in input().strip()] for _ in range(h)]
  flags = [[0] * w for _ in range(h)]
  areas = []

  for y, x in itertools.product(range(h), range(w)):
    if values[y][x] == 1:
      flags[y][x] = 1
      areas.append([x, y, x + 1, y + 1])

  for area in areas:
    sx, sy, ex, ey = area

    while True:
      if sx > 0 and sum(flags[y][sx - 1] for y in range(sy, ey)) == 0:
        for y in range(sy, ey):
          flags[y][sx - 1] += 1
        sx -= 1
      elif sy > 0 and sum(flags[sy - 1][x] for x in range(sx, ex)) == 0:
        for x in range(sx, ex):
          flags[sy - 1][x] += 1
        sy -= 1
      elif ex < w and sum(flags[y][ex] for y in range(sy, ey)) == 0:
        for y in range(sy, ey):
          flags[y][ex] += 1
        ex += 1
      elif ey < h and sum(flags[ey][x] for x in range(sx, ex)) == 0:
        for x in range(sx, ex):
          flags[ey][x] += 1
        ey += 1
      else:
        break

    area[0], area[1], area[2], area[3] = sx, sy, ex, ey

  rets = [[0] * w for _ in range(h)]

  for i, (sx, sy, ex, ey) in enumerate(areas, start=1):
    for x, y in itertools.product(range(sx, ex), range(sy, ey)):
      rets[y][x] = i

  for r in rets:
    print(' '.join(map(str, r)))


if __name__ == '__main__':
  main()