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:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h() if m > 1 else typ(input()) for _ in range(n)]


def cell(c, H, W, S, i, j):
    if c == '#':
        return c

    return sum([
        int(i - 1 >= 0 and S[i - 1][j] == '#'),
        int(j + 1 <= W - 1 and S[i][j + 1] == '#'),
        int(i + 1 <= H - 1 and S[i + 1][j] == '#'),
        int(j - 1 >= 0 and S[i][j - 1] == '#'),
        int(i - 1 >= 0 and j - 1 >= 0 and S[i - 1][j - 1] == '#'),
        int(i - 1 >= 0 and j + 1 <= W - 1 and S[i - 1][j + 1] == '#'),
        int(i + 1 <= H - 1 and j + 1 <= W - 1 and S[i + 1][j + 1] == '#'),
        int(i + 1 <= H - 1 and j - 1 >= 0 and S[i + 1][j - 1] == '#'),
    ])


def main():
    H, W = read_h()
    S = read_v(H, typ=str)

    for i, row in enumerate(S):
        ans_row = ''.join([str(cell(c, H, W, S, i, j)) for j, c in enumerate(row)])
        print(ans_row)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import copy
row = list(map(int, input().split()))
s = [input().split() for i in range(row[0])]
for i in range(row[0]):
    s[i] = list(s[i][0])

s_copy = []
for i in range(row[0]+2):
    s_row = []
    for j in range(row[1]+2):
        if i == 0 or i == row[0]+1 or j == 0 or j == row[1]+1:
            s_row.append(0)
        else:
            s_row.append(s[i-1][j-1])
    s_copy.append(s_row)

count = 0
for i in range(row[0]):
    for j in range(row[1]):
        if s[i][j] == ".":
            h = i - 1
            while h <= i+1:
                w = j - 1
                while w <= j + 1:
                    if s_copy[h+1][w+1] == "#":
                        count += 1
                    w += 1
                h += 1
            s[i][j] = str(count)
            count = 0
     
for i in range(row[0]):
    a = ''.join(s[i])
    print(a)

Submission status: Accepted

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

def hb(i, j):
    b = ss[i][j] == '#'
    if j > 0:
        b += ss[i][j-1] == '#'
    if j < w - 1:
        b += ss[i][j+1] == '#'
    return b

for i in range(h):
    for j in range(w):
        if ss[i][j] == '#':
            continue

        b = 0
        if i > 0:
            b += hb(i-1, j)
        b += hb(i, j)
        if i < h - 1:
            b += hb(i+1, j)
        ss[i][j] = chr(ord('0') + b)
print('\n'.join(''.join(s) for s in ss))

Submission status: Accepted

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 = ''
      #[-1]はリストの最後という意味で出力されちゃう
      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:
        if peri[0]!=-1 and peri[1]!=-1:  
          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: Accepted

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):
        u=s[i-1][j] if i>0 else ''
        r=s[i][j+1] if j<w-1 else ''
        d=s[i+1][j] if i<h-1 else ''
        l=s[i][j-1] if j>0 else ''
        ur=s[i-1][j+1] if i>0 and j<w-1 else ''
        ul=s[i-1][j-1] if i>0 and j>0 else ''
        dr=s[i+1][j+1] if i<h-1 and j<w-1 else ''
        dl=s[i+1][j-1] if i<h-1 and j>0 else ''
        around=[u,r,d,l,ur,ul,dr,dl]
        if s[i][j]=='.':
            s[i][j]=str(around.count('#'))
for i in s:
    print(''.join(i))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC75 B

h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,1,1,1,0,-1,-1,-1]
ans = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            x = 0
            for k in range(8):
                nx = j+dx[k]
                ny = i+dy[k]
                if 0 <= nx < w and 0 <= ny < h:
                    if s[ny][nx] == '#':
                        x += 1
            ans[i][j] = str(x)
        else:
            ans[i][j] = '#'
for i in range(h):
    print(''.join(ans[i]))

Submission status: Accepted

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

zlist = []
for i in range(h+2):
    zlist.append([])
    for j in range(w+2):
        zlist[i].append(0)
#print(zlist)

