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

input = sys.stdin.readline

h, w = map(int, input().split())
# 蛮兵
painted = [['#'] * (w + 2)]
painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
painted.append(['#'] * (w + 2))
que = []

for y in range(1, h + 1):
    for x in range(1, w + 1):
        if painted[y][x] == '#':
            painted[y][x] = 0
            que.append((y, x))
cnt = 0
while que:
    qq = []
    for y, x in que:
        if painted[y - 1][x] == '.':
            painted[y - 1][x] = painted[y][x] + 1
            qq.append((y - 1, x))
        if painted[y + 1][x] == '.':
            painted[y + 1][x] = painted[y][x] + 1
            qq.append((y + 1, x))
        if painted[y][x + 1] == '.':
            painted[y][x + 1] = painted[y][x] + 1
            qq.append((y, x + 1))
        if painted[y][x - 1] == '.':
            painted[y][x - 1] = painted[y][x] + 1
            qq.append((y, x - 1))
    que = qq

print(painted[y][x])

Fixed submission:
import sys

input = sys.stdin.readline


def main():
    h, w = map(int, input().split())
    # 蛮兵
    painted = [['#'] * (w + 2)]
    painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
    painted.append(['#'] * (w + 2))
    que = []

    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if painted[y][x] == '#':
                painted[y][x] = 0
                que.append((y, x))
    while que:
        qq = []
        for y, x in que:
            if painted[y - 1][x] == '.':
                painted[y - 1][x] = painted[y][x] + 1
                qq.append((y - 1, x))
            if painted[y + 1][x] == '.':
                painted[y + 1][x] = painted[y][x] + 1
                qq.append((y + 1, x))
            if painted[y][x + 1] == '.':
                painted[y][x + 1] = painted[y][x] + 1
                qq.append((y, x + 1))
            if painted[y][x - 1] == '.':
                painted[y][x - 1] = painted[y][x] + 1
                qq.append((y, x - 1))
        que = qq

    print(painted[y][x])


if __name__ == '__main__':
    main()

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

input = sys.stdin.readline

h, w = map(int, input().split())
# 蛮兵
painted = [['#'] * (w + 2)]
painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
painted.append(['#'] * (w + 2))
que = deque()

for y in range(1, h + 1):
    for x in range(1, w + 1):
        if painted[y][x] == '#':
            painted[y][x] = 0
            que.append((y, x))
cnt = 0
while que:
    y, x = que.popleft()
    if painted[y - 1][x] == '.':
        painted[y - 1][x] = painted[y][x] + 1
        que.append((y - 1, x))
    if painted[y + 1][x] == '.':
        painted[y + 1][x] = painted[y][x] + 1
        que.append((y + 1, x))
    if painted[y][x + 1] == '.':
        painted[y][x + 1] = painted[y][x] + 1
        que.append((y, x + 1))
    if painted[y][x - 1] == '.':
        painted[y][x - 1] = painted[y][x] + 1
        que.append((y, x - 1))
print(painted[y][x])

Fixed submission:
import sys

input = sys.stdin.readline


def main():
    h, w = map(int, input().split())
    # 蛮兵
    painted = [['#'] * (w + 2)]
    painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
    painted.append(['#'] * (w + 2))
    que = []

    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if painted[y][x] == '#':
                painted[y][x] = 0
                que.append((y, x))
    while que:
        qq = []
        for y, x in que:
            if painted[y - 1][x] == '.':
                painted[y - 1][x] = painted[y][x] + 1
                qq.append((y - 1, x))
            if painted[y + 1][x] == '.':
                painted[y + 1][x] = painted[y][x] + 1
                qq.append((y + 1, x))
            if painted[y][x + 1] == '.':
                painted[y][x + 1] = painted[y][x] + 1
                qq.append((y, x + 1))
            if painted[y][x - 1] == '.':
                painted[y][x - 1] = painted[y][x] + 1
                qq.append((y, x - 1))
        que = qq

    print(painted[y][x])


if __name__ == '__main__':
    main()

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

input = sys.stdin.readline

h, w = map(int, input().split())
# 蛮兵
painted = [['#'] * (w + 2)]
painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
painted.append(['#'] * (w + 2))

que = deque()

for y in range(1, h + 1):
    for x in range(1, w + 1):
        if painted[y][x] == '#':
            painted[y][x] = 0
            que.append((y, x))

cnt = 0
while que:
    y, x = que.popleft()
    for iy, ix in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        ny = y + iy
        nx = x + ix
        if painted[ny][nx] == '.':
            painted[ny][nx] = painted[y][x] + 1
            que.append((ny, nx))

print(painted[y][x])


Fixed submission:
import sys

