Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]

Q = deque()
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            A[h][w]="."
            Q.append((0,h,w))
ans = 0
while Q:
    cnt,h,w = Q.popleft()
    if min(h,w)<0 or h>=H or w>=W:continue
    if A[h][w]=="#":continue
    A[h][w] = "#"
    ans = max(cnt,ans)
    for i in [-1,1]:
        Q.append((cnt+1,h+i,w))
        Q.append((cnt+1,h,w+i))
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]

Q = deque()
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            A[h][w]="."
            Q.append((0,h,w))
ans = 0
while Q:
    cnt,h,w = Q.popleft()
    if min(h,w)<0 or h>=H or w>=W:continue
    if A[h][w]=="#":continue
    A[h][w] = "#"
    ans = max(cnt,ans)
    for i in [-1,1]:
        Q.append((cnt+1,h+i,w))
        Q.append((cnt+1,h,w+i))
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]

Q = deque()
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            A[h][w]="."
            Q.append((0,h,w))
ans = 0
while Q:
    cnt,h,w = Q.popleft()
    if min(h,w)<0 or h>=H or w>=W:continue
    if A[h][w]=="#":continue
    A[h][w] = "#"
    ans = max(cnt,ans)
    for i in [-1,1]:
        Q.append((cnt+1,h+i,w))
        Q.append((cnt+1,h,w+i))
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]

ans = 0
Q = []
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            A[h][w]="."
            Q.append((0,h,w))
while Q:
    c,h,w = Q.pop(0)
    if min(h,w)<0 or h>=H or w>=W:continue
    if A[h][w]=="#":continue
    A[h][w] = "#"
    for i in [-1,1]:
        Q.append((c+1,h+i,w))
        Q.append((c+1,h,w+i))
    ans = max(ans,c)
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]

ans = 0
Q = []
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            A[h][w]="."
            Q.append((0,h,w))
while Q:
    c,h,w = Q.pop(0)
    if min(h,w)<0 or h>=H or w>=W:continue
    if A[h][w]=="#":continue
    A[h][w] = "#"
    for i in [-1,1]:
        Q.append((c+1,h+i,w))
        Q.append((c+1,h,w+i))
    ans = max(ans,c)
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
def f(q):
    global H,W,A
    B = []
    for Q in q:
        h,w = Q[0],Q[1]
        if h+1!=H and A[h+1][w]==".":
            B.append([h+1,w])
            A[h+1][w]="#"
        if h-1!=-1 and A[h-1][w]==".":
            B.append([h-1,w])
            A[h-1][w]="#"
        if w+1!=W and A[h][w+1]==".":
            B.append([h,w+1])
            A[h][w+1]="#"
        if w-1!=-1 and A[h][w-1]==".":
            B.append([h,w-1])
            A[h][w-1]="#"
    return B
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
ans = 0
q = []
for h in range(H):
    for w in range(W):
        if A[h][w]=="#": q.append([h,w])
while q:
    ans+=1
    q = f(q)
print(ans-1)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
import queue
import copy
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
#######
p = queue.Queue()
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            p.put([h-1,w])
            p.put([h+1,w])
            p.put([h,w-1])
            p.put([h,w+1])
ans = 0
while True:
    q = copy.copy(p)
    p = queue.Queue()
    while not(q.empty()):
        x = q.get()
        h,w = x[0],x[1]
        if h<0 or h>=H or w<0 or w>=W or A[h][w]=="#":
            continue
        A[h][w] = "#"
        p.put([h-1,w])
        p.put([h+1,w])
        p.put([h,w-1])
        p.put([h,w+1])
    if p.empty():
        break
    ans+=1
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
import copy
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
#######
p,q = [],[]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            p.append([h-1,w])
            p.append([h+1,w])
            p.append([h,w-1])
            p.append([h,w+1])
ans = 0
while True:
    q = copy.copy(p)
    p.clear()
    while len(q)!=0:
        h,w = q[0][0],q[0][1]
        del q[0]
        if h<0 or h>=H or w<0 or w>=W or A[h][w]=="#":
            continue
        A[h][w] = "#"
        p.append([h-1,w])
        p.append([h+1,w])
        p.append([h,w-1])
        p.append([h,w+1])
    if p==[]:
        break
    ans+=1
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
INF = 10**10
trout = [[INF for i in range(W)]for i in range(H)]
q = []
for h in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=='#':
      if 0<=h-1:
        q.append([h-1,w])
      if h+1<H:
        q.append([h+1,w])
      if 0<=w-1:
        q.append([h,w-1])
      if w+1<W:
        q.append([h,w+1])
      trout[h][w] = 0