shp = []
for i in range(h):
    
    for j in range(w):
        x = i + 1
        y = j + 1
        if alist[i][j] == "#":
            shp.append([i,j])
            
            zlist[x-1][y-1] += 1#１つ前の行x3
            zlist[x-1][y  ] += 1
            zlist[x-1][y+1] += 1
            
            zlist[x][y-1]   += 1#現在の行x2
            zlist[x][y+1]   += 1
            
            zlist[x+1][y-1] += 1#１つ後の行x3
            zlist[x+1][y  ] += 1
            zlist[x+1][y+1] += 1

for ele in shp:
    x = ele[0]
    y = ele[1]
    zlist[x+1][y+1] = "#"

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

Submission status: Accepted

Solution code:
dx=[-1,0,1,1,1,0,-1,-1]
dy=[1,1,1,0,-1,-1,-1,0]
h=0
w=0

def Check(px,py,i):
    px+=dx[i]
    py+=dy[i]
    return 0<=px<w and 0<=py<h

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]!='#':
            ans=0
            for k in range(8):
                if Check(j,i,k):
                    if s[i+dy[k]][j+dx[k]]=='#':
                        ans+=1
            print(ans,end='')
        else:
            print('#',end='')
    print()

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
grid = [[0 for w in range(W)] for h in range(H)]
sharps = []
for h in range(H):
  row = [v for v in input()]
  #print(row)
  for w,r in enumerate(row):
    #print(w,r)
    if r == "#":
      #grid[h][w] = "#"
      sharps.append([h,w])
      if h>=1:
        grid[h-1][w] += 1
        if w>=1:
          grid[h-1][w-1] += 1
        if w<=W-2:
          grid[h-1][w+1] += 1
      if h<=H-2:
        grid[h+1][w] += 1
        if w>=1:
          grid[h+1][w-1] += 1
        if w<=W-2:
          grid[h+1][w+1] += 1
      if w>=1:
        grid[h][w-1] +=1
      if w<=W-2:
        grid[h][w+1] +=1
#print(sharps)
for sharp in sharps:
  grid[sharp[0]][sharp[1]] = "#"
for g in grid:
  print("".join([str(v) for v in g]))

Submission status: Accepted

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, 0, 1, -1, 1, -1, 0, 1]
for i in range(h):
    for j in range(w):
        mine = 0
        for k in range(8):
            if 0 <= i+dy[k] < h and 0 <= j+dx[k] < w and s[i+dy[k]][j+dx[k]] == '#':
                mine += 1
        if s[i][j] == '.':
            s[i] = s[i].replace(".", str(mine), 1)
    print(s[i])

