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: Wrong Answer

Solution code:
"""
author : halo2halo
date : 29, Jan, 2020
"""

import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, readline().split())
S = [readline().decode('utf8').rstrip() for _ in range(H)]

ans = [[0 if cell == '.' else '#' for cell in row] for row in S]
print(ans)

for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            continue
        for dx, dy in itertools.product([-1, 0, 1], repeat=2):
            x = i + dx
            y = j + dy
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if S[x][y] == '#':
                continue

            ans[x][y] += 1

for row in ans:
    print(*row,sep='')

Submission status: Wrong Answer

Solution code:
"""
author : halo2halo
date : 29, Jan, 2020
"""

import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, readline().split())
S = [readline().decode('utf8').rstrip() for _ in range(H)]

ans = [[0 if cell == '.' else '#' for cell in row] for row in S]
print(ans)

for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            continue
        for dx, dy in itertools.product([-1, 0, 1], repeat=2):
            x = i + dx
            y = j + dy
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if S[x][y] == '#':
                continue

            ans[x][y] += 1

for row in ans:
    print(*row, sep=' ')

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[input()+"." if i!=h else "."*(w+1) for i in range(h+1)]
def count_8(i,j):
    global h,w,s
    cnt=s[i-1][j-1]=="#"+s[i-1][j+1]=="#"+s[i+1][j-1]=="#"+s[i+1][j+1]=="#" \
        +s[i-1][j]=="#"+s[i][j-1]=="#"+s[i+1][j]=="#"+s[i][j+1]=="#"
    return cnt
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            print("#",end="")
        else:
            print(count_8(i,j),end="")
    print()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np



class grid:
  def __init__(self, debug=False):
    # admit stdin
    hw = input().split()
    self.H = int(hw[0])
    self.W = int(hw[1])

    self.circuit = [[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1]]
    
    # char decomp, loop accessed as g[row][col]
    self.g = [[self.__rep2int(c) for c in input()] for j in range(self.H)]
    if debug:
      print(self.g)

  def __rep2int(self, c):
    return 0 if c=='.' else 1

  def __onBomb(self, i, j):
    return self.g[i][j]==1

  def __circuitSum(self, i, j, array):
    val = 0
    for delta in self.circuit:
      I = i+delta[0]
      J = j+delta[1]
      # if the circuit contains the coordinate out of the grid, do nothing
      if any([I<0, I>=self.H]) or any([J<0, J>=self.W]):
        continue
      else:
        val += array[I][J]
    return val

  def CountBombs(self):
    tempg = ['' for j in range(self.H)]
    # sum
    for i in range(self.H):
      for j in range(self.W):
        tempg[i] += '#' if self.__onBomb(i,j) else str(self.__circuitSum(i,j,self.g))
    return tempg

g=grid()
print(g.CountBombs())

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np



class grid:
  def __init__(self, debug=False):
    # admit stdin
    hw = input().split()
    self.H = int(hw[0])
    self.W = int(hw[1])

    self.circuit = [[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1]]
    
    # char decomp, loop accessed as g[row][col]
    self.g = [[self.__rep2int(c) for c in input()] for j in range(self.H)]
    if debug:
      print(self.g)

  def __rep2int(self, c):
    return 0 if c=='.' else 1

  def __onBomb(self, i, j):
    return self.g[i][j]==1

  def __circuitSum(self, i, j, array):
    val = 0
    for delta in self.circuit:
      I = i+delta[0]
      J = j+delta[1]
      # if the circuit contains the coordinate out of the grid, do nothing
      if any([I<0, I>=self.H]) or any([J<0, J>=self.W]):
        continue
      else:
        val += array[I][J]
    return val

  def CountBombs(self):
    tempg = ['' for j in range(self.H)]
    # sum
    for i in range(self.H):
      for j in range(self.W):
        tempg[i] += '#' if self.__onBomb(i,j) else str(self.__circuitSum(i,j,self.g))
    return tempg

