Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.  

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).  

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.  

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.  

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Runtime Error

Solution code:
a = [int(i) for i in input().split()]
h = a[0]
w = a[1]

table = [i for i in range(h)]
for i in range(w):
    table[i] = input()

for i in range(h):
    for j in range(w):
        if table[i][j] == '.':
            c=0
            if table[i - 1][j - 1] == '#' and 0 <= i - 1 < h and 0 <= j - 1 < w:
                c += 1
            if table[i - 1][j] == '#' and 0 <= i - 1 < h and 0 <= j< w:
                c += 1
            if table[i - 1][j+1] == '#' and 0 <= i - 1 < h and 0 <= j+1< w:
                c += 1
            if table[i][j - 1] == '#' and 0 <= i< h and 0 <= j - 1 < w:
                c += 1
            if table[i][j] == '#' and 0 <= i < h and 0 <= j< w:
                c += 1
            if table[i][j+1] == '#' and 0 <= i< h and 0 <= j+1< w:
                c += 1
            if table[i + 1][j - 1] == '#' and 0 <= i + 1 < h and 0 <= j - 1 < w:
                c += 1
            if table[i + 1][j] == '#' and 0 <= i + 1 < h and 0 <= j< w:
                c += 1
            if table[i + 1][j+1] == '#' and 0 <= i + 1 < h and 0 <= j+1< w:
                c += 1
        table[i][j] = str(c)

print(table)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
src = [input() for i in range(H)]

ans = []
for row in src:
    ans.append(list(row))

dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(-1,0)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '#': continues
        c = 0
        for dx,dy in dxy:
            if x+dx <= 0 or x+dx >= W or y+dy <= 0 or y+dy >= H: continue
            if src[y+dy][x+dx] == '#': c += 1        
        ans[i][j] = c

for row in ans:
    print(''.join(list(map(str,row))))

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
S=[[] for i in range(H)]
for i in range(H):
  S[i]=list(str(input()))
ans=[[] for i in range(H)]
for i in range(H):
  for j in range(W):
    if i==0:
      if j==0:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      elif j==W-1:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      else:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
    elif i==H-1:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
    else:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
          
for i in range(H):
  print(''.join(map(str, ans[i])))

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
S=[[] for i in range(H)]
for i in range(H):
  S[i]=list(str(input()))
ans=[[] for i in range(H)]
for i in range(H):
  for j in range(W):
    if i==0:
      if j==0:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      elif j==W-1:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      else:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
    elif i==H-1:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
    else:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
          
for i in range(H):
  print(''.join(map(str, ans[i])))

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = []
T = []
for i in range(H):
    S.append(input())
    T.append([0 for j in range(W)])
if H == 1 and W == 1:
    if S[0][0] == "#":
        T[0][0] = "#"
    else:
        T[0][0] = 0
elif H == 1:
    for j in range(W):
        if S[0][j] == "#":
            T[0][j] = "#"
        elif S[0][j] == ".":
            if j == 0:
                if S[0][j + 1] == "#":
                    T[0][j] += 1
            elif j == W - 1:
                if S[0][j - 1] == "#":
                    T[0][j] += 1
            else:
                if S[0][j - 1] == "#":
                    T[0][j] += 1
                if S[0][j + 1] == "#":
                    T[0][j] += 1
elif W == 1:
    for i in range(H):
        if S[i][0] == "#":
            T[i][0] = "#"
        elif S[i][0] == ".":
            if i == 0:
                if S[i + 1][0] == "#":
                    T[i][0] += 1
            elif i == H - 1:
                if S[i - 1][0] == "#":
                    T[i][0] += 1
            else:
                if S[i - 1][0] == "#":
                    T[i][0] += 1
                if S[i + 1][0] == "#":
                    T[i][0] += 1
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            T[i][j] = "#"
        elif S[i][j] == ".":
            if i == 0 and j == 0:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == 0 and j == W - 1:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == 0:
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == W - 1:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == 0:
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif j == 0:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1:
                if S[i - 1][j - 1] =="#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
            elif j == W - 1:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
            else:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