while len(q)!=0:
  h,w = q[0][0],q[0][1]
  del q[0]
  if trout[h][w]==INF:
    s = []
    if 0<=h-1:
      q.append([h-1,w])
      s.append(trout[h-1][w])
    if h+1<H:
      q.append([h+1,w])
      s.append(trout[h+1][w])
    if 0<=w-1:
      q.append([h,w-1])
      s.append(trout[h][w-1])
    if w+1<W:
      q.append([h,w+1])
      s.append(trout[h][w+1])
    trout[h][w] = min(s)+1
res = 0
for h in range(H):
  res = max([res]+trout[h])
print(res)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
INF = 10**10
trout = [[INF for i in range(W)]for i in range(H)]
q = []
for h in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=='#':
      if 0<=h-1:
        q.append([h-1,w])
      if h+1<H:
        q.append([h+1,w])
      if 0<=w-1:
        q.append([h,w-1])
      if w+1<W:
        q.append([h,w+1])
      trout[h][w] = 0
while len(q)!=0:
  h,w = q[0][0],q[0][1]
  del q[0]
  if trout[h][w]==INF:
    s = []
    if 0<=h-1:
      q.append([h-1,w])
      s.append(trout[h-1][w])
    if h+1<H:
      q.append([h+1,w])
      s.append(trout[h+1][w])
    if 0<=w-1:
      q.append([h,w-1])
      s.append(trout[h][w-1])
    if w+1<W:
      q.append([h,w+1])
      s.append(trout[h][w+1])
    trout[h][w] = min(s)+1
res = 0
for h in range(H):
  res = max([res]+trout[h])
print(res)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
INF = 10**10
trout = [[INF for i in range(W)]for i in range(H)]
q = []
for h in range(H):
  row = list(input())
  for w in range(W):
    if row[w]=='#':
      q.append([h-1,w])
      q.append([h+1,w])
      q.append([h,w-1])
      q.append([h,w+1])
      trout[h][w] = 0
while len(q)!=0:
  h,w = q[0][0],q[0][1]
  del q[0]
  if h<0 or H<=h or w<0 or W<=w:
    continue
  if trout[h][w]!=INF:
    continue
  if trout[h][w]==INF:
    s = []
    if 0<=h-1:
      s.append(trout[h-1][w])
    if h+1<H:
      s.append(trout[h+1][w])
    if 0<=w-1:
      s.append(trout[h][w-1])
    if w+1<W:
      s.append(trout[h][w+1])
    trout[h][w] = min(s)+1
  q.append([h-1,w])
  q.append([h+1,w])
  q.append([h,w-1])
  q.append([h,w+1])
res = 0
for h in range(H):
  res = max([res]+trout[h])
print(res)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
INF = 10**10
trout = [[INF for i in range(W)]for i in range(H)]
q = []
for h in range(H):
  row = list(input())
  for w in range(W):
    if row[w]=='#':
      q.append([h,w])
      trout[h][w] = 0
while len(q)!=0:
  h,w = q[0][0],q[0][1]
  del q[0]
  if h<0 or H<=h or w<0 or W<=w:
    continue
  if trout[h][w]!=INF and trout[h][w]!=0:
    continue
  if trout[h][w]==INF:
    s = []
    if 0<=h-1:
      s.append(trout[h-1][w])
    if h+1<H:
      s.append(trout[h+1][w])
    if 0<=w-1:
      s.append(trout[h][w-1])
    if w+1<W:
      s.append(trout[h][w+1])
    trout[h][w] = min(s)+1
  q.append([h-1,w])
  q.append([h+1,w])
  q.append([h,w-1])
  q.append([h,w+1])
res = 0
for h in range(H):
  res = max([res]+trout[h])
print(res)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
import queue
H,W = map(int,input().split())
INF = 10**10
all_map = [[INF for i in range(W)]for i in range(H)]
q = queue.Queue()
for i in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=='#':
      all_map[i][w] = 0
      q.put([i-1,w])
      q.put([i+1,w])
      q.put([i,w-1])
      q.put([i,w+1])

while not(q.empty()):
  p = q.get()
  h,w = p[0],p[1]
  if h<0 or H<=h or w<0 or W<=w:
    continue
  if all_map[h][w]!=INF:
    continue
  ####
  s = []
  if 0<=h-1:
    q.put([h-1,w])
    s.append(all_map[h-1][w])
  if h+1<H:
    q.put([h+1,w])
    s.append(all_map[h+1][w])
  if 0<=w-1:
    q.put([h,w-1])
    s.append(all_map[h][w-1])
  if w+1<W:
    q.put([h,w+1])
    s.append(all_map[h][w+1])
  all_map[h][w] = min(s)+1

res = 0
for i in range(H):
  res = max(res,max(all_map[i]))
