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:
# -*- coding: utf-8 -*-

# 入力
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] == "#":
            continue

        # 検査対象のセルを格納する配列。初期値は現在対象のセル
        x_ary = [i]
        y_ary = [j]

        # 上下左右のセルについて存在確認
        if 0 <= i-1 and i-1 <= H-1:
            x_ary.append(i-1)
        if 0 <= i+1 and i+1 <= H-1:
            x_ary.append(i+1)
        if 0 <= j-1 and j-1 <= W-1:
            y_ary.append(j-1)
        if 0 <= j+1 and j+1 <= W-1:
            y_ary.append(j+1)

        # 周囲のボムの数のカウンター
        num_of_bombs = 0
        
        # ボムの数を数える
        for x in x_ary:
            for y in y_ary:
                if S[x][y] == "#":
                    num_of_bombs += 1

        # 文字列を置き換える
        s = list(S[i])
        s[j] = str(num_of_bombs)
        S[i] = ''.join(s)

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

Submission status: Accepted

Solution code:
def chk(s,i,j):
          
    cnt = 0
    for ii in range(max(0,i-1),min(h,i+2)):
        for jj in range(max(0,j-1),min(w,j+2)):
            if s[ii][jj] == '#':
                cnt +=1
    s[i][j]= str(cnt)

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

s=[]
for _ in range(h):
    s.append(list(input()))

for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            chk(s,i,j)

for i in range(len(s)):
    print("".join(s[i]))

Submission status: Accepted

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

mList = []
mList.append('-'*(w+2))

for i in range(h):
  mList.append(list('-'+input()+'-'))

resultMap = []
mList.append('-'*(w+2))
bomb = 0
for i in range(1,h+1):
  for j in range(1,w+1):
    if mList[i][j] == '.':
      for k in range(-1,2,1):
        for l in range(-1,2,1):
          if k == 0 and l == 0:
            pass
          elif mList[i+k][j+l] =="#":
            bomb+=1
      print(bomb,end='')
      bomb = 0
    else:
      print("#",end='')
  print()

Submission status: Accepted

Solution code:

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

S=[]
a1="."*(W+2)
S.append(a1)
for i in range(H):
    a2="."
    a3=input()
    a4=a2+a3+a2
    S.append(a4)
S.append(a1)




for i in range(H):
    for j in range(W):
        count=0
        if S[i+1][j+1]=="#":
            count="#"
        else:
            if S[i][j]=="#":
                count+=1
            if S[i][j+1]=="#":
                count+=1
            if S[i][j+2]=="#":
                count+=1
            if S[i+1][j]=="#":
                count+=1
            if S[i+1][j+2]=="#":
                count+=1
            if S[i+2][j]=="#":
                count+=1
            if S[i+2][j+1]=="#":
                count+=1
            if S[i+2][j+2]=="#":
                count+=1
        print(count,end="")
    print("")

Submission status: Accepted

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

# H = 3
# W = 5
areas = [input() for _ in range(H)]
points = [[0] * W for _ in range(H)]

# areas = ['.....', '.#.#.', '.....']
# points = [[0] * W for _ in range(H)]

for h in range(H):
    for w in range(W):
        if areas[h][w] == '#':
            continue
        # 条件を変数にいれるべき...
        # 右隣
        if w + 1 < W and areas[h][w+1] == '#':
            points[h][w] += 1
        # 右斜め下
        if w + 1 < W and h + 1 < H and areas[h+1][w+1] == '#':
            points[h][w] += 1
        # 真下
        if h + 1 < H and areas[h+1][w] == '#':
            points[h][w] += 1
        # 左下
        if w - 1 >= 0 and h + 1 < H and areas[h+1][w-1] == '#':
            points[h][w] += 1
        # 左
        if w - 1 >= 0 and areas[h][w-1] == '#':
            points[h][w] += 1
        # 左斜め上
        if w - 1 >= 0 and h - 1 >= 0 and areas[h-1][w-1] == '#':
            points[h][w] += 1
        # 真上
        if h - 1 >= 0 and areas[h-1][w] == '#':
            points[h][w] += 1
        # 右斜め上
        if h - 1 >= 0 and w + 1 < W and areas[h-1][w+1] == '#':
            points[h][w] += 1


ans = []
for h in range(H):
    s = ''
    for w in range(W):
        area = areas[h][w]
        p = points[h][w]
        if p != 0:
            s += str(p)
        else:
            if area == '#':
                s += '#'
            else:
                s += '0'
    ans.append(s)