s = ""
for i in range(H):
    for j in range(W):
        s += str(T[i][j])
    print(s)
    s = ""

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = map(int, input().split())
arr=np.array([])
s=0
while s != H:
  li=list(input())
  li=np.array(li)
  arr=np.hstack((arr,li))

  s+=1
arr=arr.reshape((H,W))
count=0
def check(x,y):
    global count
    if arr[x,y] == "#":
        count+=1

for x in range(H):
    stri=""
    for y in range(W):
        if arr[x,y] == ".":
            count=0
            top= None
            right =None
            left=None
            if y != 0:
                top=True
                check(x,(y-1))
            if x != 0:
                if top:
                    check((x-1),(y-1))
                check((x-1),y)
                left=True
            if x != (W-1):
                if top:
                    check((x+1),(y-1))
                right=True
                check((x+1),y)
            if y != (H-1):
                if left:
                    check(x-1,y+1)
                if right:
                    check(x+1,y+1)
                check(x,(y+1))
            print(str(count),end="")
        else:
            print("#",end="")
    print("")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#from collections import Counter

H, W = map(int, raw_input().split())
s = []
for i in xrange(H):
    s.append(map(str, raw_input()))

for i in xrange(H):
    line = ''
    for j in xrange(W):
        if s[i][j] == '#':
            line+= '#'
        elif i == 0:
            if j == 0:
                line+= str(([s[i][j+1], s[i+1][j], s[i+1][j+1]]).count('#'))
            elif j == (W-1):
                line+= str(([s[i][j-1], s[i+1][j], s[i+1][j-1]]).count('#'))
            else:
                line+= str((s[i][j+1], s[i+1][j], s[i+1][j+1], s[i][j-1], s[i-1][j-1]).count('#'))
        elif i == (H-1):
            if j == 0:
                line+= str(([s[i-1][j], s[i][j+1], s[i-1][j+1]]).count('#'))
            elif j == (W-1):
                line+= str(([s[i-1][j], s[i][j-1], s[i-1][j-1]]).count('#'))
            else:
                line+= str(([s[i-1][j], s[i][j+1], s[i-1][j+1], s[i][j-1], s[i-1][j-1]]).count('#'))
        else:
            if j == 0:
                line+= str(([s[i-1][j], s[i][j+1], s[i-1][j+1], s[i+1][j], s[i+1][j+1]]).count('#'))
            elif j == (W-1):
                line+= str(([s[i-1][j], s[i][j-1], s[i-1][j-1], s[i+1][j], s[i+1][j-1]]).count('#'))
            else:
                line+= str(([s[i-1][j], s[i][j-1], s[i-1][j-1], s[i+1][j], s[i+1][j-1], s[i][j+1], s[i-1][j+1], s[i+1][j+1]]).count('#'))
    print line

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#from collections import Counter

H, W = map(int, raw_input().split())
s = []
for i in xrange(H):
    s.append(map(str, raw_input()))

