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:
h, w = map(int, input().split())
s = []
[s.append(list(input())) for x in range(h)]
for i in range(h):
    for j in range(w):
        if "." in s[i][j]:
            s[i][j] = 0
for i in range(h):
    for j in range(w):
        try:
            if "#" in s[i][j]:
                if i != 0 and j != 0:
                    s[i - 1][j - 1] += 1
        except IndexError:
            pass
        except TypeError:
            pass
        try:
            if "#" in s[i][j]:
                if i != 0:
                    s[i - 1][j] += 1
        except IndexError:
            pass
        except TypeError:
            pass
        try:
            if "#" in s[i][j]:
                if i != 0:
                    s[i - 1][j + 1] += 1
        except IndexError:
            pass
        except TypeError:
            pass
        try:
            if "#" in s[i][j]:
                if j != 0:
                    s[i][j - 1] += 1
        except IndexError:
            pass
        except TypeError:
            pass
        try:
            if "#" in s[i][j]:
                s[i][j + 1] += 1
        except IndexError:
            pass
        except TypeError:
            pass
        try:
            if "#" in s[i][j]:
                if i != 5 and j != 0:
                    s[i + 1][j - 1] += 1
        except IndexError:
            pass
        except TypeError:
            pass
        try:
            if "#" in s[i][j]:
                s[i + 1][j] += 1
        except IndexError:
            pass
        except TypeError:
            pass
        try:
            if "#" in s[i][j]:
                s[i + 1][j + 1] += 1
        except IndexError:
            pass
        except TypeError:
            pass
for i in s:
    print(''.join(map(str, i)))

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = []
[s.append(list(input())) for x in range(h)]
for i in range(h):
    for j in range(w):
        if "." in s[i][j]:
            s[i][j] = 0
for i in range(h):
    for j in range(w):
        try:
            if "#" in s[i][j]:
                s[i - 1][j - 1] += 1
                s[i - 1][j] += 1
                s[i - 1][j + 1] += 1
                s[i][j - 1] += 1
                s[i][j + 1] += 1
                s[i + 1][j - 1] += 1
                s[i + 1][j] += 1
                s[i + 1][j + 1] += 1
        except IndexError:
            continue
        except TypeError:
            continue
for i in s:
    print(''.join(map(str, i)))

Submission status: Wrong Answer

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

M = [list("." + input() + ".") for i in range(H)]
M.insert(0, list(("."*(W+2))))
M.append(list("."*(W+2)))
print(M)

cnt = 0
Ans = []
for i in range(1, H+1):
    for j in range(1, W+1):
        if M[i][j] == ".":
            for k in range(-1, 2):
                if M[i+k][j-1] == "#":
                    cnt += 1
                if M[i+k][j] == "#":
                    cnt += 1
                if M[i+k][j+1] == "#":
                    cnt += 1
            print(cnt, end = "")
            cnt = 0
        else:
            print("#", end = "")
    print()

Submission status: Wrong Answer

Solution code:
# いつもの探索でいいです 終了

h, w = map(int, input().split())
table = []
for _ in range(h):
  table.append(list(input()))

for i in range(h):
  for j in range(w):
    if table[i][j] != "#":
      sub_h = [0] if i==0 else [-1, 0, 1] if 0<i<h-1 else [0, 1] if i==0 else [-1, 0]
      sub_w = [0] if j==0 else [-1, 0, 1] if 0<j<w-1 else [0, 1] if j==0 else [-1, 0]
      count = 0
      for sh in sub_h:
        for sw in sub_w:
          if sh == sw == 0:
            continue
          if table[i+sh][j+sw] == "#":
            count += 1
      table[i][j] = str(count)

for i in range(h):
  print("".join(table[i]))

Submission status: Wrong Answer