print('\n'.join(ans))

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 = [int(i) for i in input().split()]
li = ['0'] * H
for i in range(H) :
    li[i] = list(input())
for i in range(H) :
    for j in range(W) :
        if li[i][j] == "." :
            count = 0
            a = i -1
            b = i + 1
            c = j - 1
            d = j + 1
            if a >= 0 :
                if li[a][j] == "#" :
                    count+=1
            if b <= H-1 :
                if li[b][j] == "#" :
                    count+=1
            if c >= 0 :
                if li[i][c] == "#" :
                    count+=1
            if d <= W-1 :
                if li[i][d] == "#" :
                    count+=1
            if a >= 0 and c >= 0 :
                if li[a][c] == "#" :
                    count+=1
            if a >= 0 and d <= W-1 :
                if li[a][d] == "#":
                    count+=1
            if b <= H-1 and c >= 0 :
                if li[b][c] == "#" :
                    count+=1
            if b <= H-1 and d <= W - 1 :
                if li[b][d] == "#" :
                    count+=1
            li[i][j] = str(count)
for i in range(H) :
    a = ''.join(li[i])
    print(a)

Submission status: Accepted

Solution code:
def MineSweeper():
    H, W = map(int, input().split())
    strlist = []
    newlist = []
    for _ in range(0, H):
        strlist.append(list(input()))
    for i in range(0, H):
        templist = []
        for j in range(0, W):
            templist.append(0)
            if strlist[i][j] == '#':
                templist[j] = '#'
            else:
                for k in range(-1, 2):
                    if i + k == -1 or i + k == H:continue

                    for l in range(-1, 2):
                        if l + j == -1 or l + j == W:continue
                        if strlist[i + k][j + l] == '#':
                            templist[j] += 1
        newlist.append(templist)

    for lst in newlist:
        print(''.join(map(str, lst)))

MineSweeper()

Submission status: Accepted

Solution code:
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]

c = 0
for i in range(H):
  if S[i] != "#" * W:
    for j in range(W):
      if S[i][j] == ".":
        S[i][j] = "0"
      else:
        c += 1

if c != H * W:
 for x in range(H):
  for y in range(W):
    if S[x][y] == "#":
      for z in range(8):
        X = x + dx[z]
        Y = y + dy[z]
        if 0 <= X <= H-1  and  0 <= Y <= W-1:
          if S[X][Y] != "#":
            S[X][Y] = str(int(S[X][Y]) + 1)


for i in range(H):
  a = ""
  for j in range(W):
    a += S[i][j]
  print(a)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
H, W = map(int, input().split())
S = [(input()) for _ in range(H)]
T = [[0 for _ in range(W)] for _ in range(H)]

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

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

Submission status: Accepted

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

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

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

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

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

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


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

Submission status: Accepted

Solution code:
H,W=map(int,input().split(' '))
mine = [input() for i in range(H)]
def mine_chk(h,w):
    cnt = 0
    for i in range(max(0,h-1),min(h+2,H)):
        for j in range(max(0,w-1),min(W,w+2)):
            if mine[i][j] =="#":
                cnt += 1
    return cnt



for i in range(H):
    for j in range(W):
        if mine[i][j] == "#":
            print("#",end="")
        else:
            print(mine_chk(i,j),end="")
    print("")

Submission status: Accepted