Submission status: Accepted

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], [-1, -1], [1, -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: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            tmp = 0
            for k in range(max(0, i-1), min(i+2, h)):
                tmp += s[k][(max(0, j-1)) : min(j+2, w)].count('#')
            s[i][j] = str(tmp)

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

Submission status: Accepted

Solution code:

H,W = map(int,input().split())

ze = ["."]*(W+2)

lis = [ze]


for i in range(H):

    s = list(input())
    s = ["."] + s + ["."]

    lis.append(s)

lis.append(ze)

#print (lis)

for h in range(H):

    h += 1

    for w in range(W):

        w += 1

        #print(h,w)
        
        if lis[h][w] == "#":
            

            for x in range(3):

                x -= 1
                
                for y in range(3):

                    y -= 1
                    
                    if lis[h+x][w+y] == ".":
                        lis[h+x][w+y] = 1

                    elif lis[h+x][w+y] != "#":
                        lis[h+x][w+y] += 1

#print (lis)
for i in range(len(lis)):

    if i != 0 and i != H+1:

        ans = ""
        for j in range(len(lis[i])):

            if j != 0 and j != W+1:

                if lis[i][j] != ".":
                    ans += str(lis[i][j])
                else:
                    ans += "0"

        print (ans)

Submission status: Accepted

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

def count(x, y):
    result = 0
    for my in range(-1, 2):
        for mx in range(-1, 2):
            tx = x + mx
            ty = y + my
            if 0 <= tx < W and 0 <= ty < H:
                if board[ty][tx] == "#":
                    result += 1

    return result

output = []

for y, b in enumerate(board):
    line = ""
    for x, c in enumerate(b):
        if c == ".":
            num = count(x, y)
            line += str(num)
        else:
            line += c
    output.append(line)

for o in output:
    print(o)

Submission status: Accepted

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

def count(x, y):
    result = 0
    for my in range(-1, 2):
        for mx in range(-1, 2):
            tx = x + mx
            ty = y + my
            if 0 <= tx < W and 0 <= ty < H:
                if board[ty][tx] == "#":
                    result += 1

    return result

output = []

for y, b in enumerate(board):
    line = ""
    for x, c in enumerate(b):
        if c == ".":
            num = count(x, y)
            line += str(num)
        else:
            line += c
    output.append(line)

for o in output:
    print(o)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
grid = [list(input()) for _ in range(h)]
for y in range(h):
    for x in range(w):
        if grid[y][x] == '#':
            for dy in range(max(0,y-1),min(y+2,h)):
                for dx in range(max(0,x-1),min(x + 2,w)):
                    if grid[dy][dx] =='.' or grid[dy][dx] =='0':
                        grid[dy][dx] = '1'
                    elif grid[dy][dx] =='#':
                        continue
                    else:
                        grid[dy][dx] = str(1 + int(grid[dy][dx]))
        elif grid[y][x] == '.':
            grid[y][x] = '0'
for i in range(h):
    print(''.join(grid[i]))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
a = []
a.append("@"*(w+2))
for i in range(h):
    a.append("@"+str(input())+"@")
a.append("@"*(w+2))
for i in range(1,h+1):
    for j in range(1,w+1):
        if a[i][j]==".":
            l=[a[i-1][j-1],a[i-1][j],a[i-1][j+1],a[i][j-1],a[i][j+1],a[i+1][j-1],a[i+1][j],a[i+1][j+1]]
            tmp = l.count("#")
            a[i]=a[i][:j]+str(tmp)+a[i][j+1:]
for i in a:
    print(i.replace("@",""))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
buf = '/'*(w+2)
lst = []
lst.append(buf)
for i in range(h):
    t = input()
    lt = '/'+t+'/'
    lst.append(lt)
lst.append(buf)

for i in range(1,h+1):
    text = ''
    for j in range(1,w+1):
        if (lst[i][j]=='.'):
            n = 0
            flame = list((lst[i-1],lst[i],lst[i+1]))
            for x in range(3):
                for y in range(j-1,j+2):
                    if (flame[x][y]=='#'):
                        n += 1
            text += str(n)
        else:
            text += '#'
    print(text)

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):
    l = ''
    for j in range(w):
        if s[i][j] == '#':
            l += s[i][j]
        else:
            l += str(sum([t[max(0, j-1): min(j+2, w)].count('#')
                          for t in s[max(0, i-1): min(h, i+2)]]))
    print(l)

Submission status: Accepted

Solution code:
def checkMus(i, j, grid):
    count = 0
    if i > 0:
        if j >0 and grid[i-1][j-1] == "#":
            count+= 1
        if grid[i-1][j] == "#":
            count+= 1
        if j < len(grid[i]) -1 and grid[i-1][j+1] == "#":
            count+= 1
    if j >0 and grid[i][j-1] == "#":
        count+= 1
    if j < len(grid[i]) -1 and grid[i][j+1] == "#":
        count+= 1
    if i < len(grid) -1:
        if j >0 and grid[i+1][j-1] == "#":
            count+= 1
        if grid[i+1][j] == "#":
            count+= 1
        if j < len(grid[i]) -1 and grid[i+1][j+1] == "#":
            count+= 1
    return count

h,w = map(int, input().split())
grid = [input() for i in range(h)]
for i in range(h):
    line = ""
    for j in range(w):
        if grid[i][j] == ".":
            line += str(checkMus(i, j, grid))
        else:
            line += grid[i][j]
    print(line)

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
F = []
for _ in range(h):
    f = list(map(str,input()))
    F.append(f)
    