Solution code:
h,w =map(int,input().split())
s = [input() for i in range(h)]
dx = [1,1,1,0,0,-1,-1,-1]
dy = [1,-1,0,1,-1,1,-1,0]
s_out =["" for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
                s_out[i] += "#"
                continue
        for k in range(8):
            x = i + dx[k]
            y = j + dy[k]
            count = 0
            if x >= 0 and x <= h-1 and y >= 0 and y<= w-1 and s[x][y]=="#":
                count += 1
        s_out[i] += str(count)
for i in s_out:
    print(i)

Submission status: Wrong Answer

Solution code:
import numpy as np

def main():
    H, W = map(int, input().split())
    s = [input() for i in range(H)]
    
    diffs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for y in range(H):
        anstr = ''
        for x in range(W):
            if s[y][x] == '#': continue
            count = 0
            for dx, dy in diffs:
                if (x + dx < 0) or (x + dx > W - 1) or (y + dy < 0) or (y + dy > H - 1):
                    continue
                if s[y + dy][x + dx] == '#':
                    count += 1
            anstr += str(count)
        print(anstr)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
hw = input().split()
h = int(hw[0])
w = int(hw[1])

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

ans_field = []
for i in range(h):
  ans_field.append('')
  for j in range(w):
    if field[i][j]=='.':
      s = ''
      peris = [[i-1, j-1], [i-1, j], [i-1, j+1], [i, j-1], [i, j+1], [i+1, j-1], [i+1, j], [i+1, j+1]]
      for peri in peris:
        try:
          s += field[peri[0]][peri[1]]
        except:
          pass
      sharp = 0
      for letter in s:
        if letter=='#':
          sharp += 1
      ans_field[i] += str(sharp)
    else:
      ans_field[i] += '#'

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

Submission status: Wrong Answer

Solution code:
hw = input().split()
h = int(hw[0])
w = int(hw[1])

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

ans_field = []
for i in range(h):
  ans_field.append('')
  for j in range(w):
    s = ''
    peris = [[i-1, j-1], [i-1, j], [i-1, j+1], [i, j-1], [i, j+1], [i+1, j-1], [i+1, j], [i+1, j+1]]
    for peri in peris:
      try:
        s += field[peri[0]][peri[1]]
      except:
        pass
    sharp = 0
    for letter in s:
      if letter=='#':
        sharp += 1
    ans_field += str(sharp)

for i in range(h):
  print(ans_field)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
ans = [['#' for i in range(W)] for i in range(H)]
for x in range(W):
    for y in range(H):
        num = 0
        if S[y][x] == '.':
            for dx, dy in [1, 0], [-1, 0], [0, 1], [0, -1], [1, 1]:
                if 0 <= y + dy <= H - 1 and 0 <= x + dx <= W - 1:
                    if S[y + dy][x + dx] == '#':
                        num += 1
            ans[y][x] = str(num)

for i in ans:
    print(''.join(i))

Submission status: Wrong Answer

Solution code:
h,w = (int(i) for i in input().split())
glid = [[i for i in input()] for i in range(h)]

def search(x,y):
    try:
        if glid[x][y] == '#':   return 1
        else:   return 0
    except:
        return 0


ans = []
for i in range(h):
    ans.append([0 for _ in range(w)])
    for j in range(w):
        if glid[i][j] == '#':
            ans[i][j] = '#'
        else:
            li = [(i-1,j-1),(i,j-1),(i-1,j),(i+1,j+1),(i,j+1),(i+1,j),
                  (i+1,j-1),(i-1,j+1)]
            for x,y in li:
                ans[i][j] += search(x,y)

for i in range(h):
    for j in range(w):
        print(ans[i][j],end='')
    print()

Submission status: Wrong Answer

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

for i in range(W):
    for j in range(H):
        cnt = 0
        try:
            if f[i-1][j-1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i-1][j] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i-1][j+1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i][j-1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i][j+1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i+1][j-1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i+1][j] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i+1][j+1] == '#':
                cnt += 1
        except:
            pass
        if f[i][j] == '#':
            ans[i][j] = '#'
        else:
            ans[i][j] = str(cnt)
            