for i in xrange(H):
    line = ''
    for j in xrange(W):
        if s[i][j] == '#':
            line+= '#'
        elif i == 0:
            if j == 0:
                line+= str(([s[i][j+1], s[i+1][j], s[i+1][j+1]]).count('#'))
            elif j == W:
                line+= str(([s[i][j-1], s[i+1][j], s[i-1][j-1]]).count('#'))
            else:
                line+= str((s[i][j+1], s[i+1][j], s[i+1][j+1], s[i][j-1], s[i-1][j-1]).count('#'))
        elif i == H:
            if j == 0:
                line+= str(([s[i-1][j], s[i][j+1], s[i-1][j+1]]).count('#'))
            elif j == W:
                line+= str(([s[i-1][j], s[i][j-1], s[i-1][j-1]]).count('#'))
            else:
                line+= str(([s[i-1][j], s[i][j+1], s[i-1][j+1], s[i][j-1], s[i-1][j-1]]).count('#'))
        else:
            if j == 0:
                line+= str(([s[i-1][j], s[i][j+1], s[i-1][j+1], s[i+1][j], s[i+1][j+1]]).count('#'))
            elif j == W:
                line+= str(([s[i-1][j], s[i][j-1], s[i-1][j-1], s[i+1][j], s[i+1][j-1]]).count('#'))
            else:
                line+= str(([s[i-1][j], s[i][j-1], s[i-1][j-1], s[i+1][j], s[i+1][j-1], s[i][j+1], s[i-1][j+1], s[i+1][j+1]]).count('#'))
    print line

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = map(int, raw_input().split())
arr=np.array([])
s=0
while s != H:
  li=list(raw_input())
  li=np.array(li)
  arr=np.hstack((arr,li))

  s+=1
arr=arr.reshape((H,W))
count=0
def check(x,y):
    global count
    if arr[x,y] == "#":
        count+=1

for x in range(H):
    stri=""
    for y in range(W):
        if arr[x,y] == ".":
            count=0
            top= None
            right =None
            left=None
            if y != 0:
                top=True
                check(x,(y-1))
            if x != 0:
                if top:
                    check((x-1),(y-1))
                check((x-1),y)
                left=True
            if x != (W-1):
                if top:
                    check((x+1),(y-1))
                right=True
                check((x+1),y)
            if y != (H-1):
                if left:
                    check(x-1,y+1)
                if right:
                    check(x+1,y+1)
                check(x,(y+1))
            stri=stri+str(count)
        else:
            stri=stri+"#"
    print(stri)

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = map(int, raw_input().split())
arr=np.array([])
s=0
while s != H:
  li=list(raw_input())
  li=np.array(li)
  arr=np.hstack((arr,li))

  s+=1
arr=arr.reshape((H,W))
count=0

def check(x,y):
    import __main__
    if arr[x,y] == "#":
        __main__.count+=1

for x in range(H):
    stri=""
    for y in range(W):
        if arr[x,y] == ".":
            count=0
            top= None
            right =None
            left=None
            if y != 0:
                top=True
                check(x,(y-1))
            if x != 0:
                if top:
                    check((x-1),(y-1))
                check((x-1),y)
                left=True
            if x != (W-1):
                if top:
                    check((x+1),(y-1))
                right=True
                check((x+1),y)
            if y != (H-1):
                if left:
                    check(x-1,y+1)
                if right:
                    check(x+1,y+1)
                check(x,(y+1))
            stri=stri+str(count)
        else:
            stri=stri+"#"
    print(stri)

Submission status: Runtime Error

Solution code:
H, W = map(int, input().split())
S = []
T = []
for i in range(H):
    S.append(input())
    T.append([0 for j in range(W)])
if H == 1 and W == 1:
    if S[0][0] == "#":
        T[0][0] = "#"
    else:
        T[0][0] = 0
elif H == 1:
    for j in range(W):
        if S[0][j] == "#":
            T[0][j] = "#"
        elif S[0][j] == ".":
            if j == 0:
                if S[0][j + 1] == "#":
                    T[0][j] += 1
            elif j == W - 1:
                if S[0][j - 1] == "#":
                    T[0][j] += 1
            else:
                if S[0][j - 1] == "#":
                    T[0][j] += 1
                if S[0][j + 1] == "#":
                    T[0][j] += 1
