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:
h,w = map(int,input().split())
s = [input() for i in range(h)]

if h == 1 and w == 1:
    if s[0]=='#':
        print('#')
    else:
        print('0')

elif h == 1:
    ans = ''
    for i in range(w):
        if s[0][i]=='#':
            ans += '#'
        elif i != 0 and i != w-1:
            ans += str(s[0][i-1:i+2].count('#'))
        elif i == 0:
            ans += str(s[0][1].count('#'))
        elif i == w-1:
            ans += str(s[0][i-1].count('#'))
    print(ans)

elif w == 1:
    for i in range(h):
        if s[i]=='#':
            print('#')
        elif i != 0 and i != h-1:
            print(str(s[i-1:i+2].count('#')))
        elif i == 0:
            print(str(s[1].count('#')))
        elif i == h-1:
            print(str(s[i-1].count('#')))

else:
    for i in range(h):
        ans = ''
        for j in range(w):
            if s[i][j] == '#':
                ans += '#'
            else:
                if i != 0 and j!= 0 and i != h-1 and j!= w-1:
                    up = s[i-1][j-1:j+2].count('#')
                    parallel = s[i][j-1:j+2].count('#')
                    down = s[i+1][j-1:j+2].count('#')
                    ans += str(up+parallel+down)
                elif i==0 and j==0:
                    parallel = s[0][1].count('#')
                    down = s[1][0:2].count('#')
                    ans += str(parallel+down)
                elif i==0 and j==w-1:
                    parallel = s[0][w-2:].count('#')
                    down = s[1][w-2:].count('#')
                    ans += str(parallel+down)
                elif i==h-1 and j==0:
                    up = s[i-1][:2].count('#')
                    parallel = s[i][:2].count('#')
                    ans += str(up+parallel)
                elif i==h-1 and j==w-1:
                    up = s[i-1][w-2:].count('#')
                    parallel = s[i][w-2:].count('#')
                    ans += str(up+parallel)
                elif i==0:
                    parallel = s[0][j-1:j+2].count('#')
                    down = s[1][j-1:j+2].count('#')
                    ans += str(parallel+down)
                elif j==0:
                    up = s[i-1][:2].count('#')
                    parallel = s[i][:2].count('#')
                    down = s[i+1][:2].count('#')
                    ans += str(up+parallel+down)
                elif i==h-1:
                    up = s[i-1][j-1:j+2].count('#')
                    parallel = s[i][j-1:j+2].count('#')
                    ans += str(up+parallel)
                else:
                    up = s[i-1][w-2:].count('#')
                    parallel = s[i][w-2:].count('#')
                    down = s[i+1][w-2:].count('#')
                    ans += str(up+parallel+down)
        print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)

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

grid = [["#"]*(W+2) for _ in range(H+2)]

for i in range(1, H+1):
    s = input()
    for j, c in enumerate(s):
        grid[i][j+1] = c

cntGrid = [[0]*(W+2) for _ in range(H+2)]
for i in range(1, H+1):
    for j in range(1, W+1):
        if grid[i][j] == "#":
            cntGrid[i][j] = "#"
            d = [-1, 0, 1]
            for x in d:
                for y in d:
                    if x == 0 and y == 0:
                        continue
                    else:
                        if not grid[i+x][j+y] == "#":
                            cntGrid[i+x][j+y] += 1
for l in cntGrid[1:-1]:
    print("".join(map(str, l[1:-1])))

Submission status: Accepted

Solution code:
def b_minesweeper(H, W, S):
    # 書き換え後のフィールド
    field = [[''] * W for _ in range(H)]
    # あるマス目の右、下、左、上、右下、左下、左上、右上
    neighbouring_square = ((0, 1), (1, 0), (0, -1), (-1, 0), (1, 1), (1, -1), (-1, -1), (-1, 1))

    def is_in_field(row, col):  # 調べるマスがフィールド内か？
        return (0 <= row < H) and (0 <= col < W)

    for row in range(H):
        for col in range(W):
            if S[row][col] == '#':
                field[row][col] = '#'
                continue

            num = 0  # 周囲8マスの#の数
            for dr, dc in neighbouring_square:
                nr, nc = row + dr, col + dc
                if is_in_field(nr, nc) and S[nr][nc] == '#':
                    num += 1
            field[row][col] = str(num)
    return '\n'.join(''.join(row) for row in field)