for i in range(W):
    print(''.join(ans[i]))

Submission status: Wrong Answer

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

for i in range(W):
    for j in range(H):
        cnt = 0
        try:
            if f[i-1][j-1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i-1][j] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i-1][j+1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i][j-1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i][j+1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i+1][j-1] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i+1][j] == '#':
                cnt += 1
        except:
            pass
        try:
            if f[i+1][j+1] == '#':
                cnt += 1
        except:
            pass
        if f[i][j] == '#':
            ans[i][j] = '#'
        else:
            ans[i][j] = str(cnt)

Submission status: Wrong Answer

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

L = [""]*H
for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
        L[i] += "#"
    else:
      c = sum(t[max(0,j-1):min(W,j+2)].count("#") for t in S[max(0,i-1):min(W,i+2)])
      L[i] += str(c)
for i in range(H):
  print(L[i])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
field=[]
nfield=[[] for i in range(h)]
for i in range(h):
  field.append(input())
def check(a,b):
  num=0
  if field[a][b]=="#":
    return
  for i in range(a-1,a+2):
    for j in range(b-1,b+2):
      try:
        if j !=-1 and field[i][j]=="#":
          num+=1
      except:
        pass
  return num
for i in range(h):
  for j in range(w):
    if field[i][j]==".":
      nfield[i].append(check(i,j))
    else:
      nfield[i].append("#")
for i in range(h):
  for j in range(w):
    if j != w-1:
      print(nfield[i][j],end="")
    else:
      print(nfield[i][j])

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[]
for _ in range(H):
    a=["."]
    a.extend(list(map(str,input())))
    a.append(".")
    S.append(a)

S.insert(0,["."]*(W+2))
S.insert(H,["."]*(W+2))