g=grid(True)
print(g.CountBombs())

Submission status: Wrong Answer

Solution code:
H, W = map(int,input().split())
s = []*H
flag = 0
tmp = 0
ans = ''
t = [-1,0,1]
for i in range(H):
    s.append(input())

for i in range(H):
    ans =''
    for j in range(W):
        if s[i][j] == '.':
            tmp = 0
            for k in t:
                for m in t:
                    try:
                        if s[i-k][j-m] == '#':
                            tmp += 1
                    except IndexError:
                        pass
            ans += str(tmp)
        else:
            ans += '#'
    print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = [(input()) for _ in range(H)]
T = [[0 for _ in range(W)] for _ in range(H)]

for h in range(H):
    for w in range(W):
        for i in range(h-1, h+2):
            for j in range(w-1, w+2):
                if h-1 < -1 and w-1 < -1:
                    continue
                else:
                    if S[min(i, H-1)][min(j, W-1)] == '#':
                        T[h][w] += 1
                    elif S[h][w] == '#':
                        T[h][w] = -1

for t in T:
    t = map(str, t)
    print((''.join(t)).replace('-1', '#'))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())

li=[]
for i in range(h):
    li.append(list(input()))

new_li=[[0]*w for i in range(h)]

#＃の位置をリストに集める
sharp_li=[]
for i in range(h):
    for j in range(w):
        if li[i][j]=='#':
            sharp_li.append([i,j])   

for k in range(len(sharp_li)):
    #y行X列を扱う
    y=sharp_li[k][0]
    x=sharp_li[k][1]
    
    #tryの例外処理だとうまくいかなかったので、端の場合を自力で書いて場合わけする
    if x==0:
        if w!=1:             #1行や1列の場合がエラーの原因なので、無理やりif文で処理
            xli=[0,1]
        else:
            xli=[0]
    elif x==w-1:
        xli=[w-2,w-1]
    else:
        xli=[x-1,x,x+1]
    
    if y==0:
        if h!=1:
            yli=[0,1]
        else:
            yli=[0]
    elif y==h-1:
        yli=[h-2,h-1]
    else:
        yli=[y-1,y,y+1]
        
        
    for l in xli:
        for m in yli:
            new_li[m][l]+=1           #m行l列なので
           
        
ans=[]             
for n in range(h):
    stred=list(map(str,new_li[n]))
    ans.append(stred)
print(ans)
#もともとの爆弾の部分を＃に戻す

for o in range(len(sharp_li)):
    ans[sharp_li[o][0]][sharp_li[o][1]]='#'


for p in range(h):
    tans=''.join(ans[p])
    print(tans)

Submission status: Wrong Answer

Solution code:
HW = input().split()
H, W = int(HW[0]), int(HW[1])
S = [input() for _ in range(H)] 
result = [[0 for _ in range(W)] for _ in range(H)]

for i in range(H):
  for j in range(W):
    if S[i][j] == '#':
      result[i][j] = '#'
      continue
    if i-1 > 0 and j-1 > 0 and S[i-1][j-1] == '#':
      result[i][j] += 1
    if i-1 > 0 and S[i-1][j] == '#':
      result[i][j] += 1
    if i-1 > 0 and j+1 < W and S[i-1][j+1] == '#':
      result[i][j] += 1
    if j-1 > 0 and S[i][j-1] == '#':
      result[i][j] += 1
    if j+1 < W and S[i][j+1] == '#':
      result[i][j] += 1
    if i+1 < H and j-1 > 0 and S[i+1][j-1] == '#':
      result[i][j] += 1
    if i+1 < H and S[i+1][j] == '#':
      result[i][j] += 1
    if i+1 < H and j+1 < W and S[i+1][j+1] == '#':
      result[i][j] += 1

for i in range(H):
  ans = ''
  for j in range(W):
    ans += str(result[i][j])
  print(ans)

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().strip().split(' '))
data = [input() for i in range(h)]

for i in range(h):
    data[i] = list(data[i])