H, W = [int(i) for i in input().split()]
S = [input() for _ in range(H)]
print(b_minesweeper(H, W, S))

Submission status: Accepted

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

for i in range(H):
  for j in range(W):
    if(S[i][j] == "#"):
      reach[i][j] = "#"
      if(reach[i-1][j-1] != "#"):
        reach[i-1][j-1] += 1
      if(reach[i-1][j] != "#"):
        reach[i-1][j] += 1
      if(reach[i-1][j+1] != "#"):
        reach[i-1][j+1] += 1
      if(reach[i][j-1] != "#"):
        reach[i][j-1] += 1
      if(reach[i][j+1] != "#"):
        reach[i][j+1] += 1
      if(reach[i+1][j-1] != "#"):
        reach[i+1][j-1] += 1
      if(reach[i+1][j] != "#"):
        reach[i+1][j] += 1
      if(reach[i+1][j+1] != "#"):
        reach[i+1][j+1] += 1
        
for r in reach[:-1]:
  print("".join(list(map(str, r[:W]))))

Submission status: Accepted

Solution code:

h, w = map(int,input().split())
A = [list(input()) for i in range(h)]

B = [[0 for i in range(w+2)] for i in range(h+2)]

for i in range(h) :
    for j in range(w) :
        p = i+1
        q = j+1
        if A[i][j] == "#" :
            B[p][q] = 10
            for r in range(-1,2) :
                for s in range(-1,2) :
                    B[p+r][q+s] += 1

for i in range(h+2):
    for j in range(w+2):
        if B[i][j] > 8 :
            B[i][j] = "#"

for i in range(1,h+1) :
    ans = ""
    for j in range(1,w+1) :
        ans += str(B[i][j])
    print(ans)

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
m = []
dx = [0, 1, 1, 1, 0, -1, -1, -1]
dy = [1, 1, 0, -1, -1, -1, 0, 1]
for i in range(h):
    m.append(list(input()))
for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            continue
        num = 0
        for k in range(8):
            x = j + dx[k]
            y = i + dy[k]
            if x < 0 or x >= w or y < 0 or y >= h:
                continue
            if m[y][x] == '#':
                num += 1
        m[i][j] = str(num)