Solution code:
"""import sys
input = sys.stdin.readline().strip"""
h,w=map(int,input().split())
#print(h,w)
s=[input() for _ in range(h)]
#print(s)
ans=[["#"]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            continue
        cnt=0
        for k in [-1,0,1]:
            for l in [-1,0,1]:
                if k==0 and l==0:
                    continue
                if 0<=i+k<h and 0<=j+l<w:
                    if s[i+k][j+l]=="#":
                        cnt+=1
        ans[i][j]=cnt
#print(ans)
for a in ans:
    #print(a)
    print("".join(map(str,a)))

Submission status: Accepted

Solution code:
h,w = list(map(int,input().split()))
s=[i for i in range(h)]
for i in range(h):
  s[i] = input()
count = [[0 for i in range(w)]for j in range(h)]
if h==1and w==1:
  if s=="#":
    print("#")
    exit()
  else:
    print(0)
    exit()
  
elif h==1:
  for i in range(w):
    if s[0][i]=="#":
      if i==0:
        count[0][i+1]+=1
      elif i==w-1:
        count[0][i-1]+=1
      else:
        count[0][i-1]+=1
        count[0][i+1]+=1
  ans=""
  for i in range(w):
    if s[0][i]!="#":
      ans+=str(count[0][i])
    else:
      ans+="#"
  print(ans)
  exit()
elif w==1:
  for i in range(h):
    if s[i][0]=="#":
      if i==0:
        count[i+1][0]+=1
      elif i==h-1:
        count[i-1][0]+=1
      else:
        count[i-1][0]+=1
        count[i+1][0]+=1
  
  for i in range(h):
    ans=""
    if s[i][0]!="#":
      ans+=str(count[i][0])
    else:
      ans+="#"
    print(ans)
  exit()


for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      if i==0:
        if j==0:
          count[i+1][j]+=1
          count[i+1][j+1]+=1
          count[i][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]  +=1
          count[i+1][j+1]+=1
      elif i==h-1:
        if j==0:
          count[i][j+1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
      else:
        if j==0:
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j+1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1
        elif j==w-1:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1

for i in range(h):
  ans=""
  for j in range(w):
    if s[i][j]!="#":
  	  ans+=str(count[i][j])
    else:
      ans+="#"
  print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

H, W = MAP()
S = [list(input()) for _ in range(H)]

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

for i in range(H):
	for j in range(W):
		tmp = 0
		for k in range(8):
			x = j+dx[k]
			y = i+dy[k]
			if 0 <= x <= W-1 and 0 <= y <= H-1:
				if S[y][x] == "#":
					tmp += 1
		if not S[i][j] == "#":
			S[i][j] = tmp

for i in range(H):
	print(*map(str, S[i]), sep="")

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[]
for i in range(h):
  s.append([i for i in input()])
l=[["" for i in range(w)] for j in range(h)]
for hh in range(h):
  for ww in range(w):
    if s[hh][ww]=="#":
      l[hh][ww]="#"
    else:  
      cnt=0
      for hhh in [-1,0,1]:
        for www in [-1,0,1]:
          if 0<=hh+hhh<h and 0<=ww+www<w and s[hh+hhh][ww+www]=="#":
            cnt+=1
      l[hh][ww]=cnt
for i in l:
  for j in i:
    print(j,end="")
  print()

Submission status: Accepted

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

s = [''] * h

for l in range(h):
  s[l] = list(input())

result = [''] * h
for i in range(h):
  result[i] = [''] * w
  for j in range(w):
    count = 0
    if s[i][j] == "#":
      result[i][j] = "#"
    else:
      for n in [-1, 0, 1]:
        for m in [-1, 0, 1]:
          if (i + n < 0) or (j + m < 0) or (i + n >= h) or (j + m >= w):
            continue
          elif s[i + n][j + m] == "#":
            count += 1
      result[i][j] = str(count)

for x in range(h):
  print(''.join(result[x]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
H, W = [int(x) for x in input().rstrip().split()]
s = []
for _ in range(H):
    s.append(list(input().rstrip()))

def bomb(x, y):
    cnt = 0
    for k in [[x+1, y], [x+1, y+1], [x+1, y-1], [x, y+1], [x, y-1], [x-1, y], [x-1, y+1], [x-1, y-1]]:
        if 0 <= k[0] < H and 0 <= k[1] < W:
            if s[k[0]][k[1]] == "#":
                cnt += 1
    return str(cnt)

for i in range(H):
    for j in range(W):
        if s[i][j] == ".":
            s[i][j] = bomb(i, j)
for l in s:
    print("".join(l))

Submission status: Accepted

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

ans = [[0]*w for i in range(h)]
pattern = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
for y in range(h):
    for x in range(w):
        if graph[y][x] == '#':
            ans[y][x] = float('INF')

            for dy, dx in pattern:
                ny, nx = y + dy, x + dx
                if ny < 0 or ny >= h or nx < 0 or nx >= w:
                    continue

                ans[ny][nx] += 1
        else:
            continue

for line in ans:
    print(''.join(map(str, line)).replace('inf', '#'))

Submission status: Accepted

Solution code:
import sys

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

rows = []
for i in range(H):
    chars = list(input())
    rows.append([1 if c == '#' else 0 for c in chars])

for h in range(H):
    for w in range(W):
        if rows[h][w] == 1:
            sys.stdout.write('#')
            continue

        range_w = (max(0, w - 1), w + 2)
        range_h = (max(0, h - 1), h + 2)

        target_rows = [
            sample_row[range_w[0]:range_w[1]]
            for sample_row in rows[range_h[0]:range_h[1]]
        ]

        conv_result = sum([sum(r) for r in target_rows])
        sys.stdout.write(str(conv_result))
    
    print()

Submission status: Accepted

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

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

area = []
for i in range(h):
    area.append(input())
#print(area)

for i in range(h):
    for j in range(w):
        if area[i][j] == "#":
            print("#", end="")
        else:
            count = 0
            for k in range(max(0, i-1), min(i+1, h-1)+1):
                for l in range(max(0, j-1), min(j+1, w-1)+1):
                    if area[k][l] == "#":
                        count += 1
            print(count, end = "")
    print()

Submission status: Accepted

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

for i in range(H):
  for j in range(W):
    if area[i][j] == '#':
      print('#', end='')
    else:
      count = 0
      for k in range(max(0, i-1), min(i+1, H-1)+1):
        for l in range(max(0, j-1), min(j+1, W-1)+1):
          if area[k][l] == '#':
            count += 1
      print(count, end='')
  print()

Submission status: Accepted

Solution code:
def minesweeper(s, i, j):
    ans = 0
    for ii in range(-1,2):
        for jj in range(-1,2):
            try:
                if s[i+ii][j+jj] == "#" and ( (i+ii >= 0) and (j+jj >= 0) ):
                    ans += 1
            except:
                None
    return ans

h, w = map(int, input().split())
s = [input() for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            print(minesweeper(s, i, j), end="")
        else:
            print("#", end="")
    print("")

Submission status: Accepted

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

# .を0に置き換え
grid = [[0 if cell=="." else "#" for cell in row] for row in S] 

import itertools
for r in range(H):
  for w in range(W):
    if S[r][w] != "#":
      continue
    for dx, dy in itertools.product([-1, 0, 1], repeat=2):
      x = r+dx
      y = w+dy
      if x < 0 or x >= H or y < 0 or y >= W:
        continue
      if S[x][y] == "#":
        continue
      grid[x][y] += 1
for row in grid:
  print(*row, sep="")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#N = int(input())
H, W = map(int, input().split())
S = []
StartEnd = []
for _ in range(W+2) :
  StartEnd.append("N")
S.append(StartEnd)
for _ in range(H) :
  l = list(str(input()))
  l.insert(0, "N")
  l.append("N")
  S.append(l)
S.append(StartEnd)
#print (S)

for i in range(H+2) : 
  for j in range(W+2) :
    if S[i][j] == "." :
      cnt = 0
      for m in range(-1, 2) :
        for n in range(-1, 2) :
          if S[i+m][j+n] == "#" :
            cnt += 1
      S[i][j] = str(cnt)

for i in range(1, H+1) :
  ans = ""
  for j in range(W+2) :
    if S[i][j] != "N" :
      ans += S[i][j]
  print (ans)

Submission status: Accepted

Solution code:
def check_bomb(S_list,h,w):
    num_of_bomb=0
    for i in range(3):
        if h-1+i<0 or h-1+i>len(S_list)-1:
            continue

        for j in range(3):
            if w-1+j<0 or w-1+j>len(S_list[0])-1:
                continue
            if S_list[h-1+i][w-1+j]=="#":
                num_of_bomb+=1
    return "{}".format(num_of_bomb)



def main():
    input_str1 = list(map(int, input().split()))
    H = input_str1[0]
    W = input_str1[1]
    S_list = []
    for i in range(H):
        S_list.append(input())

    for i in range(H):
        for j in range(W):
            if S_list[i][j]==".":
                temp_list = S_list[i]
                temp_list = temp_list[:j]+check_bomb(S_list,i,j)+temp_list[j+1:]
                S_list[i]=temp_list
                
    for i in range(H):
        print(S_list[i])




main()

Submission status: Accepted

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

def bomb(x,y):
    if s[y][x]=='#':
        return '#'
    else:
        c=0
        for i in range(-1,2):
            for j in range(-1,2):
                if not (i==0 and j==0):
                    if 0<=x+i<=w-1 and 0<=y+j<=h-1:
                        if s[y+j][x+i]=='#':
                            c+=1
        return str(c)

for i in range(h):
    l=[]
    for j in range(w):
        l.append(bomb(j,i))
    print(''.join(l))

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

for i in range(H):
  for j in range(W):
    if area[i][j] == '#':
      print('#', end='')
    else:
      count = 0
      for k in range(max(0, i-1), min(i+1, H-1)+1):
        for l in range(max(0, j-1), min(j+1, W-1)+1):
          if area[k][l] == '#':
            count += 1
      print(count, end='')
  print()

Submission status: Accepted

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

def plus(x,y):

    if (0 <= x < h) and (0 <= y < w):
        #print(x,y)
        if s[x][y] == '.':
            s[x][y] = 1
        elif s[x][y] != '#':
            s[x][y] += 1

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            s[i][j] = 0
        if s[i][j] == "#":
            for k in range(-1,2):
                for l in range(-1,2):
                    plus(i+k,j+l)

for i in s:
    print(*i,sep='')

Submission status: Accepted

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

A = [''] * H

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

for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            A[h] += '#'
        else:
            c = 0
            for d in di:
                h1 = h + d[0]
                w1 = w + d[1]
                if 0 <= h1 < H and 0 <= w1 < W:
                    if S[h1][w1] == '#':
                        c += 1
            A[h] += str(c)

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

Submission status: Accepted

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

Submission status: Accepted

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

m = []

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

a = []
b = [-1,1,0]

for i in range(len(b)):
    for j in range(len(b)):
        a.append([b[i],b[j]])

for i in range(H):
    for j in range(W):
        if m[i][j]=="#":
            continue
        count = 0
        for k in range(len(a)):
            x = a[k][0]
            y = a[k][1]
            if (x+j)<0 or (y+i)<0 or (x+j)>=W or (y+i)>=H:
                continue
            if m[y+i][x+j]=="#":
                count = count + 1
        m[i][j]=count

for i in range(H):
    print(*m[i],sep="")

Submission status: Accepted

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

m = []

for _ in range(H):
    S = list(input())
    m.append(S)
a = [[-1,1],[0,1],[1,1],[-1,0],[1,0],[-1,-1],[0,-1],[1,-1]]
for i in range(H):
    for j in range(W):
        if m[i][j]=="#":
            continue
        count = 0
        for k in range(len(a)):
            x = a[k][0]
            y = a[k][1]
            if (x+j)<0 or (y+i)<0 or (x+j)>=W or (y+i)>=H:
                continue
            if m[y+i][x+j]=="#":
                count = count + 1
        m[i][j]=count

for i in range(H):
    print(*m[i],sep="")

Submission status: Accepted

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

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

for i in range(H):
    for j in range(W):
        if list[i][j] == "#":
            continue
        num = 0
        for k in range(8):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x >= H:
                continue
            if y < 0 or y >= W:
                continue
            if list[x][y] == "#":
                num += 1
        list[i][j] = num
        
for l in list:
    print(''.join([str(c) for c in l]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
ww = "." * (w+2)
board = [ww] * (h+2)
for i in range(1, h+1):
    bb = "." + input() + "."
    board[i] = bb

for i in range(1, h+1):
    for j in range(1, w+1):
        if board[i][j] == "#":
            print("#", end='')
        else:
            count = 0
            if board[i+1][j] == "#":
                count += 1
            if board[i][j+1] == "#":
                count += 1
            if board[i+1][j+1] == "#":
                count += 1
            if board[i+1][j-1] == "#":
                count += 1
            if board[i][j-1] == "#":
                count += 1
            if board[i-1][j-1] == "#":
                count += 1
            if board[i-1][j+1] == "#":
                count += 1
            if board[i-1][j] == "#":
                count += 1
            print(count, end='')
        if j == w:
            print("\n", end='')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
S=list(input() for _ in range(h))
ans=[[0]*(w+2) for _ in range(h+2)]
for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i-1][j-1]=="#":
            ans[i-1][j-1] += 1
            ans[i-1][j] += 1
            ans[i-1][j+1] += 1
            ans[i][j-1] += 1
            ans[i][j+1] += 1
            ans[i+1][j-1] += 1
            ans[i+1][j] += 1
            ans[i+1][j+1] += 1
for i in range(h):
    for j in range(w):
        if S[i][j]=="#":
            ans[i+1][j+1]  = "#"
        print(ans[i+1][j+1],end="")
        if j==w-1 and i<h-1:print("")