dxy =[(-1,0),(-1,-1),(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,1)]

ans = [[ 0 for i in range(w)] for j in range(h)] 
print(data)

for x in range(h):
    for y in range(w):
        print(x, y)
        if data[x][y] == '#':
            ans[x][y] = '#'
            continue
            
        c = 0
        for dx, dy in dxy:
            if x+dx < 0 or x+dx > h-1 or y+dy < 0 or y+dy > w-1:
                continue
            if data[x+dx][y+dy] == '#':
                c += 1
        
        ans[x][y] = c
        
for row in ans:
    print(''.join(list(map(str,row))))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [input() for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            ans = 0
            for ii in range(-1,2):
                for jj in range(-1,2):
                    try:
                        if s[i+ii][j+jj] == "#":
                            ans += 1
                    except:
                        None
            print(ans, end="")
        else:
            print("#", end="")
    print("")

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(list(str(input())))
x = []

for i in range(h):
    lst = []
    for j in range(w):
        cnt = 0
        if s[i][j]=='.':
            for m in range(3):
                for n in range(3):
                    if 0<i-1+m<h and 0<j-1+n<w:
                        if s[i-1+m][j-1+n]=='#':
                            cnt += 1
            lst.append(str(cnt))
        else:
            lst.append('#')

    x.append(''.join(lst))

for i in range(h):
    print(x[i])

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())

s = [[''for i in range(W+2)]for j in range(H+2)]
for i in range(H):
    s[i+1][1:W+1] = input()
    for j in range(W):
        if s[i+1][j+1] == '.':
            s[i+1][j+1] = str(1*(sum(s[k+i][j:j+3].count('#') for k in range(3))))
    print(*s[i+1][1:W+1],sep='')

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[['.']*(w+2),['.']*(w+2)]
for i in range(h):
    s.insert(-1,list('.'+input()+'.'))
for i in range(h):
    for j in range(w):
        a=0
        if s[i+1][j+1]=='.':
            if s[i][j]=='#':
                a+=1
            if s[i+1][j]=='#':
                a+=1
            if s[i+2][j]=='#':
                a+=1
            if s[i][j+1]=='#':
                a+=1
            if s[i+2][j+1]=='#':
                a+=1
            if s[i][j+2]=='#':
                a+=1
            if s[i+1][j+2]=='#':
                a+=1
            if s[i+2][j+2]=='#':
                a+=1
            s[i][j]=a
for i in range(h):
    for j in range(w):
        print(s[i+1][j+1],end='')
    print('')

Submission status: Wrong Answer

Solution code:
h, w = list(map(int, input().split()))
s = [[c for c in input()] for i in range(h)]
ans = [[0] * w for i in range(h)]

dx = [1, 0, -1, 0, 1, -1, -1, -1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        num = 0
        for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]
            if ni < 0 or h <= ni:
                continue
            if nj < 0 or w <= nj:
                continue
            if s[ni][nj] == '#':
                num += 1
        s[i][j] = num
for l in s:
    print(''.join([str(c) for c in l]))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

ar = [["."] * w for i in range(h)]

for i in range(h):
    st = input()
    for j, s in enumerate(st):
        ar[i][j] = s

for i in range(h):
    for j in range(w):
        if ar[i][j] == ".":
            sum = 0
            for ii in range(3):
                for jj in range(3):
                    if i+ii >h or j + jj > w:
                        continue
                    
                    if ar[i + ii - 1][j + jj -1] == "#":
                        sum += 1
            ar[i][j] = str(sum)



for i in ar:
    print("".join(i))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
grid = []
for i in range(h+2):
    grid.append([0]*(w+2))

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            grid[i + 1][j + 1] = '#'

            if grid[i][j] != '#':
                grid[i][j] += 1

            if grid[i][j + 1] != '#':
                grid[i][j + 1] += 1

            if grid[i][j+2] != '#':
                grid[i][j+2] += 1

            if grid[i+1][j] != '#':
                grid[i+1][j] += 1

            if grid[i+1][j+2] != '#':
                grid[i+1][j+2] += 1

            if grid[i+2][j] != '#':
                grid[i+2][j] += 1

            if grid[i+2][j+1] != '#':
                grid[i+2][j+1] += 1

            if grid[i+2][j+2] != '#':
                grid[i+2][j+2] += 1