for i in range(h):
    print(''.join(m[i]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
f = [input() for _ in range(h)]
for i in range(h):
  for j in range(w):
    if f[i][j] == '#':
      print('#', end = "")
      continue
    cnt = 0
    for k in range(i-1, i+2):
      if k < 0 or h <= k:
        continue
      for l in range(j-1, j+2):
        if l < 0 or w <= l:
          continue
        if f[k][l] == '#':
          cnt += 1
    print(cnt, end = "")
  print()

Submission status: Accepted

Solution code:
H,W=[int(x) for x in input().split()]

bom_list=[]
serch_list=[-1,0,1]

for h in range(H):
  str_list=[x for x in input()]
  for w in range(W):
    if str_list[w]=='#':
      bom_list.append([h,w])
      
for h in range(H):
  out_list=[]
  for w in range(W):
    bom_num=0
    
    if [h,w] in bom_list:
      out_list.append('#')
    else:
      for x in [-1,0,1]:
        for y in [-1,0,1]:
          if [h+x,w+y] in bom_list:
            bom_num += 1
      out_list.append(bom_num)
  
  out_str=''
  for s in out_list:
    out_str+=str(s)
    
  print(out_str)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
masu=[]
up=1
down=1
left=1
right=1
for i in range(h):
    masu.append([i for i in input()])
for i in range(h):
    for j in range(w):
        bakudan=0
        if masu[i][j]==".":
            if i==0:up=0
            if i==h-1:down=0
            if j==0:left=0
            if j==w-1:right=0
            if up:
                if masu[i-1][j]=="#":bakudan+=1
            if up and left:
                if masu[i-1][j-1]=="#":bakudan+=1
            if up and right:
                if masu[i-1][j+1]=="#":bakudan+=1
            if left:
                if masu[i][j-1]=="#":bakudan+=1
            if left and down:
                if masu[i+1][j-1]=="#":bakudan+=1
            if down:
                if masu[i+1][j]=="#":bakudan+=1
            if down and right:
                if masu[i+1][j+1]=="#":bakudan+=1
            if right:
                if masu[i][j+1]=="#":bakudan+=1
            masu[i][j]=str(bakudan)
        up=1
        down=1
        left=1
        right=1

for i in masu:
    s=''.join(i)
    print(s)

Submission status: Accepted

Solution code:
h,w = (int(i) for i in input().split())
d = {}
nei = [-101,-100,-99,-1,1,99,100,101]
for i in range(h):
  s = input()
  for j in range(w):
    d[(i+1)*100+j+1] = s[j]
for k in d:
  if d[k] == '.':
    cnt = 0
    for l in nei:
      if k+l in d:
        if d[k+l] == '#':
          cnt += 1
    d[k] = cnt
for i in range(h):
  ans = ''
  for j in range(w):
    ans += str(d[(i+1)*100+j+1])
  print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
lst = [input() for _ in range(n)]

def check(lst, i, j, n, m):
    if n == 1:
        row_lst = [0]
    elif i == 0:
        row_lst = [i, i+1]
    elif i == n-1:
        row_lst = [i-1, i]
    else:
        row_lst = [i-1, i, i+1]
    if m == 1:
        col_lst = [0]
    elif j == 0:
        col_lst = [j, j+1]
    elif j == m-1:
        col_lst = [j-1, j]
    else:
        col_lst = [j-1, j, j+1]
    count = 0
    for a in row_lst:
        for b in col_lst:
            if lst[a][b] == '#':
                count += 1
    return count


for i in range(n):
    ans = ''
    for j in range(m):
        if lst[i][j] == '#':
            ans += '#'
        else:
            ans += str(check(lst, i, j, n, m))
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
sc = np.array([[0]*(W+2)] + [[0] + i + [0] for i in s] + [[0]*(W+2)])
for i in range(1, H+1):
    for p in range(1, W+1):
        if sc[i][p] == '.':
            sums = 0
            for e in sc[i-1:i+2,p-1:p+2]:
                sums += list(e).count('#')
            sc[i][p] = str(sums)
for i in sc[1:H+1,1:W+1]:
    print(''.join(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 p in range(W):
        if s[i][p] == '.':
            s[i][p] = 0
for i in range(H):
    for p in range(W):
        if s[i][p] != '#':
            if (i != 0) and (p != 0) and (s[i-1][p-1] == '#'):
                s[i][p] += 1
            if (i != 0) and (s[i-1][p] == '#'):
                s[i][p] += 1
            if (p != 0) and (s[i][p-1] == '#'):
                s[i][p] += 1
            if (i != (H - 1)) and (p != (W - 1)) and (s[i+1][p+1] == '#'):
                s[i][p] += 1
            if (i != (H-1)) and (s[i+1][p] == '#'):
                s[i][p] += 1
            if (p != (W - 1)) and (s[i][p+1] == '#'):
                s[i][p] += 1
            if (i != 0) and (p != (W-1)) and (s[i-1][p+1] == '#'):
                s[i][p] += 1
            if (i != (H-1)) and (p != 0) and (s[i+1][p-1] == '#'):
                s[i][p] += 1            
for i in s:
    print(''.join(list(map(str, i))))

Submission status: Accepted

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

S = []
S.append(list('@'*(W+2)))
for i in range(H):
  a = list('@'+input()+'@')
  S.append(a)
S.append(list('@'*(W+2)))

ans = []
for i in range(H+2):
  ans.append([0]*(W+2))

for i in range(H+2):
 if((i != 0)and(i != H+1)): 
  for j in range(W+2):
    if((j != 0)and(j != W+1)):      
      if(S[i+1][j-1] == '#'):
        ans[i][j] += 1   
      if(S[i+1][j] == '#'):
        ans[i][j] += 1
      if(S[i+1][j+1] == '#'):
        ans[i][j] += 1
      if(S[i][j-1] == '#'):
        ans[i][j] += 1

      if(S[i][j+1] == '#'):
        ans[i][j] += 1
      if(S[i-1][j-1] == '#'):
        ans[i][j] += 1   
      if(S[i-1][j] == '#'):
        ans[i][j] += 1      
      if(S[i-1][j+1] == '#'):
        ans[i][j] += 1
      if(S[i][j] == '#'):#自分自身が爆弾なら
        ans[i][j] = '#'
      ans[i][j] = str(ans[i][j])#出力用文字列変換
      
      
for i in range(H+2):
 if((i != 0)and(i != H+1)):
  ans[i].pop(0)
  ans[i].pop(-1)
  print(''.join(ans[i]))

Submission status: Accepted

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

def main():
	y, x = map(int, input().split(" "))
	
	lines = []
	for ii in range(y):
		line = input()
		lines.append(list(line))
	
	for i, val in enumerate(lines):
		for j, vol in enumerate(val):
			if vol == ".":
				counts = 0
				if i != 0:
					if lines[i - 1][j] == "#":
						counts = counts + 1
				if (y - 1) > i:
					if lines[i + 1][j] == "#":
						counts = counts + 1
				if j != 0:
					if lines[i][j - 1] == "#":
						counts = counts + 1
				if (x - 1) > j:
					if lines[i][j + 1] == "#":
						counts = counts + 1
				if i != 0 and j != 0:
					if lines[i - 1][j - 1] == "#":
						counts = counts + 1
				if (y - 1) > i and (x - 1) > j:
					if lines[i + 1][j + 1] == "#":
						counts = counts + 1
				if i != 0 and (x - 1) > j:
					if lines[i - 1][j + 1] == "#":
						counts = counts + 1
				if j != 0 and (y - 1) > i:
					if lines[i + 1][j - 1] == "#":
						counts = counts + 1
				print("{}".format(counts), end = "")
			else:
				print("#", end = "")
		print("")
	
if __name__ == "__main__":
	main()

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):
        if S[i][j] == '.':
            count = 0
            if i!=0:
                if j!=0 and S[i-1][j-1]=='#':
                    count+=1
                if S[i-1][j]=='#':
                    count+=1
                if j!=W-1 and S[i-1][j+1]=='#':
                    count+=1
            if j!=0 and S[i][j-1]=='#':
                count+=1
            if j!=W-1 and S[i][j+1]=='#':
                count+=1
            if i!=H-1:
                if j!=0 and S[i+1][j-1]=='#':
                    count+=1
                if S[i+1][j]=='#':
                    count+=1
                if j!=W-1 and S[i+1][j+1]=='#':
                    count+=1
            print(count, end='')
        else:
            print(S[i][j], end='')
    print()

Submission status: Accepted

Solution code:
# coding: utf-8
H, W = map(int, input().split())
grid = [['*'] * (W + 2)]
for _ in range(H):
    grid.append(['*'] + list(input()) + ['*'])
grid.append(['*'] * (W + 2))

def check(i, j):
    re = 0
    for p in range(3):
        for q in range(3):
            if grid[i - 1 + p][j - 1 + q] == '#':
                re += 1
    return re

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if grid[i][j] == '.':
            grid[i][j] = check(i, j)

for i in range(1, H + 1):
    for j in range(1, W + 1):
        print(grid[i][j], end='')
    print()

Submission status: Accepted

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

ans = []
for i in range(H):
    s = ''
    for j in range(W):
        if S[i][j] == '.':
            count = 0
            for x in [-1,0,1]:
                for y in [-1,0,1]:
                    if 0 <= i+x < H and 0 <= j+y < W:
                        count += 1 if S[i+x][j+y] == '#' else 0
            s += str(count)
        else:
            s += '#'
    ans.append(s)

print(*ans,sep='\n')

Submission status: Accepted

Solution code:
h,w=[int(i) for i in input().split()]
s=[]
for i in range(h):
    s.append([j for j in input()])
for i in range(h):
    text=""
    for j in range(w):
        if s[i][j]!="#":
            cnt=0
            try:
                if s[i-1 if i-1>=0 else 100][j-1 if j-1>=0 else 100]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i-1 if i-1>=0 else 100][j]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i-1 if i-1>=0 else 100][j+1]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i][j-1 if j-1>=0 else 100]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i][j+1]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j-1 if j-1>=0 else 100]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j]=="#":
                    cnt+=1
            except:
                pass
            try:
                if s[i+1][j+1]=="#":
                    cnt+=1
            except:
                pass
            text+=str(cnt)
        else:
            text+="#"
    print(text)

