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: Accepted

Solution code:
y,x = map(int, raw_input().split(" "))
mines = []
answer = []
for t in range(y):
    mines.append(list(raw_input()))

for i in range(y):
    notes = ""
    for h in range(x):
        if mines[i][h] == '.':
            count = 0
            if i != 0:
                if h != 0:
                    if mines[i-1][h-1] == "#":
                        count += 1
                if mines[i-1][h] == "#":
                    count += 1
                if h != x-1:
                    if mines[i-1][h+1] == "#":
                        count += 1
            if h != 0:
                if mines[i][h-1] == "#":
                    count += 1
            if mines[i][h] == "#":
                count += 1
            if h != x-1:
                if mines[i][h+1] == "#":
                    count += 1
            if i != y-1:
                if h != 0:
                    if mines[i+1][h-1] == "#":
                        count += 1
                if mines[i+1][h] == "#":
                    count += 1
                if h != x-1:
                    if mines[i+1][h+1] == "#":
                        count += 1
            mines[i][h] = str(count)
    notes = "".join(mines[i])
    print notes

Submission status: Accepted

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

m = []

for i in range(h):
    l = list(input())
    m.append([ '#' if i=='#' else 0 for i in l])

for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            for u in range(3):
                for v in range(3):
                    if u==1 and v==1:
                        pass
                    else:
                        try:
                            y = i+u-1
                            x = j+v-1
                            if y < 0 or x < 0:
                                continue

                            if m[y][x] != '#':
                                m[y][x] += 1

                        except:
                            pass

for l in m:
    s = map(str, l)
    print(''.join(s))

Submission status: Accepted

Solution code:
def countBombs(y,x):
	count = 0
	for n in range(y-1,y+2):
		for m in range(x-1,x+2):
			# print(n)
			# print(m)
			if n < 1 or m < 0\
			 or n > int(inputsMassNum[0]) or m > int(inputsMassNum[1])-1:
				pass
			elif inputsBombs[n][m] == "#":
				count += 1

	return count
	

inputsBombs = [[]] 

#3*3想定
if __name__ == '__main__':
	input1 = input()
	inputsMassNum = input1.split()#3 3

	for i in range(0,int(inputsMassNum[0])):#0~3(3回)
		# print(i)
		input2 = input()#
		inputsBombs.append(list(input2)) #[0][]は空
		#  [   ] 
		#	###
		#	#.#
		#	###

		#inputBombs,「1」要素に追加
		# print(inputsBombs[i+1])
			
	for y in range(1,int(inputsMassNum[0])+1):#0~3
		for x in range(0,int(inputsMassNum[1])):
			# print(inputsBombs[y][x])
			if inputsBombs[y][x] == ".":#
				tmp = countBombs(y,x)
				inputsBombs[y][x] = tmp


	inputsBombs.pop(0)
	# for i in range(0,int(inputsMassNum[0])):
	# 	print(inputsBombs)

	for i in range(0,int(inputsMassNum[0])):
		for j in range(0,int(inputsMassNum[1])):
			print(inputsBombs[i][j],end = "")
		print("")