def bomb(field,i,j):
    cnt = 0
    for p in range(-1,2):
        for q in range(-1,2):
            if 0 <= i+p <= len(field)-1 and 0 <= j+q <= len(field[0])-1:
                if field[i+p][j+q] == "#":
                    cnt += 1
    return cnt
    cnt = 0

ans = [[0]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if F[i][j] != "#":
            ans[i][j] = str(bomb(F,i,j))
        else:
            ans[i][j] = "#"
for s in ans:
    print("".join(s))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(list(input()))
bomb = []
for i,h in enumerate(S):
    for j,r in enumerate(h):
        if r=="#":
            bomb.append((i, j))
        else:
            S[i][j]=0
for i,j in bomb:
    if 0<=i-1<=H-1 and 0<=j-1<=W-1 and S[i-1][j-1]!="#":
        S[i-1][j-1] += 1
    if 0<=i-1<=H-1 and 0<=j<=W-1 and S[i-1][j]!="#":
        S[i-1][j] += 1
    if 0<=i-1<=H-1 and 0<=j+1<=W-1 and S[i-1][j+1]!="#":
        S[i-1][j+1] += 1
    if 0<=i<=H-1 and 0<=j+1<=W-1 and S[i][j+1]!="#":
        S[i][j+1] += 1
    if 0<=i+1<=H-1 and 0<=j+1<=W-1 and S[i+1][j+1]!="#":
        S[i+1][j+1] += 1
    if 0<=i+1<=H-1 and 0<=j<=W-1 and S[i+1][j]!="#":
        S[i+1][j] += 1
    if 0<=i+1<=H-1 and 0<=j-1<=W-1 and S[i+1][j-1]!="#":
        S[i+1][j-1] += 1
    if 0<=i<=H-1 and 0<=j-1<=W-1 and S[i][j-1]!="#":
        S[i][j-1] += 1
for s in S:
    print("".join(list(map(str, s))))

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if mas[i][j] == '.':
            # 爆弾検索
            count = 0
            for k in range(i-1, i+2):
                for l in range(j-1, j+2):
                    if ((0 <= k < h and 0 <= l < w)
                        and mas[k][l] == '#'):
                        count += 1
            mas[i][j] = str(count)

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

Submission status: Accepted

Solution code:
row,column = map(int,input().split())
S = [list(input()) for i in range(row)]
for i in range(row):
  for j in range(column):
    if S[i][j] == "#":
      continue
    cnt = 0
    for dx in [-1,0,1]:
      for dy in [-1,0,1]:
        if not 0<= i+dy <row or not 0 <= j + dx < column:
          continue
        if S[i + dy][j + dx] ==  "#":
          cnt +=1
    S[i][j]  = str(cnt)
for s in S:
  print("".join(s))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, S: "List[str]"):

    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                print("#",end="")
            else:
                c = 0
                for p in range(max(i-1,0),min(i+2,H)):
                    for q in range(max(j-1,0),min(j+2,W)):
                        if (p != i or q != j) and S[p][q] == "#":
                            c += 1
                print(c,end="")
        print("")
                        
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    S = [ next(tokens) for _ in range(H) ]  # type: "List[str]"
    solve(H, W, S)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
hw = input().split(" ");
h = int(hw[0]);
w = int(hw[1]);
f = [[0]*(w+2) for i in range(h+2)];
ans = [[0]*(w) for i in range(h)];
for i in range(1,h+1) :
	s = input()
	for j in range(1,w+1) :
		ans[i-1][j-1] = s[j-1];
		if s[j-1] == '.' :
			f[i][j] = 0;
		elif s[j-1] == '#':
			f[i][j] = 1;
for i in range(1,h+1) :
	for j in range(1,w+1) :
		if f[i][j] == 0 :
			countbom = 0;
			for k in range(i-1,i+2) :
				for l in range(j-1,j+2) :
					countbom += f[k][l];
			ans[i-1][j-1] = countbom;
for i in range(0,h) :
	out = '';
	for j in range(0,w) :
		out += str(ans[i][j]);
	print(out);

Submission status: Accepted

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

for i in S:
    s_new = "." + i + "."
    S_new.append(s_new)