Submission status: Accepted

Solution code:
H, W = map(int,input().split())
S = [list(input()) for i in range(H)] #リストを二重にする
#print(S)
for i in range(H): #すべてのマスを探索する
  for j in range(W):
    if S[i][j] == ".": #行列の中の点(なんもない地帯)を0に変換する
      S[i][j] = 0
#print(S)
for i in range(H): #すべてのマスを探索する
  for j in range(W):
    if S[i][j] == "#": #爆弾マスに当たった場合以下の処理が起きてそれ以外は何もおきない
       for k in range(-1, 2): #爆弾マスの横軸の行3マス分
          for l in range(-1, 2): #爆弾マスの縦軸の列3マス分
            ni = i+k 
            nj = j+l #爆弾マスの詳しい座標情報
            if (0 <= ni < H) and (0 <= nj < W) and \
               (S[ni][nj] != '#') and (S[ni][nj] != '.'): 
                #在りもしないマスで無いかの条件式
                #そのマスが数字であるかの条件式
              S[ni][nj] += 1
for i in range(H): #リストの文字列を連結・結合
    print(''.join(map(str, S[i]))) #二重リストであることに注意、改行すると余計ややこしくなる

Submission status: Accepted

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

for i in range(H):
  for j in range(W):
    if S[i][j]=='.':
      num=0
      for (x,y) in zip(dx,dy):
        if 0<=i+x<H and 0<=j+y<W and S[i+x][j+y]=='#':
          num+=1
      S[i][j]=str(num)

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