ans = []
for i in range(h):
    ans.append([0]*(w))

for i in range(1, h+1):
    for j in range(1, w+1):
        ans[i-1][j-1] = grid[i][j]

print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
M=[]
for i in range(h):
    l=list(input().replace('.','0').replace('#','9'))
    l=list(map(int,l))
    M.append(l)
for i in range(h):
    for j in range(w):
        if M[i][j]==9:
            M[i][j]='#'
            if i+1 in range(h):
                M[i+1][j]+=1
            if i-1 in range(h):
                M[i-1][j]+=1
            if j+1 in range(w):
                M[i][j+1]+=1
            if j-1 in range(w):
                M[i][j-1]+=1
for i in range(h):
    print(*M[i],sep='')

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
matrix = [input() for i in range(H)] 
ans = ""  
for h in range(H):
        for w in range(W):
                bomb = 0 
                for i in range(-1, 2): 
                        if 0 <= h - 1 and 0 <= w + i < W:
                                if matrix[h - 1][w + i] == "#":
                                        bomb += 1
                        if h + 1 < H and 0 <= w + i < W:
                                if matrix[h + 1][w + i] == "#":
                                        bomb += 1
                for i in range(-1, 2, 2): 
                        if 0 <= w + i < W:
                                if matrix[h][w + i] == "#":
                                        bomb += 1
                if matrix[h][w] == "#":
                        ans += "#" 
                else:
                        ans += str(bomb)
                if W - 1 > 0:
                        if w % (W - 1) == 0 and w != 0:
                                ans += "\n"
print(ans)

Submission status: Wrong Answer

Solution code:
HW = list(map(int,input().split()))
H=HW[0]
W=HW[1]
S = [list(input()) for i in range(H)]
cnt=0
for i in range(H):
    for j in range(W):
        if S[i][j]=='#':
            continue
        try:
            if S[i-1][j-1]=='#':
                      cnt=cnt+1            
        except:
            None
                      
        try:
            if S[i-1][j]=='#':
                      cnt=cnt+1
        except:
            None
        try:
            if S[i-1][j+1]=='#':
                      cnt=cnt+1
        except:
            None
        try:
            if S[i][j-1]=='#':
                      cnt=cnt+1
        except:
            None
        try:
            if S[i][j+1]=='#':
                      cnt=cnt+1
        except:
            None
        try:
            if S[i+1][j-1]=='#':
                      cnt=cnt+1
        except:
            None
        try:
            if S[i+1][j]=='#':
                      cnt=cnt+1
        except:
            None
        try:
            if S[i+1][j+1]=='#':
                      cnt=cnt+1
        except:
            None
        S[i][j]=cnt
        cnt=0

for a in S:
    print(*a, sep='')

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
slis=[]
a=["."]*(w+2)
slis.append(a)
for i in range(h):
	s=list("."+input()+".")
	slis.append(s)
slis.append(a)
for i in range(1,h+1):
	for j in range(1,w+1):
		a=slis[i-1][j-1:j+2]+slis[i][j-1:j+2]+slis[i+1][j-1:j+2]
		if slis[i][j] != "#":
			slis[i][j]=a.count("#")
for i in range(1,h+1):
	print(*slis[i][1:w+1])

Submission status: Wrong Answer

Solution code:
import itertools
H,W = map(int,input().split())
grid = []
for i in range(H):
    a = input()
    l = []
    for i in a:
        if(i == "."):
            i = 0
        l.append(i)
    grid.append(l)