elif W == 1:
    for i in range(H):
        if S[i][0] == "#":
            T[i][0] = "#"
        elif S[i][0] == ".":
            if i == 0:
                if S[i + 1][0] == "#":
                    T[i][0] += 1
            elif i == H - 1:
                if S[i - 1][0] == "#":
                    T[i][0] += 1
            else:
                if S[i - 1][0] == "#":
                    T[i][0] += 1
                if S[i + 1][0] == "#":
                    T[i][0] += 1
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            T[i][j] = "#"
        elif S[i][j] == ".":
            if i == 0 and j == 0:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == 0 and j == W - 1:
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == 0:
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1 and j == W - 1:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
            elif i == 0:
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif j == 0:
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
            elif i == H - 1:
                if S[i - 1][j - 1] =="#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
            elif j == W - 1:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
            else:
                if S[i - 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i - 1][j] == "#":
                    T[i][j] += 1
                if S[i - 1][j + 1] == "#":
                    T[i][j] += 1
                if S[i][j - 1] == "#":
                    T[i][j] += 1
                if S[i][j + 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j - 1] == "#":
                    T[i][j] += 1
                if S[i + 1][j] == "#":
                    T[i][j] += 1
                if S[i + 1][j + 1] == "#":
                    T[i][j] += 1
for i in range(H):
    for j in range(W - 1):
        print(T[i][j], end="")
    print(T[i][W - 1])

Submission status: Runtime Error

Solution code:
import numpy as np

H, W = map(int, raw_input().split())
arr=np.array([])
s=0
while s != H:
  li=list(raw_input())
  li=np.array(li)
  arr=np.hstack((arr,li))

  s+=1
arr=arr.reshape((H,W))
count=0

def check(x,y):
    import __main__
    if arr[x,y] == "#":
        __main__.count+=1

for x in range(H):
    for y in range(W):
        if arr[x,y] == ".":
            count=0
            top= None
            right =None
            left=None
            if y != 0:
                top=True
                check(x,(y-1))
            if x != 0:
                if top:
                    check((x-1),(y-1))
                check((x-1),y)
                left=True
            if x != (W-1):
                if top:
                    check((x+1),(y-1))
                right=True
                check((x+1),y)
            if y != (H-1):
                if left:
                    check(x-1,y+1)
                if right:
                    check(x+1,y+1)
                check(x,(y+1))
            arr[x,y]=str(count)

for x in range(H):
    stri=""
    for y in range(W):
        stri=stri + arr[x,y]
    print(stri)

Submission status: Runtime Error

Solution code:
def calc(array, i, j):
    ret = 0
    if i == 0 and j == 0:
        if array[1][0] == "#":
            ret += 1
        if array[0][1] == "#":
            ret += 1
        if array[1][1] == "#":
            ret += 1
        return ret
    if i == 0 and j == len(array[0])-1:
        if array[i][j-1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        return ret
    if i == len(array)-1 and j == 0:
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        return ret
    if i == len(array)-1 and j == len(array[0])-1:
        if array[i][j-1] == "#":
            ret += 1
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        return ret
    if i == 0:
        if array[i][j-1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i+1][j+1] == "#":
            ret += 1
        return ret
    if i == len(array)-1:
        if array[i][j-1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        return ret
    if j == 0:
        if array[i-1][j] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i+1][j+1] == "#":
            ret += 1
        return ret
    if j == len(array[0])-1:
        if array[i-1][j] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        if array[i][j-1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        return ret
    else:
        if array[i][j-1] == "#":
            ret += 1
        if array[i][j+1] == "#":
            ret += 1
        if array[i-1][j-1] == "#":
            ret += 1
        if array[i-1][j] == "#":
            ret += 1
        if array[i-1][j+1] == "#":
            ret += 1
        if array[i+1][j-1] == "#":
            ret += 1
        if array[i+1][j] == "#":
            ret += 1
        if array[i+1][j+1] == "#":
            ret += 1
        return ret


num = input().split()
num = [int(x) for x in num]

array = []
for h in range(num[0]):
    array.append(list(input()))

for i in range(num[0]):
    for j in range(num[1]):
        if array[i][j] == ".":
            array[i][j] = calc(array, i, j)
for i in range(0, len(array)):
    array[i] = [str(x) for x in array[i]]
    print("".join(array[i]))