Submission status: Accepted

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

def cange(x):
  if x == '.':
    return 0
  else:
    return 1

zerofill = [ '.' for _ in range(W+2)] 

L.append(zerofill)
for _ in range(H):
  L.append(['.']+list(input())+['.'])
L.append(zerofill)

for i in range(1,H+1):
  for j in range(1,1+W):
    if L[i][j] == '.':
      cnt = 0
      for x in [-1,0,1]:
        for y in [-1,0,1]:
          if not (x == 0 and y == 0):
            cnt += cange(L[i+x][j+y])
      print(cnt,end='')
    else:
      print('#',end='')
  print()

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):
        if s[i][j]=='.':
            c = 0
            for iw in range(i-1,i+2):
                for jw in range(j-1,j+2):
                    if iw<0 or h<=iw or jw<0 or w<=jw:
                        continue
                    elif s[iw][jw]=="#":
                        c+=1
            s[i][j]=str(c)
for s_ in s:
    print(''.join(s_))

Submission status: Accepted

Solution code:
def main():
    H,W = map(int, input().split())
    S = [input() for i in range(H)]
    
    #S[1][1] = 1
    #print(S)
    
    #exit()
    #S.insert(0,"." * (W))
    #S.append("." * (W))
    
    #print(S)
    #exit()
    
    i = 0
    T = S
    
    #H=y=i
    for i in range(H):
        Ten = ''
        #W=x=j
        for j in range(W):
            if S[i][j] == "#":
                Ten += '#'
                #l = 1
            else:
                count = 0
                #y増分
                for k in range(3):
                    #x増分
                    for l in range(3):
                        if 0 <= i + (k - 1) < H and 0 <= j + (l-1) < W and S[i+(k-1)][j+(l-1)] == '#':
                            count = count + 1
                Ten += str(count)
    #S.insert(0,"." * W)
    #S.append("." * W)
        print(Ten)
        #print("A"*100)
main()

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):
        if s[i][j]=='.':
            c = 0
            for iw in range(i-1,i+2):
                for jw in range(j-1,j+2):
                    if iw<0 or h<=iw or jw<0 or w<=jw:
                        continue
                    elif s[iw][jw]=="#":
                        c+=1
            s[i][j]=str(c)
for s_ in s:
    print(''.join(s_))

Submission status: Accepted