for i in range(1,H+1):
    for j in range(1,W+1):
        if S_new[i][j] == ".":
            count = 0
            for k in range(i-1,i+2):
                for r in range(j-1,j+2):
                    if S_new[k][r] == "#":
                        count += 1
            result.append(count)
        else:
            result.append("#")

flag = 0
for i in result:
    print(i,end="")
    flag += 1
    if flag == W:
        print("")
        flag = 0

Submission status: Accepted

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

bom=[]

for i in range(H):
    bom.append(list(input()))

#print(bom)

for h in range(H):
    for w in range(W):
        if bom[h][w]=="#":
            continue
        cnt=0
        for x in range(-1,2):
            for y in range(-1,2):
                if h+x>=0 and w+y>=0 and h+x<H and w+y<W:
                    if bom[h+x][w+y]=="#":
                        cnt+=1
        bom[h][w]=str(cnt)
for i in bom:
    print("".join(i))

Submission status: Accepted

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] == '#' and x != -1 and y != -1:
            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: Accepted

Solution code:
# run on python 3.4.3

from sys import stdin
from os import linesep


def main():
    h, w = map(int, stdin.readline().split())
    s = ['' for i in range(h)]
    for i in range(h):
        s[i] = list(stdin.readline())
    dy = [0, 0, 1, 1, 1, -1, -1, -1]
    dx = [-1, 1, -1, 0, 1, -1, 0, 1]
    for y in range(h):
        for x in range(w):
            if s[y][x] != '.':
                continue
            bombs = 0
            for d in range(8):
                py, px = y+dy[d], x+dx[d]
                if 0 > py or py >= h or 0 > px or px >= w:
                    continue
                bombs += 1 if s[py][px] == '#' else 0
            s[y][x] = str(bombs)
    print(linesep.join(map(''.join, s)))


main()

Submission status: Accepted

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

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

for j, ss in enumerate(S):
    ans = ''
    for i in range(W):
        if ss[i] == '#':
            ans += '#'
        else:
            cnt = 0
            for n in dx:
                for m in dy:
                    if n == 0 and m == 0:
                        continue
                    if i+n >= 0 and i+n < W and j+m >= 0 and j+m < H:
                        if S[j+m][i+n] == '#':
                            cnt += 1
            ans += str(cnt)
    print(ans)

Submission status: Accepted

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

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

for h in  range(H):
    for w in range(W):
        if bom[h][w]=="#":
            continue

        cnt=0
        for x in range(-1,2):
            for y in range(-1,2):
                if h+x>=0 and w+y>=0 and h+x<H and w+y<W:
                    if bom[h+x][w+y]=="#":
                        cnt+=1
        bom[h][w]=str(cnt)
for i in bom:
    print("".join(i))

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if bom[h][w] == "#":
            continue

        cnt = 0
        for x in range(-1, 2):
            for y in range(-1, 2):
                if 0 <= h+x < H and 0 <= w+y < W:
                    if bom[h+x][w+y] == "#":
                        cnt += 1
        bom[h][w] = str(cnt)
for i in bom:
    print("".join(i))

Submission status: Accepted

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] == '#' and x != -1 and y != -1:   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: Accepted

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

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

for i in range(H):
    r = ''
    for j in range(W):
        if sq[i][j] == '#':
            r += '#'
            continue
        cnt = 0
        for y in range(i-1, i+2):
            if y < 0 or y > H-1:
                continue
            for x in range(j-1, j+2):
                if x < 0 or x > W-1:
                    continue
                if sq[y][x] == '#':
                    cnt += 1
        r += str(cnt)
    print(r)

Submission status: Accepted

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

bom=[]
for i in range(H):
    bom.append(list(input()))
#print(bom)

for h in range(H):
    for w in range(W):
        if bom[h][w]=="#":
            #continue ループ内の内容をスキップ
            continue

        cnt=0
        for x in range(-1,2):
            for y in range(-1,2):
                if h+x>=0 and w+y>=0 and h+x<H and w+y<W:
                    if bom[h+x][w+y]=="#":
                        cnt+=1
        bom[h][w]=str(cnt)

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
graph = []
for i in range(h):
    row = []
    for j in input():
        row.append(j)
    graph.append(row)

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