for i in range(H):
    for j in range(W):
        if(grid[i][j] != "#"):
            continue
        for dx,dy in itertools.product([-1,0,1],repeat=2):
            x = i + dx
            y = j + dy
            if(x<0 or H<=x):
                continue
            if(y<0 or W <= y):
                continue
            if grid[x][y] == "#":
                continue
            grid[x][y] += 1
for Row in grid:
    print(*Row,sep=" ")

Submission status: Wrong Answer

Solution code:
import itertools
H,W = map(int,input().split())
grid = []
for i in range(H):
    a = input()
    l = []
    for i in a:
        if(i == "."):
            i = 0
        l.append(i)
    grid.append(l)
for i in range(H):
    for j in range(W):
        if(grid[i][j] != "#"):
            continue
        for dx,dy in itertools.product([-1,0,1],repeat=2):
            x = i + dx
            y = j + dy
            if(x<0 or H<=x):
                continue
            if(y<0 or W <= y):
                continue
            if grid[x][y] == "#":
                continue
            grid[x][y] += 1
for Row in grid:
    print(*Row)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[['.']*(w+2),['.']*(w+2)]
for i in range(h):
    s.insert(-1,list('.'+input()+'.'))
for i in range(h):
    for j in range(w):
        a=0
        if s[i+1][j+1]=='.':
            if s[i][j]=='#':
                a+=1
            if s[i+1][j]=='#':
                a+=1
            if s[i+2][j]=='#':
                a+=1
            if s[i][j+1]=='#':
                a+=1
            if s[i+2][j+1]=='#':
                a+=1
            if s[i][j+2]=='#':
                a+=1
            if s[i+1][j+2]=='#':
                a+=1
            if s[i+2][j+2]=='#':
                a+=1
            s[i][j]=a
for i in range(h):
    for j in range(w):
        print(s[i][j],end='')
    print('')

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[input() for _ in range (h)]
for i in range(h):
  l=""
  for j in range(w):
    if s[i][j]=="#":
      l+="#"
    else:
      l+=str(sum([t[max(0,j-1):min(w,j+2)].count("#") for t in s[max(0,i-1):min(h,i+2)]]))
print(l)

Submission status: Wrong Answer

Solution code:
import copy
h,w = map(int,input().split())
ls = []
for i in range(h):
    s = list(input())
    ls.append(s)
    
ls2 = copy.copy(ls)

for i in range(h):
    for j in range(w):
        ans = 0
        if ls[i][j] == "#":
          ans = "#"
        else:
          if j+1 < w-1:#right
            if ls[i][j+1] == "#":
                ans += 1
            if i+1 < h-1:
              if ls[i+1][j+1] == "#":
                ans += 1
            if i-1 >= 0:
              if ls[i-1][j+1] == "#":
                ans += 1
          if i+1 < h-1:#down
            if ls[i+1][j] == "#":
                ans += 1
          if j-1 >= 0:#left
            if ls[i][j-1] == "#":
                ans += 1
            if i+1 < h-1:
              if ls[i+1][j-1] == "#":
                ans += 1
            if i-1 >= 0:
              if ls[i-1][j-1] == "#":
                ans += 1
          if i-1 >= 0:#up
            if ls[i-1][j] == "#":
                ans += 1
         
        ls2[i][j] = ans
        
for i in range(h):
  for j in range(w):
  	print(ls2[i][j],end="")
  print("")

Submission status: Wrong Answer

Solution code:
import numpy as np
line = input().split()
h = int(line[0])
w = int(line[1])
grid = []
for i in range(h):
  grid.append(list(input()))
              
grid = np.array(grid)


def get8(grid,y,x):
  v = [None]*8
  try :
    if y < 0 :
      pass
    else:
      v[0] = grid[y-1][x]
  except :
    pass
  try :
    if y-1 < 0 :
      pass
    else :
      v[1] = grid[y-1][x+1]
  except :
    pass  
  try :
    v[2] = grid[y][x+1]
  except :
    pass
  try :
    v[3] = grid[y+1][x+1]
  except :
    pass
  try :
    v[4] = grid[y+1][x]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[5] = grid[y+1][x-1]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[6] = grid[y][x-1]
  except :
    pass
  try :
    if y-1 < 0 or x-1 < 0 :
      pass
    else :
    	v[7] = grid[y-1][x-1]
        
  except :
    pass
  
  return v