Solution code:
H,W = map(int,input().split())
L = []
ANS = [['#']*W for _ in range(H)]
for i in range(H):
  L.append(list(input()))
dx = [0,0,1,1,1,-1,-1,-1]
dy = [1,-1,1,-1,0,1,-1,0]
for i in range(H):
  for j in range(W):
    cnt = 0
    if L[i][j] == '#':
        continue
    else:
      for k in range(8):
        if i+dy[k] < 0 or i+dy[k] >= H or j+dx[k] < 0 or j+dx[k] >= W:
          None
        else:
          if L[i+dy[k]][j+dx[k]] == '#':
            cnt += 1
      ANS[i][j] = cnt
for i in range(H):
  for j in range(W):
    print(ANS[i][j],end = '')
  print()

Submission status: Accepted

Solution code:
H, W = [int(_) for _ in input().split()]
cbd = [list(input()) for _ in range(H)]
for i in range(H): 
    for j in range(W):
        if cbd[i][j] == ".":
            c = 0
            for iw in range(i-1,i+2):
                for jw in range(j-1,j+2):
                    if iw < 0 or H <= iw or jw < 0 or W <= jw:
                        continue
                    elif cbd[iw][jw] == "#":
                        c += 1
            cbd[i][j] = str(c)
for i in range(H):
    print("".join(cbd[i]))

Submission status: Accepted

Solution code:

def look(S, i,j):
    if S[i][j] is '#': return '#'
    h = len(S)
    w = len(S[0])
    cnt=0
    for ii in range(-1, 2):
        for jj in range(-1, 2):
            if ii+i<0 or ii+i >= h or jj+j<0 or jj+j >= w or (ii==0 and jj==0): continue
            t = S[i+ii][j+jj]
            if t is '#': cnt += 1
    return cnt


h,w=map(int,input().split())
mat = [input() for _ in range(h)]

for i in range(h):
    for j in range(w):
        print(look(mat, i, j), end='')
    print('')

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] == ".":
            sum = 0
            for a in range(max(i-1, 0), min(i+2, h)):
                for b in range(max(j-1, 0), min(j+2, w)):
                    if s[a][b] == "#":
                        sum += 1
            print(sum, end="")
        else:
            print("#", end="")
    print()

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
ans = []
line = [0 for i in range(w)]
ad = ["." for i in range(w+2)]

for i in range(h):
    s[i].insert(0,".")
    s[i].append(".")
s.insert(0,ad)
s.append(ad)



for i in range(1,h+1):
    for j in range(1,w+1):
        cnt =[]
        if s[i][j] =="#":
            line[j-1] = "#"
        else:
            for k in range(3):
                for l in range(3):
                    cnt.append(s[i-1+k][j-1+l])
            line[j-1] = str(cnt.count("#"))
        del cnt
    ans.append("".join(line))
for i in range(h):
    print(ans[i])

Submission status: Accepted

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


'''generating a grid expanded by '&'. For example,

            &&&&&
#.#         &#.#&
###    ->   &###&
##.         &##.& 
            &&&&&

'''

expanded_grid = [list('&' * (W + 2))]
for row in rows:
    expanded_grid.append(list('&' + row + '&'))
expanded_grid.append(list('&' * (W + 2)))

for i in range(H):
    for j in range(W):
        if (grid[i])[j] == '.':
            EightPiece = [
                (expanded_grid[i])[j],
                (expanded_grid[i])[j+1],
                (expanded_grid[i])[j + 2],

                (expanded_grid[i+1])[j],
                (expanded_grid[i+1])[j+2],

                (expanded_grid[i+2])[j],
                (expanded_grid[i+2])[j+1],
                (expanded_grid[i+2])[j+2],
            ]

            (grid[i])[j] = str(EightPiece.count('#'))

for row_in_the_form_of_list in grid:
    print(''.join(row_in_the_form_of_list))

Submission status: Accepted

Solution code:
a = input().split()
H = int(a[0])
W = int(a[1])

ans = []
for i in range(H+2):
 ans.append([])
 for j in range(W+2):
   ans[i].append(0)

S = []
S.append([])
for j in range(W+2):
   S[0].append('.')