input = sys.stdin.readline


def main():
    h, w = map(int, input().split())
    # 蛮兵
    painted = [['#'] * (w + 2)]
    painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
    painted.append(['#'] * (w + 2))
    que = []

    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if painted[y][x] == '#':
                painted[y][x] = 0
                que.append((y, x))
    while que:
        qq = []
        for y, x in que:
            if painted[y - 1][x] == '.':
                painted[y - 1][x] = painted[y][x] + 1
                qq.append((y - 1, x))
            if painted[y + 1][x] == '.':
                painted[y + 1][x] = painted[y][x] + 1
                qq.append((y + 1, x))
            if painted[y][x + 1] == '.':
                painted[y][x + 1] = painted[y][x] + 1
                qq.append((y, x + 1))
            if painted[y][x - 1] == '.':
                painted[y][x - 1] = painted[y][x] + 1
                qq.append((y, x - 1))
        que = qq

    print(painted[y][x])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import deque
input = sys.stdin.readline

h, w = map(int, input().split())
painted = [list(input()) for _ in range(h)]
starts = []
for y in range(h):
    for x in range(w):
        if painted[y][x] == '#':
            starts.append((y, x))


def paint_black(starts):
    que = deque()
    for start in starts:
        que.append(start)
    cnt = 0
    while not all([i == '#' for row in painted for i in row]):
        next_position = []
        cnt += 1
        while que:
            y, x = que.popleft()
            for iy, ix in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
                ny = y + iy
                nx = x + ix
                if (0 <= ny < h) and (0 <= nx < w):
                    if painted[ny][nx] != '#':
                        painted[ny][nx] = '#'
                        next_position.append((ny, nx))

        for start in next_position:
            que.append(start)
    return cnt

cnt = paint_black(starts)
print(cnt)

Fixed submission:
import sys

input = sys.stdin.readline


def main():
    h, w = map(int, input().split())
    # 蛮兵
    painted = [['#'] * (w + 2)]
    painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
    painted.append(['#'] * (w + 2))
    que = []

    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if painted[y][x] == '#':
                painted[y][x] = 0
                que.append((y, x))
    while que:
        qq = []
        for y, x in que:
            if painted[y - 1][x] == '.':
                painted[y - 1][x] = painted[y][x] + 1
                qq.append((y - 1, x))
            if painted[y + 1][x] == '.':
                painted[y + 1][x] = painted[y][x] + 1
                qq.append((y + 1, x))
            if painted[y][x + 1] == '.':
                painted[y][x + 1] = painted[y][x] + 1
                qq.append((y, x + 1))
            if painted[y][x - 1] == '.':
                painted[y][x - 1] = painted[y][x] + 1
                qq.append((y, x - 1))
        que = qq

    print(painted[y][x])


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h, w = map(int, input().split())
painted = [list(input()) for _ in range(h)]
starts = []
for y in range(h):
    for x in range(w):
        if painted[y][x] == '#':
            starts.append((y, x))


def paint_black(starts):
    que = deque()
    for start in starts:
        que.append(start)
    cnt = 0
    while not all([i == '#' for row in painted for i in row]):
        next_position = []
        cnt += 1
        while que:
            y, x = que.popleft()
            for iy, ix in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
                ny = y + iy
                nx = x + ix
                if (0 <= ny < h) and (0 <= nx < w):
                    if painted[ny][nx] != '#':
                        painted[ny][nx] = '#'
                        next_position.append((ny, nx))

        for start in next_position:
            que.append(start)
    return cnt

cnt = paint_black(starts)
print(cnt)

Fixed submission:
import sys

input = sys.stdin.readline


def main():
    h, w = map(int, input().split())
    # 蛮兵
    painted = [['#'] * (w + 2)]
    painted += [['#'] + list(input().rstrip()) + ['#'] for _ in range(h)]
    painted.append(['#'] * (w + 2))
    que = []

    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if painted[y][x] == '#':
                painted[y][x] = 0
                que.append((y, x))
    while que:
        qq = []
        for y, x in que:
            if painted[y - 1][x] == '.':
                painted[y - 1][x] = painted[y][x] + 1
                qq.append((y - 1, x))
            if painted[y + 1][x] == '.':
                painted[y + 1][x] = painted[y][x] + 1
                qq.append((y + 1, x))
            if painted[y][x + 1] == '.':
                painted[y][x + 1] = painted[y][x] + 1
                qq.append((y, x + 1))
            if painted[y][x - 1] == '.':
                painted[y][x - 1] = painted[y][x] + 1
                qq.append((y, x - 1))
        que = qq

    print(painted[y][x])


if __name__ == '__main__':
    main()