Submission status: Accepted

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

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        cnt = 0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                if 0 <= i + dy < H and 0 <= j + dx < W and S[i + dy][j + dx] == '#':
                    cnt += 1
        S[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
def main():
    H,W = map(int, input().split())
    field = []
    for i in range(H):
        buf=list(input())
        field.append(buf)

    for i in range(H):
        for j in range(W):
            if field[i][j]=="#":
                continue
            num = 0
            for k in range(-1,2):
                for l in range(-1,2):
                    if i+k==-1 or i+k==H or j+l==-1 or j+l==W:
                        continue
                    if field[i+k][j+l]=="#":
                        num = num +1
            field[i][j]=str(num)
    
    for i in range(H):
        print("".join(field[i]))



        





main()

Submission status: Accepted

Solution code:
h, w = map(int, input().split(' '))
mine = []
mine_result = []

def count(i, j):
    try:
        if i < 0 or j < 0:
            return None
        if mine[i][j] == '#':
            return 1
    except:
        return None

    return None

if __name__ == '__main__':
    for i in range(0, h):
        inp = list(input())
        mine.append(inp)
        mine_result.append(inp)

    for i in range(0, h):
        for j in range(0, w):
            if (mine[i][j] == '.'):
                mine_result[i][j] = sum(list(filter(None,
                    [
                        count(i + 1, j),
                        count(i - 1, j),
                        count(i, j + 1),
                        count(i, j - 1),
                        count(i + 1, j + 1),
                        count(i + 1, j - 1),
                        count(i - 1, j + 1),
                        count(i - 1, j - 1)
                    ])))
            else:
                mine_result[i][j] = '#'

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

Submission status: Accepted

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

command = (-1, 0, 1)
for row in range(h):
    ans = ""
    for col in range(w):
        if s[row][col] == "#":
            ans += "#"
        else:
            bomb = 0
            for i in command:
                for j in command:
                    if 0 <= row + i < h and 0 <= col + j < w and s[row + i][col + j] == "#":
                        bomb += 1
            ans += str(bomb)
    print(ans)

Submission status: Accepted

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

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

            continue
for h in range(H):
    l = ''
    for w in range(W):
        if S[h][w] == '#':
            l += '#'
        else:
            x = 0
            for dh in [ -1, 0, 1]:
                for dw in [-1, 0, 1]:
                    if 0 <= h + dh < H and 0 <= w + dw < W and S[h+dh][w+dw] == '#':
                        x += 1
            l += str(x)
    print(l)

Submission status: Accepted

Solution code:
import sys
h,w=sys.stdin.readline().split()
h=int(h)
w=int(w)
field=[[] for i in range(h)]
ans=[[0 for i in range(w)]for j in range(h)]
for i in range(h):
    field[i]=sys.stdin.readline()
    for j in range(w):
        if field[i][j]=='#':
            ans[i][j]=-1

for i in range(h):
    for j in range(w):
        if ans[i][j] == -1:
            continue
        if i != 0:
            if ans[i-1][j]==-1:
                ans[i][j]=ans[i][j]+1
            if j != 0:
                if ans[i-1][j-1] == -1:
                    ans[i][j]=ans[i][j]+1
            if j != (w-1):
                if ans[i-1][j+1] == -1:
                    ans[i][j]=ans[i][j]+1
        if j != 0:
            if ans[i][j-1]==-1:
                ans[i][j]=ans[i][j]+1
        if j != (w-1):
            if ans[i][j+1]==-1:
                ans[i][j]=ans[i][j]+1
        if i != (h-1):
            if ans[i+1][j]==-1:
                ans[i][j]=ans[i][j]+1
            if j != 0:
                if ans[i+1][j-1]==-1:
                    ans[i][j]=ans[i][j]+1
            if j!=(w-1):
                if ans[i+1][j+1]==-1:
                    ans[i][j]=ans[i][j]+1

for i in range(h):
    for j in range(w):
        if ans[i][j]==-1:
            print('#',end='')
        else:
            print(ans[i][j],end='')
    print('')

Submission status: Accepted

Solution code:
import sys


def solve(lines):
    split = lines[0].strip().split()
    H = int(split[0])
    W = int(split[1])

    board = list(map(lambda x: list(x.strip()), lines[1:]))
    result = [[0 for _ in range(W)] for _ in range(H)]

    for i in range(H):
        for j in range(W):
            if board[i][j] == '#':
                result[i][j] = '#'
                continue

            count = 0
            if i > 0:
                count += board[i - 1][j] == '#'
                if j > 0:
                    count += board[i - 1][j - 1] == '#'
                if j < W - 1:
                    count += board[i - 1][j + 1] == '#'
            if i < H - 1:
                count += board[i + 1][j] == '#'
                if j > 0:
                    count += board[i + 1][j - 1] == '#'
                if j < W - 1:
                    count += board[i + 1][j + 1] == '#'
            if j > 0:
                count += board[i][j - 1] == '#'
            if j < W - 1:
                count += board[i][j + 1] == '#'

            result[i][j] = str(count)

    return '\n'.join([''.join(r) for r in result])


if __name__ == '__main__':
    print(solve(sys.stdin.readlines()))

Submission status: Accepted

Solution code:
# coding: utf-8
h, w = list(map(int, input().split()))
ls = [list(input()) for y in range(h)]
ls.append(list("." * w))
ls = [list("." * w)] + ls

for i in range(h + 2):
    ls[i].append(".")
    ls[i] = ["."] + ls[i]
ans = [[0 for x in range(w+2)] for y in range(h+2)]

for y in range(1, h+1):
    for x in range(1, w+1):
        if ls[y][x] == "#":
            ans[y][x] = "#"
        else:
            ans[y][x] = [ls[y-1][x-1], ls[y-1][x], ls[y-1][x+1], ls[y][x-1], ls[y][x+1], ls[y+1][x-1], ls[y+1][x], ls[y+1][x+1]].count("#")

for i in range(1, h+1):
    print(''.join(map(str, ans[i]))[1:w+1])

Submission status: Accepted

Solution code:
#ABC075B

h,w=map(int,raw_input().split())
l=[]
for i in xrange(h):
 l.append(list(raw_input()))
for y in xrange(h):
 for x in xrange(w):
  if l[y][x]=='#':
   continue
  cnt=0
  for dy in [-1,0,1]:
   for dx in [-1,0,1]:
    ny=y+dy
    nx=x+dx
    if 0<=ny<h and 0<=nx<w:
     if l[ny][nx]=='#':
      cnt+=1
  l[y][x]=str(cnt)
for i in xrange(h):
 print ''.join(l[i])

Submission status: Accepted

Solution code:
# coding: utf-8
h,w=map(int,input().split())
field = [['.' for i in range(w+2)]]
for i in range(h):
    field.append(['.']+list(input())+['.'])
field.append(['.' for i in range(w+2)])
v = [(-1,-1),(0,-1),(1,-1),(-1,0),(1,0),(-1,1),(0,1),(1,1)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if (field[i][j]=='.'):
            count=0
            for a,b in v:
                if field[i+b][j+a]=='#':
                    count+=1
            field[i][j]=str(count)
        print(field[i][j],end='')
    print('')

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S = [ input() for _ in range(H) ]
for h in range(H):
  l = ''
  for w in range(W):
    if S[h][w] == '#':
      l += '#'
    else:
      x = 0
      for dh in [ -1, 0, 1]:
        for dw in [-1, 0, 1]:
          if 0 <= h + dh < H and 0 <= w + dw < W and S[h+dh][w+dw] == '#':
            x += 1
      l += str(x)
  print(l)

Submission status: Accepted

Solution code:
h,w = map(int, raw_input().split())
s = [raw_input() for _ in range(h)]
t = []
for i in range(h):
    u = ''
    for j in range(w):
        if s[i][j] == '.':
            c = 0
            for y in range(max(0, i-1), min(i+2, h)):
                for x in range(max(0, j-1), min(j+2, w)):
                    if s[y][x] == '#':
                        c += 1
            u += str(c)
        else:
            u += s[i][j]
    t.append(u)
for u in t:
    print u

Submission status: Accepted

Solution code:
#!/usr/bin/python3

h, w = list(map(int, input().split()))
s = []
s.append([0 for i in range(w + 2)])
for i in range(h):
    line = [0]
    line.extend(input().replace('.', '0'))
    line.append(0)
    s.append(line)
s.append([0 for i in range(w + 2)])

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == '#':
            for k in range(i - 1, i + 2):
                for l in range(j - 1, j + 2):
                    if k == i and l == j:
                        continue
                    if s[k][l] == '#':
                        continue
                    elif s[k][l] == '0':
                        s[k][l] = 1
                    else:
                        s[k][l] += 1

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

Submission status: Accepted

Solution code:
h,w = map(int, input().strip().split())
arr = []
arr = [list(input()) for i in range(h)]
for i in range(h):
  for j in range(w):
    if arr[i][j] == ".":
      u = max(i-1,0)
      d = min(i+2,h)
      l = max(j-1,0)
      r = min(j+2,w)
      n = 0
      for line in arr[u:d]:
          n += line[l:r].count("#")
      arr[i][j] = str(n)

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

Submission status: Accepted

Solution code:
def get_cnt(mat, i, j, h, w):
    cnt = 0
    for ii in range(i-1, i+2):
        if ii < 0 or ii > h-1:
            continue
        for jj in range(j-1, j+2):
            if jj < 0 or jj > w-1:
                continue
            if mat[ii][jj] == '#':
                cnt += 1
    return cnt


def main():
    h, w = map(int, input().split())
    mat1 = [[c for c in input()] for _ in range(h)]
    mat2 = [[None for _ in range(w)] for _ in range(h)]

    for i in range(h):
        for j in range(w):
            if mat1[i][j] == '.':
                cnt = get_cnt(mat1, i, j, h, w)
                mat2[i][j] = cnt
            else:
                mat2[i][j] = '#'

    print('\n'.join([''.join([str(c) for c in  mat2[i]]) for i in range(h)]))
    

if __name__ == "__main__":
    main()

Submission status: Accepted

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

    board = []
    for _ in range(H):
        row = list(input())
        board.append(row)

    dhs = [-1, -1, -1,  0, 0,  1, 1, 1]
    dws = [-1,  0,  1, -1, 1, -1, 0, 1]

    for i in range(H):
        for j in range(W):
            if board[i][j] == "#":
                print("#", end="")
                continue

            num_bombs = 0
            for dh, dw in zip(dhs, dws):
                new_i = i + dh
                new_j = j + dw

                if new_i < 0 or new_i >= H:
                    continue
                if new_j < 0 or new_j >= W:
                    continue

                if board[new_i][new_j] == "#":
                    num_bombs += 1
            print(num_bombs, end="")
        print()

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        ret = 0
        for dx in (-1, 0, 1):
            for dy in (-1, 0, 1):
                if dx == dy == 0:
                    continue
                ni = i + dy
                nj = j + dx
                if not (0 <= ni < H) or not (0 <= nj < W):
                    continue
                ret += 1 if S[ni][nj] == '#' else 0
        S[i][j] = str(ret)

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import itertools
H, W = map(int, input().split())
s = [ list(input()) for h in range(H) ]
for y in range(H):
    for x in range(W):
        if s[y][x] == '#':
            continue
        y1 = [ tmp for tmp in [ y-1, y, y+1 ] if tmp >= 0 and tmp < H ]
        x1 = [ tmp for tmp in [ x-1, x, x+1 ] if tmp >= 0 and tmp < W ]
        bomb = 0
        for pair in itertools.product(y1,x1):
            if pair[0] == y and pair[1] == x:
                continue
            if s[pair[0]][pair[1]] == '#':
                bomb += 1
            else:
                pass
        s[y][x] = str(bomb)
for y in range(H):
    print(''.join(s[y]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
table = [["."]*(w+2) for _ in range(h+2)]
for i in range(1,h+1):
    s = input()
    for j in range(1,w+1):
        if s[j-1] == '#':
            table[i][j] = "#"
for i in range(1,h+1):
    for j in range(1,w+1):
        if table[i][j] != "#":
            ctr = table[i-1][j-1:j+2].count("#")
            ctr += table[i][j-1:j+2].count("#")
            ctr += table[i+1][j-1:j+2].count("#")
            table[i][j] = str(ctr)
for i in range(1,h+1):
    print("".join(table[i][1:w+1]))

Submission status: Accepted

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),(0,-1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '#': continue
        c = 0
        for dx,dy in dxy:
            if x+dx < 0 or x+dx > W-1 or y+dy < 0 or y+dy > H-1: continue
            if src[y+dy][x+dx] == '#': c += 1
        ans[y][x] = c

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

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[[] for i in range(H+2)]
S[0]=list("_"*(W+2))
S[H+1]=list("_"*(W+2))
for i in range(H):
  S[i+1]=list("_"+str(input())+"_")
for i in range(1,H+1):
  for j in range(1,W+1):
    if S[i][j]!="#":
      bomb=0
      for h in [-1,0,1]:
        for w in [-1,0,1]:    
          if S[i+h][j+w]=="#": bomb+=1
      S[i][j]=bomb
        
for i in range(H+2):
  print(''.join(map(str, S[i])).replace("_",""))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import traceback


def main():
    def _check(y,x,h,w,_list,count):
        if x >= 0 and y >= 0 and x < w and y < h:
            if _list[y][x] == '#':
                count += 1

        return count


    h,w = map(int,raw_input().split())
    S = [list(raw_input()) for i in range(h)]
    ans = S

    for i,line in enumerate(S):
        for j,val in enumerate(line):
            count = 0
            if val == '.':
                count = _check(i-1,j-1,h,w,S,count)
                count = _check(i-1,j,h,w,S,count)
                count = _check(i-1,j+1,h,w,S,count)
                count = _check(i,j-1,h,w,S,count)
                count = _check(i,j,h,w,S,count)
                count = _check(i,j+1,h,w,S,count)
                count = _check(i+1,j-1,h,w,S,count)
                count = _check(i+1,j,h,w,S,count)
                count = _check(i+1,j+1,h,w,S,count)

                ans[i][j] = str(count)

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

main()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
table = []
table.append(list("_" * (W + 2)))
for _ in range(H):
    table.append(list("_" + input() + "_"))
table.append(list("_" * (W + 2)))

for h in range(1, H + 1):
    for w in range(1, W + 1):
        if table[h][w] == "#":
            continue
        count = 0
        for _h in [-1, 0, +1]:
            for _w in [-1, 0, +1]:
                if table[h + _h][w + _w] == "#":
                    count += 1
        table[h][w] = str(count)

for h in range(1, H + 1):
    print("".join(table[h]).replace("_", ""))

Submission status: Accepted

Solution code:
def calc(array, i, j, H, W):
    move = [[1, -1], [1, 0], [1, 1], [0, -1], [0, 1], [-1, 0], [-1, -1], [-1, 1]]
    num_mine = 0
    for k in range(8):
        x = i+move[k][0]
        y = j+move[k][1]
        if x >= H or x < 0 or y >= W or y < 0:
            pass
        else:
            num_mine += 1 if array[x][y] == "#" else 0
    return num_mine


def main():
    num = input().split()
    num = [int(x) for x in num]
    H = num[0]
    W = num[1]

    array = []
    for i in range(H):
        array.append(list(input()))

    for i in range(H):
        for j in range(W):
            if array[i][j] == "#":
                pass
            else:
                array[i][j] = calc(array, i, j, H, W)

    for i in range(0, num[0]):
        print("".join(map(str, array[i])))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

dxs = [0, 1, 0, -1, 1, 1, -1, -1]
dys = [1, 0, -1, 0, 1, -1, 1, -1]
output = []
for i in range(H):
    row = []
    for j in range(W):
        if table[i][j] == "#":
            row.append("#")
        else:
            cnt = 0
            for d in range(8):
                ni = i + dxs[d]
                nj = j + dys[d]
                if 0 <= ni and ni < H and 0 <= nj and nj < W:
                    if table[ni][nj] == "#":
                        cnt += 1
            row.append("{}".format(cnt))
    output.append(row)
for row in output:
    print("".join(row))

Submission status: Accepted

Solution code:
H,W=list(map(int,raw_input().split()))
a = []
for i in range(H):
    a.append(list(raw_input()))
#print a

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

cnt=0
for i in range(H):
	for j in range(W):
		if a[i][j]==".":
			cnt=0
			for k in range(8):
				#print dx[k],dy[k]
				x=i+dx[k]
				y=j+dy[k]
				if x>=0 and x<H and y>=0 and y<W and a[x][y]=="#":
					cnt+=1
			a[i][j]=cnt
			#print i,j,cnt


for i in range(H):
	print ''.join(map(str,a[i]))

Submission status: Accepted

Solution code:
import sys
import socket

hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('b1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    h, w = read_int_list()
    s = [list(read_str()) for i in range(h)]
    for i in range(h):
        for j in range(w):
            if s[i][j] == '.':
                c = 0
                for k in [-1, 0, 1]:
                    for l in [-1, 0, 1]:
                        if 0 <= i + k < h:
                            if 0 <= j + l < w:
                                if s[i + k][j + l] == '#':
                                    c += 1
                s[i][j] = str(c)
    for i in range(h):
        print(''.join(s[i]))


main()

Submission status: Accepted

Solution code:
def solution():
	h,v  = [int(x) for x in input().split(' ')]
	ls = []
	ans = []
	for i in range(h):
		ls.append(list(input()))
		ans.append([0]*v)
	
	for i in range(h):
		for j in range(v):
			if ls[i][j] == '#':
				if i>0:
					ans[i-1][j]+=1
					if j>0:
						ans[i-1][j-1]+=1
					if j<v-1:
						ans[i-1][j+1]+=1
				if i<h-1:
					ans[i+1][j]+=1
					if j>0:
						ans[i+1][j-1]+=1
					if j<v-1:
						ans[i+1][j+1]+=1
				if j>0:
					ans[i][j-1]+=1
				if j<v-1:
					ans[i][j+1]+=1

	for i in range(h):
		for j in range(v):
			if 	ls[i][j] == '#':
				print('#',end='')
			else:
				print(ans[i][j],end='')

		print("")

solution()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i] = S[i].replace(".", "0", 1)
            rec = 0
            for d in [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]:
                nx, ny = i + d[0], j + d[1]
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                    rec += 1
            if rec > 0:
                S[i] = S[i][::-1]
                S[i] = S[i].replace("0", str(rec), 1)
                S[i] = S[i][::-1]

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i] = S[i].replace(".", "0", 1)
            rec = 0
            for d in [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]:
                nx, ny = i + d[0], j + d[1]
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                    rec += 1
            if rec > 0:
                S[i]=S[i][::-1]
                S[i] = S[i].replace("0", str(rec), 1)
                S[i] = S[i][::-1]




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

Submission status: Accepted

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

H, W = map(int, input().split())
b = []
[b.append(list(input())) for i in range(H)]

c = [[0] * (W + 2) for i in range(H + 2)]
for i in range(H):
    for j in range(W):
        if b[i][j] == "#":
            c[i][j] += 1
            c[i + 1][j] += 1
            c[i + 2][j] += 1
            c[i][j + 1] += 1
            # c[i+1][j+1] += 1
            c[i + 2][j + 1] += 1
            c[i][j + 2] += 1
            c[i + 1][j + 2] += 1
            c[i + 2][j + 2] += 1
d = [""] * H
for i in range(H):
    for j in range(W):
        if b[i][j] == ".":
            d[i] += str(c[i + 1][j + 1])
        else:
            d[i] += "#"

for i in range(H):
    print(d[i])

Submission status: Accepted

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
else:
    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 = ""