Problem p03574 (python)

Fixed submission:
H,W = map(int,input().split())
S = []
for i in range(H):
  S.append(list(input()))
  
if H == 1:
  if W == 1:
    if S[0][0] == '.':
      S[0][0] = str(0)
    print("".join(S[0]))
    exit()
  if W == 2:
    if S[0][0] == '.' and S[0][1] == '.':
      S[0][0] = str(0)
      S[0][1] = str(0)
    elif S[0][0] == '.' and S[0][1] == '#':
      S[0][0] = str(1)
    elif S[0][0] == '#' and S[0][1] == '.':
      S[0][1] = str(1)
    else:
      pass
    print("".join(S[0]))
    exit()
  
  if W > 2:
    for i in range(W):
      if S[0][i] == '.':
        if i == 0:
          S[0][i] = str(int(S[0][i+1] == '#'))
        elif 0 < i < W-1:
          S[0][i] = str(int(S[0][i-1] == '#') + int(S[0][i+1] == '#'))
        elif i == W-1:
          S[0][i] = str(int(S[0][i-1] == '#'))

    print("".join(S[0]))
    exit()
    
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if i == 0:
        if j == 0:
          cnt = S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif 0<i<H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#')+S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#')+ S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif i == H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#') + S[i][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#') + S[i][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#')
          S[i][j] = str(cnt)

for i in range(H):
  print("".join(S[i]))

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
S = []
for i in range(H):
  S.append(list(input()))
  
if H == 1:
  if W == 1:
    if S[0][0] == '.':
      S[0][0] = str(0)
    print("".join(S[0]))
    exit()
  if W == 2:
    if S[0][0] == '.' and S[0][1] == '.':
      S[0][0] = str(0)
      S[0][1] = str(0)
    elif S[0][0] == '.' and S[0][1] == '#':
      S[0][0] = str(1)
    elif S[0][0] == '#' and S[0][1] == '.':
      S[0][1] = str(1)
    else:
      pass
    print("".join(S[0]))
    exit()
  
  if W > 2:
    for i in range(W):
      if S[0][i] == '.':
        if i == 0:
          S[0][i] = str(int(S[0][i+1] == '#'))
        elif 0 < i < W:
          S[0][i] = str(int(S[0][i-1] == '#') + int(S[0][i+1] == '#'))
        elif i == W-1:
          S[0][i] = str(int(S[0][i-1] == '#'))

    for i in range(H):
      print("".join(S[i]))

    exit()
    
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if i == 0:
        if j == 0:
          cnt = S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif 0<i<H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#')+S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#')+ S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif i == H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#') + S[i][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#') + S[i][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#')
          S[i][j] = str(cnt)

for i in range(H):
  print("".join(S[i]))

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

for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if i == 0:
        if j == 0:
          cnt = S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif 0<i<H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#')+S[i][:j+2].count('#') + S[i+1][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#')+ S[i][j-1:j+2].count('#') + S[i +1][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#') + S[i+1][j-1:].count('#')
          S[i][j] = str(cnt)
      elif i == H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('#') + S[i][:j+2].count('#')
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('#') + S[i][j-1:j+2].count('#')
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('#') + S[i][j-1:].count('#')
          S[i][j] = str(cnt)

for i in range(H):
  print("".join(S[i]))