Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import math


def main():
    h, w = map(int, raw_input().split())
    aa = []
    for _ in xrange(h):
        aa.append(raw_input())
    q = []
    dist = [[float('inf')] * w for _ in xrange(h)]
    for y in xrange(h):
        row = aa[y]
        for x in xrange(w):
            if row[x] == '#':
                dist[y][x] = 0
                q.append([0, x, y])
    heapq.heapify(q)
    while q:
        d, x, y = heapq.heappop(q)
        dd = d + 1
        for dx, dy in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            xx = x + dx
            yy = y + dy
            if 0 <= xx < w and 0 <= yy < h:
                if aa[yy][xx] == '.' and dist[yy][xx] > dd:
                    dist[yy][xx] = dd
                    heapq.heappush(q, [dd, xx, yy])
    m = 0
    for row in dist:
        for v in row:
            if not math.isinf(v) and v > m:
                m = v
    print m


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    h, w = map(int, raw_input().split())
    aa = []
    for _ in xrange(h):
        aa.append(raw_input())
    q = []
    dist = [[False] * w for _ in xrange(h)]
    for y in xrange(h):
        row = aa[y]
        for x in xrange(w):
            if row[x] == '#':
                dist[y][x] = True
                q.append([x, y])
    m = 0
    nexts = []
    while True:
        for x, y in q:
            for dx, dy in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
                xx = x + dx
                yy = y + dy
                if 0 <= xx < w and 0 <= yy < h:
                    if aa[yy][xx] == '.' and not dist[yy][xx]:
                        dist[yy][xx] = True
                        nexts.append([xx, yy])
        if nexts:
            m += 1
            q = nexts
            nexts = []
        else:
            break
    print m


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import math


def main():
    h, w = map(int, raw_input().split())
    aa = []
    for _ in xrange(h):
        aa.append(raw_input())
    q = []
    dist = [[float('inf')] * w for _ in xrange(h)]
    for y in xrange(h):
        row = aa[y]
        for x in xrange(w):
            if row[x] == '#':
                dist[y][x] = 0
                q.append([-0, x, y])
    heapq.heapify(q)
    while q:
        d, x, y = heapq.heappop(q)
        dd = -d + 1
        for dx, dy in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            xx = x + dx
            yy = y + dy
            if 0 <= xx < w and 0 <= yy < h:
                if aa[yy][xx] == '.' and dist[yy][xx] > dd:
                    dist[yy][xx] = dd
                    heapq.heappush(q, [-dd, xx, yy])
    m = 0
    for row in dist:
        for v in row:
            if not math.isinf(v) and v > m:
                m = v
    print m


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    h, w = map(int, raw_input().split())
    aa = []
    for _ in xrange(h):
        aa.append(raw_input())
    q = []
    dist = [[False] * w for _ in xrange(h)]
    for y in xrange(h):
        row = aa[y]
        for x in xrange(w):
            if row[x] == '#':
                dist[y][x] = True
                q.append([x, y])
    m = 0
    nexts = []
    while True:
        for x, y in q:
            for dx, dy in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
                xx = x + dx
                yy = y + dy
                if 0 <= xx < w and 0 <= yy < h:
                    if aa[yy][xx] == '.' and not dist[yy][xx]:
                        dist[yy][xx] = True
                        nexts.append([xx, yy])
        if nexts:
            m += 1
            q = nexts
            nexts = []
        else:
            break
    print m


if __name__ == '__main__':
    main()