for row in range(h):
    for col in range(w):
        if graph[row][col] == '#':
            print("#", end='')
            continue

        # 爆弾カウント
        bomb = 0
        for i in range(8):
            row_sur = row + dx[i]
            col_sur = col + dy[i]
            if row_sur < 0 or h <= row_sur or col_sur < 0 or w <= col_sur:
                continue
            if graph[row_sur][col_sur] == "#":
                bomb += 1
        print(bomb, end='')
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
matrix1 = []
matrix2 = []
for i in range(H):
    matrix1.append(list(input()))
for i in range(H):
    matrix3 = []
    for j in range(W):
        bomb = 0
        if i - 1 >= 0 and j - 1 >= 0 and matrix1[i-1][j-1] == "#":
            bomb += 1
        if i - 1 >= 0 and matrix1[i-1][j] == "#":
            bomb += 1
        if i - 1 >= 0 and j + 1 <= W-1 and matrix1[i-1][j+1] == "#":
            bomb += 1
        if j - 1 >= 0 and matrix1[i][j-1] == "#":
            bomb += 1
        if j + 1 <= W-1 and matrix1[i][j+1] == "#":
            bomb += 1
        if i + 1 <= H-1 and j - 1 >= 0 and matrix1[i+1][j-1] == "#":
            bomb += 1
        if i + 1 <= H-1 and matrix1[i+1][j] == "#":
            bomb += 1
        if i + 1 <= H-1 and j + 1 <= W-1 and matrix1[i+1][j+1] == "#":
            bomb += 1
        if matrix1[i][j] == "#":
            matrix3.append("#")
        else:
            matrix3.append(str(bomb))
    print(''.join(matrix3))

Submission status: Accepted

Solution code:
H, W = [int(i) for i in input().split()]
S = ['.' * (W + 2)]
for i in range(H):
  S.append('.' + input() + '.')
S.append('.' * (W + 2))

mine = [[0 for _ in range(W)] for _ in range(H)]
for j in range(H):
  for i in range(W):
    if S[j + 1][i + 1] == '#':
      mine[j][i] = '#'
    else:
      mine[j][i] = sum([s[i:i + 3].count('#') for s in S[j:j + 3]])
      
for m in mine:
  print(''.join([str(i) for i in m]))

Submission status: Accepted

Solution code:
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

H, W = nl()

mas = []
for i in range(H):
    mas.append(s())


res = [[0] * (W+2) for i in range(H+2)]
for i in range(H):
    for j in range(W):
        if mas[i][j] == '#':
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if k == 0 and l == 0:
                        continue
                    res[i+1+k][j+1+l] += 1

for i in range(H):
    row = ''
    for j in range(W):
        if mas[i][j] == '#':
            row += '#'
        else:
            row += str(res[i+1][j+1])
    print(row)

Submission status: Accepted

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

ans = []
dl = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
for i, row in enumerate(S):
    row_list = []
    for j, grid in enumerate(row):
        if grid == '#':
            print('#', end='')
            continue

        cnt = 0
        for dx, dy in dl:
            if i + dx < 0 or H - 1 < i + dx or j + dy < 0 or W - 1 < j + dy:
                continue
            elif S[i+dx][j+dy] == '#':
                cnt += 1
        print(cnt, end='')
    print('\n', end='')

Submission status: Accepted

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

a = [ [ 0 for i in range(W)] for j in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            a[i][j] = "#"
            for p in [-1,0,1]:
                for q in [-1,0,1]:
                    if i+p >= 0 and j+q >= 0 and i+p < H and j+q < W and S[i+p][j+q] != "#":
                        a[i+p][j+q] += 1


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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
grid = [input() for _ in range(H)]
ans = []
for i, g in enumerate(grid):
    r = ''
    for j, s in enumerate(g):
        if s == '#':
            r += s
        else:
            r += str(sum([temp[max(j-1, 0): min(j+1, W)+1].count('#') for temp in grid[max(i-1, 0): min(i+1, H)+1]]))
    ans.append(r)
print(*ans, sep='\n')

Submission status: Accepted

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

ans = [[0 for _ in range(W)] for _ in range(H)]

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