print(res)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
A = []
for i in range(H):
  a = list(input())
  A.append(a)
def f(h,w):
  global H,W,A
  res = 0
  for i in range(H*W):
    for x in range(-i,i+1):
      for y in range(-i,i+1):
        if abs(x)+abs(y)>i:
          continue
        hx,wx = h+x,w+y
        if hx<0 or H<=hx or wx<0 or W<=wx:
          continue
        if A[hx][wx]=='#':
          res = i
          break
      if res>0:
        break
    if res>0:
      break
  return res
ans = []
for h in range(H):
  for w in range(W):
    if A[h][w]=='.':
      ans.append(f(h,w))
print(max(ans))

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy
sys.setrecursionlimit(10**9)
H,W = map(int,input().split())
A = []
B_place = []
for i in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=="#":
      B_place.append([i,w])
  A.append(a)

all_map = [[10**10 for i in range(W)] for i in range(H)]
def f(h,w,cou):
  global H,W,all_map,C
  if h<0 or H<=h or w<0 or W<=w:
    return
  if [h,w] in C:
    return
  if cou>=all_map[h][w]:
    return
  else:
    all_map[h][w]=cou
  f(h-1,w,cou+1)
  f(h+1,w,cou+1)
  f(h,w-1,cou+1)
  f(h,w+1,cou+1)

for B in B_place:
  C = copy.copy(B_place)
  del C[C.index(B)]
  f(B[0],B[1],0)
ans = 0
for i in range(H):
  ans = max(ans,max(all_map[i]))

print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
H,W = map(int,input().split())
A = []
B_place = []
for i in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=="#":
      B_place.append([i,w])
  A.append(a)

all_map = [[10**10 for i in range(W)] for i in range(H)]
def f(h,w,cou):
  global H,W,all_map
  if h<0 or H<=h or w<0 or W<=w:
    return
  if cou>=all_map[h][w]:
    return
  else:
    all_map[h][w]=cou
  f(h-1,w,cou+1)
  f(h+1,w,cou+1)
  f(h,w-1,cou+1)
  f(h,w+1,cou+1)

for B in B_place:
  f(B[0],B[1],0)

ans = 0
for i in range(H):
  ans = max(ans,max(all_map[i]))

print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
A = []
B_place = []
for i in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=="#":
      B_place.append([i,w])
  A.append(a)

all_map = [[10**10 for i in range(W)] for i in range(H)]
def f(h,w,cou):
  global H,W,all_map
  if h<0 or H<=h or w<0 or W<=w:
    return
  if cou>=all_map[h][w]:
    return
  else:
    all_map[h][w]=cou
  f(h-1,w,cou+1)
  f(h+1,w,cou+1)
  f(h,w-1,cou+1)
  f(h,w+1,cou+1)

for B in B_place:
  f(B[0],B[1],0)

ans = 0
for i in range(H):
  ans = max(ans,max(all_map[i]))

print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W = map(int,input().split())
A = []
B_place = []
for i in range(H):
  a = list(input())
  for w in range(W):
    if a[w]=="#":
      B_place.append([i,w])
  A.append(a)
ans = 0
for h in range(H):
  for w in range(W):
    if A[h][w]==".":
      cou = 10**10  
      for B in B_place:
        s = abs(h-B[0])+abs(w-B[1])
        cou = min(cou,s)
      ans = max(ans,cou)
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)

Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
A = []
for i in range(H):
  a = list(input())
  A.append(a)

ans = 0
def f(h,w,count):
  global H,W,cou,B
  if h<0 or H<=h or w<0 or W<=w:
    return
  if A[h][w]=="#":
    cou = min(cou,count)
    k = 1
    return
  if B[h][w]>count:
    B[h][w]=count
  else:
    return
  f(h-1,w,count+1)
  f(h+1,w,count+1)
  f(h,w-1,count+1)
  f(h,w+1,count+1)
def return_map():
  B = [[10**10 for i in range(W)] for i in range(H)]
  return B
for x in range(H):
  for y in range(W):
    B = return_map()
    cou = 10**10
    f(x,y,0)
    ans = max(ans,cou)
print(ans)

Fixed submission:
H,W = map(int,input().split())
A = [list(input()) for i in range(H)]
S = [[0]*W for i in range(H)]
for h in range(H):
    for w in range(W):
        if A[h][w]=="#":
            S[h][w] = H*W
for h in range(H):
    a = 0
    for w in range(W):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for w in range(W)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
for w in range(W):
    a = 0
    for h in range(H):
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
    for h in range(H)[::-1]:
        S[h][w] = max(a,S[h][w])
        a = max(a,S[h][w])-1
ans = H*W
for h in range(H):
    for w in range(W):
        ans = min(ans,S[h][w])
print(H*W-ans)