# print(S)

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]=='.':
            S[i][j]=str((S[i-1][j-1:j+2]+S[i][j-1:j+2]+S[i+1][j-1:j+2]).count('#'))
    print(''.join(S[i][1:-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

H, W = map(int, input().split())
# 0行目を作っておく，端の処理を楽にするために，上下左右に拡張する。
line_0 = ''
for i in range(W+2):
    line_0 += '.'

grid = [line_0]

for i in range(H):
    line_i = input()
    line_i = '.' + line_i + '.'
    grid.append(line_i)
grid.append(line_0)

ans = []
for i in range(1, H+1):
    flag_line = ''
    for j in range(1, W+1):
        # 自分が#だったら
        me = grid[i]
        me_j = me[j:j+1]
        if me_j == '#':
            flag_line += '#'
        else:
            count = 0
            # 一個上の行
            if grid[i-1][j-1:j] == '#':
                count += 1
            if grid[i-1][j:j+1] == '#':
                count += 1
            if grid[i-1][j+1:j+2] == '#':
                count += 1
            # 同じ行
            if grid[i][j-1:j] == '#':
                count += 1
            if grid[i-1][j+1:j+2] == '#':
                count += 1
            # 1個下
            if grid[i+1][j-1:j] == '#':
                count += 1
            if grid[i+1][j:j+1] == '#':
                count += 1
            if grid[i+1][j+1:j+2] == '#':
                count += 1
            flag_line += str(count)
    print(flag_line)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            cnt = 0
            for dx in [-1, 0, 1]:
                for dy in [-1, 0, 1]:
                    if 0 <= dx + j <= (w-1) and 0 <= dy + h <= (h-1):
                        if s[i+dy][j+dx] == "#":
                            cnt += 1
            s[i][j] = str(cnt)
for k in s:
    print("".join(k))

Submission status: Wrong Answer

Solution code:
import numpy as np

H,W = (int(n) for n in input().split())
arr =np.zeros((H,W),dtype=int)

mines = []
for h in range(H):
    row = list(input())
    for w in range(W):
        if(row[w]=="#"):
            arr[h-1:h+2,w-1:w+2] += 1
            mines.append((h,w))
            
arr = arr.astype(str)
for mine in mines:
    arr[mine] = "#"

for row in arr :
    print("".join(row))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
	H, W = map(int, input().split())
	l = []
	ans = [[0] * W for i in range(H)]
	for i in range(H):
		s = input()
		for j in range(W):
			if s[j] == '#':
				ans[i][j] = '#'
				right = j+1 < W 
				left = -1 < j-1 
				up =  i+1 < H
				down = -1 < i-1
				if right and s[j+1] != '#':
					ans[i][j+1] += 1
				if left and s[j-1] != '#':
					ans[i][j-1] += 1
				if up and ans[i+1][j] != '#':
					ans[i+1][j] += 1
					if right and ans[i+1][j+1] != '#':
						ans[i+1][j+1] += 1
					if left and ans[i+1][j-1] != '#':
						ans[i+1][j-1] += 1
				if down and ans[i-1][j] != '#':
					ans[i-1][j] += 1
					if right and ans[i-1][j+1] != '#':
						ans[i-1][j+1] += 1
					if left and ans[i-1][j-1] != '#':
						ans[i-1][j-1] += 1
	
	for s in ans:
		tmp = map(str,s)
		print(''.join(tmp))


if __name__ == '__main__':
	import sys
	sys.exit(main())

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
S=[["." for _ in range(w+2)]]
S+=[["."]+[i for i in input()]+["."]for _ in range(h)]
S+=[["." for _ in range(w+2)]]
print(S)
for i in range(1, h+1):
    l=""
    for j in range(1, w+1):
        if S[i][j]=="#":
            l+="#"
        else:
            # l+=str(int(S[i][j]=="."))
            l+=str(sum(S[i+n][j+m]=="#" for n in range(-1, 2, 1) for m in range(-1, 2, 1)))
    print(l)

Submission status: Wrong Answer

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

S = []

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

for _ in range(H):
    S.append(list(input()))

print(S)

for i in range(H):
    for j in range(W):
        if(S[i][j] == '#'): continue
        
        num = 0
        for dxy in dxy_list:
            ni = i + dxy[0]
            nj = j + dxy[1]

            if (ni < 0 or H <= ni): continue
            if (nj < 0 or W <= nj): continue
            
            if(S[ni][nj] == '#'): num += 1

        S[i][j] = str(num)

for row in S:
    print(''.join(row))

Submission status: Wrong Answer

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

S = []

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

for _ in range(H):
    S.append(list(input()))


for i in range(H):
    for j in range(W):
        if(S[i][j] == '#'): continue
        
        num = 0
        for dxy in dxy_list:
            ni = i + dxy[0]
            nj = j + dxy[1]

            if (ni < 0 or H <= ni): continue
            if (nj < 0 or W <= nj): continue
            
            if(S[ni][nj] == '#'): num += 1

            S[i][j] = str(num)

for row in S:
    print(''.join(row))

Submission status: Wrong Answer

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

S = []
result = []

for _ in range(H):
    S.append(list(input()))

for i in range(H):

    result_row = ''

    for j in range(W):
        if (S[i][j] == '.'):

            try:
                a = 1 if S[i-1][j] == '#' else 0
            except IndexError:
                a = 0

            try:
                b = 1 if S[i+1][j] == '#' else 0
            except IndexError:
                b = 0

            try:
                c = 1 if S[i][j-1] == '#' else 0
            except IndexError:
                c = 0

            try:
                d = 1 if S[i][j+1] == '#' else 0
            except IndexError:
                d = 0

            try:
                e = 1 if S[i-1][j-1] == '#' else 0
            except IndexError:
                e = 0

            try:
                f = 1 if S[i-1][j+1] == '#' else 0
            except IndexError:
                f = 0

            try:
                g = 1 if S[i+1][j-1] == '#' else 0
            except IndexError:
                g = 0

            try:
                h = 1 if S[i+1][j+1] == '#' else 0
            except IndexError:
                h = 0

            result_row += str(a+b+c+d+e+f+g+h)

                
        elif (S[i][j] == '#'):
            result_row += '#'

    print(result_row)

Submission status: Wrong Answer

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

S = []
result = []

for _ in range(H):
    S.append(list(input()))


print(S)


for i in range(H):

    result_row = ''

    for j in range(W):
        if (S[i][j] == '.'):

            try:
                a = 1 if S[i-1][j] == '#' else 0
            except IndexError:
                a = 0

            try:
                b = 1 if S[i+1][j] == '#' else 0
            except IndexError:
                b = 0

            try:
                c = 1 if S[i][j-1] == '#' else 0
            except IndexError:
                c = 0

            try:
                d = 1 if S[i][j+1] == '#' else 0
            except IndexError:
                d = 0

            try:
                e = 1 if S[i-1][j-1] == '#' else 0
            except IndexError:
                e = 0

            try:
                f = 1 if S[i-1][j+1] == '#' else 0
            except IndexError:
                f = 0

            try:
                g = 1 if S[i+1][j-1] == '#' else 0
            except IndexError:
                g = 0

            try:
                h = 1 if S[i+1][j+1] == '#' else 0
            except IndexError:
                h = 0

            result_row += str(a+b+c+d+e+f+g+h)

                
        elif (S[i][j] == '#'):
            result_row += '#'

    print(result_row)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
S = [list(input()) for i in range(H)]
print(S)
dx = [1,1,1,0,0,-1,-1,-1]
dy = [1,0,-1,1,-1,1,0,-1]
for j in range(H):
    for i in range(W):
        cnt = 0
        for k in range(8):
            if 0<=i+dx[k]<W and 0<=j+dy[k]<H:
                if S[j+dy[k]][i+dx[k]]=="#":
                    cnt += 1
        if S[j][i] == "#":
            continue
        else:
            S[j][i] = cnt


for i in range(H):
    for j in range(W):
        S[i][j] = str(S[i][j])

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

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
S = []
for i in range(H):
    S.append(list(input()))
    #S.append(list(i for i in input()))
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            cnt = 0
            if i != 0:
                cnt += S[i-1][max(0,j-1):min(j+1,W-1)+1].count('#')
            cnt += S[i-1][max(0,j-1):min(j+1,W-1)+1].count('#')
            if i != H-1:
                cnt += S[i-1][max(0,j-1):min(j+1,W-1)+1].count('#')
            S[i][j] = str(cnt)
for i in range(H):
    print(''.join(S[i]))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(list(input()))
if h == 1 and w == 1:
    print(S[0][0])
if h == 1 and w == 2:
    if S == [['.','#']]:
        print('1#')
    if S == [['#','#']]:
        print('##')
    if S == [['#','.']]:
        print('#1')
    if S == [['.','.']]:
        print('..')
if h == 1 and w >= 3:
    for g in range(w):
        if S[0][0] == '.':
            if S[0][1] == '#':
                S[0][0] = '1'
        if 1 <= g <= w-2:
            if S[0][g] == '.':
                S[0][g] = str(S[0][g-1:g+2].count('#'))
        if S[0][w-1] == '.':
            if S[0][w-2] == '#':
                S[0][w-1] = '1'
    print(''.join(S[0]))
if h >= 3 and w == 1:
    for y in range(h):
        if S[0][0] == '.':
            if S[1][0] == '#':
                S[0][0] = '1'
        if 1 <= y <= h-2:
            if S[y][0] == '.':
                count = 0
                if S[y-1][0] == '#':
                    count += 1
                if S[y+1][0] == '#':
                    count += 1
                S[y][0] = int(count)
        if S[h-1][0] == '.':
            if S[h-2][0] == '#':
                S[h-1][0] = '1'
    for p in S:
        print(p[0])
if h == 2 and w == 1:
    if S == [['.'],['.']]:
        print('.')
        print('.')
    if S == [['#'],['.']]:
        print('#')
        print('1')
    if S == [['.'],['#']]:
        print('1')
        print('#')
    if S == [['#'],['#']]:
        print('#')
        print('#')
if h >= 2 and w >= 2:
    for k in range(h):
        for j in range(w):
            if S[k][j] == '.':
                if k == 0 and j == 0:
                    count = 0
                    if S[0][1] == '#':
                        count += 1 
                    count += S[1][0:2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and 1 <= j <= w-2:
                    count = 0
                    count += S[0][j-1:j+2].count('#')
                    count += S[1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and j == w-1:
                    count = 0
                    if S[0][j-1] == '#':
                        count += 1
                    count += S[1][j-1:].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    if S[k][1] == '#':
                        count += 1 
                    count += S[k+1][0:2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    count += S[k+1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    count += S[k+1][j-1:].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    count += S[k][1].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    S[k][j] = str(count)
    for p in S:
        print(''.join(p))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(list(input()))
if h == 1 and w == 1:
    print(S[0][0])
if h == 1 and w == 2:
    if S == [['.','#']]:
        print('1#')
    if S == [['#','#']]:
        print('##')
    if S == [['#','.']]:
        print('#1')
    if S == [['.','.']]:
        print('..')
if h == 1 and w >= 3:
    for g in range(w):
        if S[0][0] == '.':
            if S[0][1] == '#':
                S[0][0] = '1'
        if 1 <= g <= w-2:
            if S[0][g] == '.':
                S[0][g] = str(S[0][g-1:g+2].count('#'))
        if S[0][w-1] == '.':
            if S[0][w-2] == '#':
                S[0][w-1] == '1'
    print(''.join(S[0]))
if h >= 3 and w == 1:
    for y in range(h):
        if S[0][0] == '.':
            if S[1][0] == '#':
                S[0][0] = '1'
        if 1 <= y <= h-2:
            if S[y][0] == '.':
                count = 0
                if S[y-1][0] == '#':
                    count += 1
                if S[y+1][0] == '#':
                    count += 1
                S[y][0] = int(count)
        if S[h-1][0] == '.':
            if S[h-2][0] == '#':
                S[h-1][0] = '1'
    for p in S:
        print(p[0])
if h == 2 and w == 1:
    if S == [['.'],['.']]:
        print('.')
        print('.')
    if S == [['#'],['.']]:
        print('#')
        print('1')
    if S == [['.'],['#']]:
        print('1')
        print('#')
    if S == [['#'],['#']]:
        print('#')
        print('#')
if h >= 2 and w >= 2:
    for k in range(h):
        for j in range(w):
            if S[k][j] == '.':
                if k == 0 and j == 0:
                    count = 0
                    if S[0][1] == '#':
                        count += 1 
                    count += S[1][0:2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and 1 <= j <= w-2:
                    count = 0
                    count += S[0][j-1:j+2].count('#')
                    count += S[1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == 0 and j == w-1:
                    count = 0
                    if S[0][j-1] == '#':
                        count += 1
                    count += S[1][j-1:].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    if S[k][1] == '#':
                        count += 1 
                    count += S[k+1][0:2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    count += S[k+1][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif 1 <= k <= h-2 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    count += S[k+1][j-1:].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == 0:
                    count = 0
                    count += S[k-1][0:2].count('#')
                    count += S[k][1].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and 1 <= j <= w-2:
                    count = 0
                    count += S[k-1][j-1:j+2].count('#')
                    count += S[k][j-1:j+2].count('#')
                    S[k][j] = str(count)
                elif k == h-1 and j == w-1:
                    count = 0
                    count += S[k-1][j-1:].count('#')
                    count += S[k][j-1:].count('#')
                    S[k][j] = str(count)
    for p in S:
        print(''.join(p))