Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":

    h,w = list(map(int,input().split()))

    q = []
    reached = [[True]*(w+2)]
    for i in range(1, h+1):
      reached.append([True] + [s == '#' for s in list(input())] + [True])
      for j in range(1,w+1):
        if reached[i][j]: q.append((i,j))
    reached.append([True]*(w+2))

    res = 0
    while 1:
      qq = []
      for k,l in q:
        if not reached[k+1][l]:
          qq.append((k+1, l))
          reached[k+1][l] = True
        if not reached[k][l+1]:
          qq.append((k, l+1))
          reached[k][l+1] = True
        if not reached[k-1][l]:
          qq.append((k-1, l))
          reached[k-1][l] = True
        if not reached[k][l-1]:
          qq.append((k, l-1))
          reached[k][l-1] = True
      if len(qq) == 0:
        break
      else:
        pass
      
      q = qq
      res += 1

    print(res)


Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":

    h,w = list(map(int,input().split()))

    q = []
    reached = [[True]*(w+2)]
    for i in range(1, h+1):
      reached.append([True] + [s == '#' for s in list(input())] + [True])
      for j in range(1,w+1):
        if reached[i][j]: q.append((i,j))
    reached.append([True]*(w+2))

    res = 0
    head = 0
    tail = len(q)
    count = len(q)
    i = 0
    while tail - head > 0:
        k = q[head][0]
        l = q[head][1]
        if not reached[k+1][l]:
            q.append((k+1, l))
            reached[k+1][l] = True
            tail += 1
        if not reached[k][l+1]:
            q.append((k, l+1))
            reached[k][l+1] = True
            tail += 1
        if not reached[k-1][l]:
            q.append((k-1, l))
            reached[k-1][l] = True
            tail += 1
        if not reached[k][l-1]:
            q.append((k, l-1))
            reached[k][l-1] = True
            tail += 1

        head += 1
        i += 1
        if i == count and tail - head > 0:
            count = tail - head
            i = 0
            res += 1
        else:
            pass

    print(res)


Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":

    h,w = list(map(int,input().split()))
    a = [None]*h
    for i in range(h): a[i] = input()

    reached = [[True]*(w+2)]
    for i in range(1, h+1): reached.append([True] + [False]*w + [True])
    reached.append([True]*(w+2))

    q = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                q.append([i+1, j+1])
                reached[i+1][j+1] = True

    res = 0
    head = 0
    tail = len(q)
    count = len(q)
    i = 0
    while tail - head > 0:
        k = q[head][0]
        l = q[head][1]
        if not reached[k+1][l]:
            q.append([k+1, l])
            reached[k+1][l] = True
            tail += 1
        if not reached[k][l+1]:
            q.append([k, l+1])
            reached[k][l+1] = True
            tail += 1
        if not reached[k-1][l]:
            q.append([k-1, l])
            reached[k-1][l] = True
            tail += 1
        if not reached[k][l-1]:
            q.append([k, l-1])
            reached[k][l-1] = True
            tail += 1

        head += 1
        i += 1
        if i == count and tail - head > 0:
            count = tail - head
            i = 0
            res += 1
        else:
            pass

    print(res)


Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

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