for i in range(h) :
  for j in range(w) :
    v = get8(grid,i,j)
    if grid[i][j] == "." :
    	grid[i][j] = v.count("#")
  
  
for i in range(h) :
  print("".join(grid[i]))

Submission status: Wrong Answer

Solution code:
import numpy as np
line = input().split()
h = int(line[0])
w = int(line[1])
grid = []
for i in range(h):
  grid.append(list(input()))
              
grid = np.array(grid)


def get8(grid,y,x):
  v = [None]*8
  try :
    if y < 0 :
      pass
    else:
      v[0] = grid[y-1][x]
  except :
    pass
  try :
    if y-1 < 0 :
      pass
    else :
      v[1] = grid[y-1][x+1]
  except :
    pass  
  try :
    v[2] = grid[y][x+1]
  except :
    pass
  try :
    v[3] = grid[y+1][x+1]
  except :
    pass
  try :
    v[4] = grid[y+1][x]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[5] = grid[y+1][x-1]
  except :
    pass
  try :
    if x-1 < 0 :
      pass
    else:
      v[6] = grid[y][x-1]
  except :
    pass
  try :
    if y-1 < 0 or x-1 < 0 :
      pass
    else :
    	v[7] = grid[y-1][x-1]
        
  except :
    pass
  
  return v

for i in range(h) :
  for j in range(w) :
    v = get8(grid,i,j)
    if i== 3 and j == 0 :
      print(v)
    if grid[i][j] == "." :
    	grid[i][j] = v.count("#")
  
  
print(grid)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
s=[]
cnt=0
for i in range(h):
    s.append(list(input()))
if h==1 and w==1:
    if s[0][0]=='#':
        print('#')
    else:
        print(0)
elif h==1:
    for i in range(w):
        if s[0][i]=='#':
            continue
        if i==0:
            if s[0][i+1]=='#':
                s[0][i]=='1'
            else:
                s[0][i]=='0'
        elif i==w-1:
            if s[0][i-1]=='#':
                s[0][i]=='1'
            else:
                s[0][i]=='0'
        else:
            if s[0][i+1]=='#':
                cnt+=1
            if s[0][i-1]=='#':
                cnt+=1
            s[0][i]=str(cnt)
            cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''
elif w==1:
    for i in range(h):
        if s[i][0]=='#':
            continue
        if i==0:
            if s[i+1][0]=='#':
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
        elif i==h-1:
            if s[i-1][0]=='#':
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
        else:
            if s[i+1][0]=='#':
                cnt+=1
            if s[i-1][0]=='#':
                cnt+=1
            s[i][0]=str(cnt)
            cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''
else:
    for j in range(h):
        for k in range(w):
            if j==0:
                if s[j][k]=='#':
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
            elif j==h-1:
                if s[j][k]=='#':
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0 
            elif 0<j<h-1:
                if s[j][k]=='#':
                    continue
                else:
                    if k==0:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    elif k==w-1:
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
                    else:
                        if s[j][k+1]=='#':
                            cnt+=1
                        if s[j-1][k]=='#':
                            cnt+=1
                        if s[j-1][k+1]=='#':
                            cnt+=1
                        if s[j-1][k-1]=='#':
                            cnt+=1
                        if s[j+1][k]=='#':
                            cnt+=1
                        if s[j+1][k+1]=='#':
                            cnt+=1
                        if s[j][k-1]=='#':
                            cnt+=1
                        if s[j+1][k-1]=='#':
                            cnt+=1
                        s[j][k]=str(cnt)
                        cnt=0
    ans=''
    for l in range(h):
        for b in range(w):
            ans+=s[l][b]
        print(ans)
        ans=''