for i in range(H):
  s = input()
  s = list(s)
  s.insert(0,'.')
  s.append('.')
  S.append(s)
S.append([])
for j in range(W+2):
   S[-1].append('.')

for i in range(H+2):
  if((i != 0)and(i != H+1)):
   for j in range(W+2):
    if((j != 0)and(j != W+1)):
      if(S[i][j] == '#'):
        ans[i][j] = '#'
      else:
       if(S[i-1][j] == '#'):#上
        ans[i][j] += 1
       if(S[i+1][j] == '#'):#下
        ans[i][j] += 1
       if(S[i][j+1] == '#'):#右
        ans[i][j] += 1
       if(S[i][j-1] == '#'):#左
        ans[i][j] += 1 
       if(S[i-1][j+1] == '#'):#右上
        ans[i][j] += 1
       if(S[i-1][j-1] == '#'):#左上
        ans[i][j] += 1
       if(S[i+1][j+1] == '#'):#右下
        ans[i][j] += 1
       if(S[i+1][j-1] == '#'):#左下
        ans[i][j] += 1
        
for i in range(H+2):
  if((i != 0)and(i != H+1)):
   ans[i].pop(0)
   ans[i].pop(-1)
   for j in range(W):
    ans[i][j] = str(ans[i][j])
   print(''.join(ans[i]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
delta = [(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]
for y in range(h):
    for x in range(w):
        if s[y][x] == ".":
            cnt = 0
            for dx,dy in delta:
                if 0 <= x+dx < w and 0 <= y+dy < h and s[y+dy][x+dx] == "#":
                        cnt += 1
            s[y][x] = str(cnt)
for i in s:
    print("".join(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):
        if S[i][j] == '.':
            cnt = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    ni = i + k
                    nj = j + l
                    if 0 <= ni < h and 0 <= nj < w and S[ni][nj] == '#':
                        cnt += 1                    
            S[i][j] = cnt
                        
for i in range(h):
    print(''.join(map(str, S[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):
        if S[i][j] == '.':
            S[i][j] = 0
            
for i in range(h):
    for j in range(w):
        if S[i][j] == '#':
            for k in range(-1, 2):
                for l in range(-1, 2):
                    ni = i + k
                    nj = j + l
                    if 0 <= ni < h and 0 <= nj < w and S[ni][nj] != '#' and S[ni][nj] != '.':
                        S[ni][nj] += 1
                        
for i in range(h):
    print(''.join(map(str, S[i])))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
s.append(["$"]*(w+2))
for i in range(h):
    s.append(list("$"+input()+"$"))
s.append(["$"]*(w+2))
c=[]
for j in range(h+2):
    v=[]
    for k in range(w+2):
        v.append(0)
    c.append(v)
for y in range(1,h+1):
    for x in range(1,w+1):
        if s[y-1][x-1]=="#":
            c[y][x]+=1
        if s[y-1][x]=="#":
            c[y][x]+=1
        if s[y-1][x+1]=="#":
            c[y][x]+=1
        if s[y][x-1]=="#":
            c[y][x]+=1
        if s[y][x+1]=="#":
            c[y][x]+=1
        if s[y+1][x-1]=="#":
            c[y][x]+=1
        if s[y+1][x]=="#":
            c[y][x]+=1
        if s[y+1][x+1]=="#":
            c[y][x]+=1
for y in range(1,h+1):
    ans=""
    for x in range(1,w+1):
        if s[y][x]==".":
            ans+=str(c[y][x])
        else:
            ans+=s[y][x]
    print(ans)

Submission status: Accepted

Solution code:
# 075B
h,w = map(int, input().split())
ss = [list(map(str,str(input()))) for _ in range(h)]
# print(ss)
def count_bombs(ss,i,j):
    count=0
    if ss[i][j]=='#':
        return '#'
    for y in [-1,0,1]:
        for x in [-1,0,1]:
#             print(i+y, x+j, count)
            if 0<=i+y<h and 0<=j+x<w and ss[i+y][j+x]=='#':
                count+=1
    return str(count)

for i in range(h):
    for j in range(w):
        ss[i][j] = count_bombs(ss,i,j)
#         print('## ',i,j,ss[i][j])
for s in ss:
    print(''.join(s))