if __name__ == "__main__":

    h,w = list(map(int,input().split()))

    a = [[None]]*h
    for i in range(h): a[i] = input()

    reached = [[True]*(w+2) for i in range(h+2)]
    for i in range(1, h+1): reached[i][1:-1] = [False]*w

    q = deque()
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                q.append([i+1, j+1])
                reached[i+1][j+1] = True

    res = 0
    head = 0
    tail = len(q)
    count = len(q)
    i = 0
    while tail - head > 0:
        k = q[0][0]
        l = q[0][1]
        if not reached[k+1][l]:
            q.append([k+1,l])
            reached[k+1][l] = True
            tail += 1
        if not reached[k][l+1]:
            q.append([k,l+1])
            reached[k][l+1] = True
            tail += 1
        if not reached[k-1][l]:
            q.append([k-1,l])
            reached[k-1][l] = True
            tail += 1
        if not reached[k][l-1]:
            q.append([k,l-1])
            reached[k][l-1] = True
            tail += 1

        tail -= 1
        i += 1
        q.popleft()
        if i == count and tail - head > 0:
            count = tail
            i = 0
            res += 1
        else:
            pass

    print(res)

Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":

    h,w = list(map(int,input().split()))

    a = [[] for j in range(h)]
    for i in range(h): a[i] = input()

    reached = [[True for i in range(w+2)] for j in range(h+2)]
    for i in range(1, h+1): reached[i][1:-1] = [False for i in range(w)]

    queue = []
    for i in range(h):
        for j in range(w):
            if a[i][j] == "#":
                queue.append([i+1, j+1])
                reached[i+1][j+1] = True

    res = 0
    head = 0
    tail = len(queue)
    count = len(queue)
    i = 0
    while tail - head > 0:
        k = queue[0][0]
        l = queue[0][1]
        if not reached[k+1][l]:
            queue.append([k+1,l])
            reached[k+1][l] = True
            tail += 1
        if not reached[k][l+1]:
            queue.append([k,l+1])
            reached[k][l+1] = True
            tail += 1
        if not reached[k-1][l]:
            queue.append([k-1,l])
            reached[k-1][l] = True
            tail += 1
        if not reached[k][l-1]:
            queue.append([k,l-1])
            reached[k][l-1] = True
            tail += 1

        tail -= 1
        i += 1
        queue.pop(0)
        if i == count and tail - head > 0:
            count = tail
            i = 0
            res += 1
        else:
            pass

    print(res)

Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
  h,w = list(map(int,input().split()))
  a = [["*"*w] for i in range(h)]
  reached = [[-1 for i in range(w)] for j in range(h)]
  for i in range(h): a[i] = input()
  
  queue = []
  for i in range(h):
    for j in range(w):
      if a[i][j] == "#":
        queue.append([i,j])
        reached[i][j] = 0
  
  res = 0
  while len(queue) > 0:
    for i in range(len(queue)):
      k = queue[0][0]
      l = queue[0][1]
      if 0 <= k+1 < h and 0 <= l < w and reached[k+1][l] == -1:
          queue.append([k+1, l])
      if 0 <= k < h and 0 <= l+1 < w and reached[k][l+1] == -1:
          queue.append([k, l+1])
      if 0 <= k-1 < h and 0 <= l < w and reached[k-1][l] == -1:
          queue.append([k-1, l])
      if 0 <= k < h and 0 <= l-1 < w and reached[k][l-1] == -1:
          queue.append([k, l-1])
      reached[k][l] = res
      queue.pop(0)
    
    res += 1
  
  print(res-1)

Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
  h,w = list(map(int,input().split()))
  a = [["*"*w] for i in range(h)]
  reached = [[-1 for i in range(w)] for j in range(h)]
  for i in range(h): a[i] = input()
  
  queue = []
  for i in range(h):
    for j in range(w):
      if a[i][j] == "#":
        queue.append([i,j])
        reached[i][j] = 0
  
  res = 0
  while len(queue) > 0:
    for i in range(len(queue)):
      k = queue[0][0]
      l = queue[0][1]
      if 0 <= k+1 < h and 0 <= l < w and reached[k+1][l] == -1:
          queue.append([k+1, l])
      if 0 <= k < h and 0 <= l+1 < w and reached[k][l+1] == -1:
          queue.append([k, l+1])
      if 0 <= k-1 < h and 0 <= l < w and reached[k-1][l] == -1:
          queue.append([k-1, l])
      if 0 <= k < h and 0 <= l-1 < w and reached[k][l-1] == -1:
          queue.append([k, l-1])
      reached[k][l] = res
      queue.pop(0)
    
    res += 1
  
  print(res-1)

Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
  h,w = list(map(int,input().split()))
  a = [["*"*w] for i in range(h)]
  reached = [[-1 for i in range(w)] for j in range(h)]
  for i in range(h): a[i] = input()
  
  queue = []
  for i in range(h):
    for j in range(w):
      if a[i][j] == "#":
        queue.append([i,j])
        reached[i][j] = 0
  
  res = 0
  while len(queue) > 0:
    k = queue[0][0]
    l = queue[0][1]
    p = reached[k][l] + 1
    if 0 <= k+1 < h and 0 <= l < w:
      if reached[k+1][l] == -1:
        reached[k+1][l] = p
        res = p
        queue.append([k+1, l])
    if 0 <= k < h and 0 <= l+1 < w:
      if reached[k][l+1] == -1:
        reached[k][l+1] = p
        res = p
        queue.append([k, l+1])
    if 0 <= k-1 < h and 0 <= l < w:
      if reached[k-1][l] == -1:
        reached[k-1][l] = p
        res = p
        queue.append([k-1, l])
    if 0 <= k < h and 0 <= l-1 < w:
      if reached[k][l-1] == -1:
        reached[k][l-1] = p
        res = p
        queue.append([k, l-1])
    queue.pop(0)
  print(res)

Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
if __name__ == "__main__":
  h,w = list(map(int,input().split()))
  a = [["*"*w] for i in range(h)]
  reached = [[10000 for i in range(w)] for j in range(h)]
  for i in range(h): a[i] = input()
  
  queue = []
  for i in range(h):
    for j in range(w):
      if a[i][j] == "#":
        queue.append([i,j])
        reached[i][j] = 0
  
  res = 0
  while len(queue) > 0:
    k = queue[0][0]
    l = queue[0][1]
    p = reached[k][l] + 1
    if 0 <= k+1 < h and 0 <= l < w:
      if p < reached[k+1][l]:
        reached[k+1][l] = p
        res = max(p, res)
        queue.append([k+1, l])
    if 0 <= k < h and 0 <= l+1 < w:
      if p < reached[k][l+1]:
        reached[k][l+1] = p
        res = max(p, res)
        queue.append([k, l+1])
    if 0 <= k-1 < h and 0 <= l < w:
      if p < reached[k-1][l]:
        reached[k-1][l] = p
        res = max(p, res)
        queue.append([k-1, l])
    if 0 <= k < h and 0 <= l-1 < w:
      if p < reached[k][l-1]:
        reached[k][l-1] = p
        res = max(p, res)
        queue.append([k, l-1])
    queue.pop(0)
  print(res)

Fixed submission:
def main():
  h,w = map(int,input().split())

  q = []
  reached = [[True]*(w+2)]
  for i in range(1, h+1):
    reached.append([True] + [s == '#' for s in list(input())] + [True])
    for j in range(1,w+1):
      if reached[i][j]: q.append((i,j))
  reached.append([True]*(w+2))

  res = 0
  for i in range(100000000):
    qq = []
    for k,l in q:
      if not reached[k+1][l]:
        qq.append((k+1, l))
        reached[k+1][l] = True
      if not reached[k][l+1]:
        qq.append((k, l+1))
        reached[k][l+1] = True
      if not reached[k-1][l]:
        qq.append((k-1, l))
        reached[k-1][l] = True
      if not reached[k][l-1]:
        qq.append((k, l-1))
        reached[k][l-1] = True
    if len(qq) == 0:
      break
    else:
      pass

    q = qq
    res += 1

  print(res)
  return 0

if __name__ == "__main__":
  main()