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())
masu = [list(input()) for a in range(h)]
masu2 =  [[0 for a in range(w)] for a in range(h)]
for i in range(h):
    for j in range(w):
        a = 0
        if i > 0:
            a += masu[i-1][max(0,j-1):min(w,j+2)].count("#")
        a += masu[i][max(0,j-1):min(w,j+2)].count("#")
        if i < h - 1:
            a += masu[i+1][max(0,j-1):min(w,j+2)].count("#")
        if masu[i][j] =="#":
            masu2[i][j]="#"
        else:
            masu2[i][j]=str(a)
for a in masu2:
    print("".join(a))

Submission status: Accepted

Solution code:
H, W = map(int, input(). split())
S = [input() for _ in range(H)]
new_S = [[0 for _ in range(W)] for _ in range(H)]
point = []
for h in range(H):
  for w in range(W):
    if S[h][w] == '#':
      point.append([h,w])
      if h > 0:
        new_S[h-1][w] += 1
        if w > 0:
          new_S[h-1][w-1] += 1
        if w < W-1:
          new_S[h-1][w+1] += 1
      if h < H-1:
        new_S[h+1][w] += 1
        if w > 0:
          new_S[h+1][w-1] += 1
        if w < W-1:
          new_S[h+1][w+1] += 1
      if w > 0:
        new_S[h][w-1] += 1
      if w < W-1:
        new_S[h][w+1] += 1

for p in point:
  new_S[p[0]][p[1]] = '#'

for s in new_S:
  print(''.join(map(str, s)))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
masu=[list(input()) for a in range(h)]
masu2=[[0 for a in range(w)] for a in range(h)]
for i in range(h):
    for j in range(w):
        a=0
        if i>0:
            a+=masu[i-1][max(0,j-1):min(w,j+2)].count("#")
        a+=masu[i][max(0,j-1):min(w,j+2)].count("#")
        if i<h-1:
            a+=masu[i+1][max(0,j-1):min(w,j+2)].count("#")
        if masu[i][j]=="#":
            masu2[i][j]="#"
        else:
            masu2[i][j]=str(a)
for a in masu2:
    print("".join(a))

Submission status: Accepted

Solution code:
def check_current_posi(s_i_list, h, w):
  _height = len(s_i_list)
  _width = len(s_i_list[0])
  if h == -1 or w == -1 or h >= _height or w >= _width:
    return False

  else:
    if s_i_list[h][w] == "#":
      # bomb
      return True

    else:
      # not bomb
      return False

# hとwは0から
def check_around_bomb_num(s_i_list, h, w):

  check_posi_h = h - 1
  check_posi_w = w - 1

  bomb_counter = 0

  for i in range(3):

    for j in range(3):

      if check_current_posi(s_i_list, check_posi_h, check_posi_w):
        bomb_counter += 1

      check_posi_w += 1

    check_posi_h += 1
    check_posi_w = w - 1
  
  return bomb_counter
      

# _input = [6, 6]
_input = list(map(int, input().split(" ")))

height = _input[0]
width = _input[1]

s_i_list = []

for i in range(height):
  _input_row = list(input())
  s_i_list.append(_input_row)

# s_i_list = [['#', '#', '#', '#', '#', '.'], ['#', '.', '#', '.', '#', '#'], ['#', '#', '#', '#', '.', '#'], ['.', '#', '.', '.', '#', '.'], ['#', '.', '#', '#', '.', '.'], ['#', '.', '#', '.', '.', '.']]

for row in range(height):
  for column in range(width):
    if s_i_list[row][column] == "#":
      continue

    s_i_list[row][column] = check_around_bomb_num(s_i_list, row, column)

for row in s_i_list:
  output_row = "".join(list(map(str,row)))
  print(output_row)

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
S = ['+' * (w + 2)]



def func(i, j):
    cnt = S[i-1][j-1:j+2].count('#')
    cnt += S[i+1][j-1:j+2].count('#')
    cnt += S[i][j-1:j+2:2].count('#')
    
    return str(cnt)

for i in range(h):
    S += [['+']+list(input())+['+']]
S += ['+' * (w + 2)]

for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j] == '.':
            S[i][j] = func(i,j)

for s in S[1:h+1]:
    print(''.join(s[1:w+1]))

Submission status: Accepted

Solution code:
import numpy as np
 
H, W = map(int, input().split())
S = np.array([list(input()) for _ in range(H)])
 
for i in range(H):
  for j in range(W):
    if S[i, j] == '.':
      S[i, j] = np.sum(S[max(i-1, 0):i+2, max(j-1, 0):j+2] == '#')
for s in S:
  print(''.join(s))

Submission status: Accepted

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

#配列の定義
arr = [[0 for c in range(W)] for r in range(H)]

#配列へ入力
for r in range(H):
    Sh=input()
    for c in range(W):
        arr[r][c]=Sh[c]

#判定
for r in range(H):
    for c in range(W):
        if arr[r][c]=='.':
            #置き換え実施
            #周りの#の数を確認
            counter=0 #初期化
            #一つ上の行
            #行があるか？
            if r-1>=0:
                #一つ上の行、一つ前の列
                #列があるか？
                if c-1>=0:
                    if arr[r-1][c-1]=='#':
                        counter+=1
                #一つ上の行、同一の列
                if arr[r-1][c]=='#':
                    counter+=1
                #一つ上の行、一つ後の列
                #列があるか？
                if c+1<=W-1:
                    if arr[r-1][c+1]=='#':
                        counter+=1
            #同一行
            #同一行、一つ前の列
            #列があるか？
            if c-1>=0:
                if arr[r][c-1]=='#':
                    counter+=1
            #同一行、一つ後の列
            #列があるか？
            if c+1<=W-1:
                if arr[r][c+1]=='#':
                    counter+=1
            #一つ下の行
            #行があるか？
            if r+1<=H-1:
                #一つ下の行、一つ前の列
                #列があるか？
                if c-1>=0:
                    if arr[r+1][c-1]=='#':
                        counter+=1
                #一つ下の行、同一の列
                if arr[r+1][c]=='#':
                    counter+=1
                #一つ下の行、一つ後の列
                #列があるか？
                if c+1<=W-1:
                    if arr[r+1][c+1]=='#':
                        counter+=1
            #置き換え
            arr[r][c]=counter

#出力する
for r in range(H):
    for c in range(W):
        print(arr[r][c],end='')
    print()

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    s = [input() for _ in range(H)]
    ctr = [[0] * W for _ in range(H)]
    for r, row in enumerate(s):
        for c, cell in enumerate(row):
            if cell == '#':
                for dr in (-1, 0, 1):
                    for dc in (-1, 0, 1):
                        if 0 <= r + dr < H and 0 <= c + dc < W and s[r + dr][c + dc] == '.':
                            ctr[r + dr][c + dc] += 1
    for r in range(H):
        for c in range(W):
            print('#' if s[r][c] == '#' else ctr[r][c], end='')
        print()


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

Solution code:
h,w = map(int,input().split(' '))
S = []
i = 0
while i < h:
    S.append(input())
    i += 1
ans = ''
p = 0
while p < h:
    q = 0
    while q < w:
        s0 = S[p][q]
        if s0 == '#':
            ans += '#'
        else:
            count = 0
            if p-1 >= 0:
                if S[p-1][q] == '#':
                    count += 1
                if q-1 >= 0 and S[p-1][q-1] == '#':
                    count += 1
                if q+1 < w and S[p-1][q+1] == '#':
                    count += 1
            if q-1 >= 0 and S[p][q-1] == '#':
                count += 1
            if q+1 < w and S[p][q+1] == '#':
                count += 1
            if p+1 < h:
                if S[p+1][q] == '#':
                    count += 1
                if q-1 >= 0 and S[p+1][q-1] == '#':
                    count += 1
                if q+1 < w and S[p+1][q+1] == '#':
                    count += 1
            ans += str(count)
        q += 1
    ans += '\n'
    p += 1
print(ans)

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
bo = []
bo.append("."*(b+2))
for i in range(a):
    s = input()
    s = "." + s + "."
    s = list(s)
    bo.append(s)
bo.append("."*(b+2))

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

for i in range(1,a+1):
    for j in range(1,b+1):
        cnt = 0
        if bo[i][j] == "#":
            continue
        for k in range(8):
            ch = bo[i-dx[k]][j-dy[k]]
            if ch == "#":
                cnt += 1
        bo[i][j] = str(cnt)

for i in range(1,a+1):
    print("".join(bo[i][1:-1]))

Submission status: Accepted

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]

for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        count = 0
        for x, y in dist:
            now_x = j + x
            now_y = i + y
            if now_x >= W or now_x < 0:
                continue
            if now_y >= H or now_y < 0:
                continue
            if board[now_y][now_x] == '#':
                count += 1
        ans[i][j] = str(count)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    H, W = map(int, input().split())
    grid = [list(input().strip("\n")) for _ in range(H)]
    ans = [[0] * W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            if grid[h][w] == "#":
                ans[h][w] = "#"
                continue
            count = 0
            if h > 0:
                if w > 0 and grid[h-1][w-1] == "#":
                    count += 1
                if grid[h-1][w] == "#":
                    count += 1
                if w < W - 1 and grid[h-1][w+1] == "#":
                    count += 1
            if w > 0 and grid[h][w-1] == "#":
                count += 1
            if w < W - 1 and grid[h][w+1] == "#":
                count += 1
            if h < H - 1:
                if w > 0 and grid[h+1][w-1] == "#":
                    count += 1
                if grid[h+1][w] == "#":
                    count += 1
                if w < W - 1 and grid[h+1][w+1] == "#":
                    count += 1
            ans[h][w] = count

    for h in range(H):
        print("".join(map(str, ans[h])))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
H,W = map(int,input().split())

S = [list(input()) for _ in range(H)]
point_list = np.array([['#']*W]*H)
dy = [0,0,1,1,1,-1,-1,-1]
dx = [1,-1,0,1,-1,0,1,-1]

for h in range(H):
  for w in range(W):
    count = 0
    for i in range(8):
      y = h+dy[i]
      x = w+dx[i]
      if y < 0 or y >= H or x < 0 or x >= W:
        continue
      if S[h][w] == '.' and S[y][x] == '#':
        count += 1
    if S[h][w] == '.':
      point_list[h][w] = count

for j in range(H):
  print(''.join(point_list[j]))

Submission status: Accepted

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

ans = board
dist = [(1,1), (1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1) ]
for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        count = 0
        for x, y in dist:
            now_x = j + x
            now_y = i + y
            if now_x >= W or now_x < 0:
                continue
            if now_y >= H or now_y < 0:
                continue
            if board[now_y][now_x] == '#':
                count += 1
        ans[i][j] = str(count)


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

Submission status: Accepted

Solution code:
import numpy as np
from collections import defaultdict
 
H, W = map(int, input().split())
S = np.array([list(input()) for _ in range(H)])
 
for i in range(H):
  for j in range(W):
    if S[i, j] == '.':
      d = defaultdict(int)
      S[i, j] = np.sum(S[max(i-1, 0):i+2, max(j-1, 0):j+2] == '#')
for s in S:
  print(''.join(s))

Submission status: Accepted

Solution code:
icase=0
if icase==0:
    s=[]
    h,w=map(int,input().split())
    t=[["#"]*w for i in range(h)]
    s.append(["."]*(w+2))
    for i in range(h):
        si=list("."+input()+".")
        s.append(si)
    s.append(["."]*(w+2))
    for i in range(h):
        for j in range(w):
            if s[i+1][j+1]==".":
                scnt=0
                for jj in range(j,j+3):
                    if s[i][jj]=="#":
                        scnt=scnt+1
                    if s[i+2][jj]=="#":
                        scnt=scnt+1
                if s[i+1][j]=="#":
                    scnt=scnt+1
                if s[i+1][j+2]=="#":
                    scnt=scnt+1
                t[i][j]=str(scnt)
    for i in range(h):
        si=""
        for j in range(w):                    
            si=si+t[i][j]
        print(si)

Submission status: Accepted

Solution code:
# AtCoder Beginner Contest 075
# B - Minesweeper
# https://atcoder.jp/contests/abc075/tasks/abc075_b
H, W, *S = open(0).read().split()
H, W = int(H), int(W)
S = [tuple(s) for s in S]

ans = [[0]*W for _ in [0]*H]
dirs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

for y, _s in enumerate(S):
    for x, s in enumerate(_s):
        if s == '#':
            ans[y][x] = '#'
            for dy, dx in dirs:
                if 0 <= x + dx < W and 0 <= y + dy < H and S[y+dy][x+dx] != "#":
                    ans[y+dy][x+dx] += 1

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

Submission status: Accepted

Solution code:
import sys
import numpy as np 

h, w = map(int, sys.stdin.readline().split())
grid = np.array([list(sys.stdin.readline().rstrip()) for _ in range(h)], dtype='U1')
grid = np.pad(grid, 1, 'constant')

def main():
  d = [(-1, 0), (0, -1), (1, 0), (0, 1)]
  cnt = np.zeros_like(grid, dtype=np.int8)
  for i in range(1, h + 1):
    for j in range(1, w + 1):
      if grid[i][j] == '.': continue
      for dy, dx in [(dy, dx) for dy in range(-1, 2) for dx in range(-1, 2)]:
        cnt[i+dy][j+dx] += 1
  cnt = cnt.astype('U1')
  cnt[grid == '#'] = '#'
  cnt = cnt[1:-1, 1:-1]
  for i in range(h):
    print(''.join(cnt[i]))
    
if __name__ == '__main__':
  main()

Submission status: Accepted

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

if h==1 and w==1:
  if s[0]=="#":
    print("#")
  else:
    print(0)
elif h==1 and w>1:
  for i in range(w):
    t=0
    if i==0:
      if s[0][i+1]=="#":t+=1
    elif 0<i<w-1:
      if s[0][i+1]=="#":t+=1
      if s[0][i-1]=="#":t+=1
    elif i==w-1:
      if s[0][i-1]=="#":t+=1
    if s[0][i]=="#":
      a[0][i]="#"
    else:
      a[0][i]=t
  tt=""
  for aa in a:
    for aaa in aa:
      tt+=str(aaa)
  print(str(tt))

elif w==1 and h>1:
  for i in range(h):
    t=0
    if i==0:
      if s[i+1]=="#":t+=1
    elif 0<i<h-1:
      if s[i+1]=="#":t+=1
      if s[i-1]=="#":t+=1
    elif i==h-1:
      if s[i-1]=="#":t+=1
    if s[i]=="#":
      a[i][0]="#"
    else:
      a[i][0]=t
  for aa in a:
    print(*aa)
elif w>1 and h>1:
  for i in range(h):
    for j in range(w):
      t=0
      if i==0:
        if j==0:
          if s[i][j+1]=="#":t+=1
          if s[i+1][j+1]=="#":t+=1
          if s[i+1][j]=="#":t+=1
        elif 0<j<w-1:
          if s[i][j+1]=="#":t+=1
          if s[i+1][j+1]=="#":t+=1
          if s[i+1][j]=="#":t+=1
          if s[i+1][j-1]=="#":t+=1
          if s[i][j-1]=="#":t+=1        
        elif j==w-1:
          if s[i+1][j]=="#":t+=1
          if s[i+1][j-1]=="#":t+=1
          if s[i][j-1]=="#":t+=1 
      elif 0<i<h-1:
        if j==0:
          if s[i-1][j]=="#":t+=1
          if s[i-1][j+1]=="#":t+=1
          if s[i][j+1]=="#":t+=1
          if s[i+1][j+1]=="#":t+=1
          if s[i+1][j]=="#":t+=1
        elif 0<j<w-1:
          if s[i][j+1]=="#":t+=1
          if s[i+1][j+1]=="#":t+=1
          if s[i+1][j]=="#":t+=1
          if s[i+1][j-1]=="#":t+=1
          if s[i][j-1]=="#":t+=1
          if s[i-1][j-1]=="#":t+=1
          if s[i-1][j]=="#":t+=1
          if s[i-1][j+1]=="#":t+=1
        elif j==w-1:
          if s[i+1][j]=="#":t+=1
          if s[i+1][j-1]=="#":t+=1
          if s[i][j-1]=="#":t+=1
          if s[i-1][j-1]=="#":t+=1
          if s[i-1][j]=="#":t+=1      
      elif i==h-1:
        if j==0:
          if s[i][j+1]=="#":t+=1
          if s[i-1][j+1]=="#":t+=1
          if s[i-1][j]=="#":t+=1
        elif 0<j<w-1:
          if s[i][j+1]=="#":t+=1
          if s[i-1][j+1]=="#":t+=1
          if s[i-1][j]=="#":t+=1
          if s[i-1][j-1]=="#":t+=1
          if s[i][j-1]=="#":t+=1        
        elif j==w-1:
          if s[i-1][j]=="#":t+=1
          if s[i-1][j-1]=="#":t+=1
          if s[i][j-1]=="#":t+=1
      if s[i][j]=="#":
        a[i][j]="#"
      else:
        a[i][j]=t
  for aa in a:
    tt=""
    for aaa in aa:
      tt+=str(aaa)
    print(tt)

Submission status: Accepted

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

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

def numbering(x, y):
    if field[y][x] == ".":
        c = 0
        for i in range(8):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx and nx < w and 0 <= ny and ny < h:
                if field[ny][nx] == "#":
                    c += 1
        field[y][x] = str(c)

for X in range(w):
    for Y in range(h):
        numbering(X, Y)

for Y in range(h):
    for X in range(w):
        print(field[Y][X], end = "")
    print()

Submission status: Accepted

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

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

for i in range(h):
    for j in range(w):
        c = 0
        if s[i][j] == '#':
            g[i].append('#')
            continue
        for n, m in ([-1,1],[-1,0],[-1,-1],[0,1],[0,-1],[1,1],[1,0],[1,-1]):
            new_h, new_w = i + n, j + m
            if new_h < 0 or new_h >= h or new_w < 0 or new_w >= w:
                continue
            if s[new_h][new_w] == '#':
                c += 1
        g[i].append(str(c))

ans = ''
for i in range(h):
    c = ''.join(g[i])
    ans += c
    if i != h-1:
        ans += '\n'

print(ans)

Submission status: Accepted

Solution code:
import pprint
h,w = map(int,input().split())
s = [["!"]*(w+2)] + [list("!" + input() + "!") for _ in range(h)] + [["!"]*(w+2)]
next = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "#":
            continue
        count = 0
        for d1,d2 in next:
            count += (s[i + d1][j+d2] == "#")
        s[i][j] = str(count)
ans = []
for k in s:
    k = [x for x in k if not x =="!"]
    if k:
        ans.append(k)
for adf in ans:
    print("".join(adf))

Submission status: Accepted

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

area = []
d = []

h += 2
w += 2

for i in range(h):
    if i == 0 or i == h-1:
        array = '.' * w
        area.append(array)
    else:
        array = input()
        array = '.' + array + '.'
        area.append(array)


def check(i, j):
    cnt = 0
    for x in range(i-1, i+2):
        for y in range(j-1, j+2):
            if area[x][y] == '#':
                cnt += 1
    return cnt

for i in range(1, h-1):
    for j in range(1, w-1):
        if area[i][j] == '.':
            d.append(check(i, j))
        else:
            d.append('#')
a = 0
for i in d:
    a += 1
    print(i, end='')
    if a == w-2:
        print()
        a = 0

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
maze=[[None] for _ in range(h)]
for i in range(h):
  s=list(input())
  for j in range(w):
    if s[j]=="#":
      pass
    else:
      s[j]=0
  maze[i]=s

for i in range(h):
  for j in range(w):
    if maze[i][j]=="#":
      for ii in range(i-1,i+1+1):
        for jj in range(j-1,j+1+1):
          if 0<=ii<h and 0<=jj<w:
            if maze[ii][jj]!="#":
              maze[ii][jj]+=1
for i in range(h):
  for j in range(w):
    print(maze[i][j],end="")
  print()

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
maze=[[None] for _ in range(h)]
for i in range(h):
  s=list(input())
  for j in range(w):
    if s[j]=="#":
      pass
    else:
      s[j]=0
  maze[i]=s

for i in range(h):
  for j in range(w):
    if maze[i][j]=="#":
      for ii in range(i-1,i+1+1):
        for jj in range(j-1,j+1+1):
          if 0<=ii<h and 0<=jj<w:
            if maze[ii][jj]!="#":
              maze[ii][jj]+=1
for i in range(h):
  for j in range(w):
    print(maze[i][j],end="")
  print()

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            s[i][j] = 0
            left = max(0, j - 1)
            right = min(w - 1, j + 1)
            up = max(0, i - 1)
            down = min(h - 1, i + 1)
            for k in range(up, down + 1):
                for l in range(left, right + 1):
                    if s[k][l] == '#':
                        s[i][j] += 1

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

Submission status: Accepted

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

move = [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]
for i in range(h):
    ans = []
    for j in range(w):
        cnt = "#"
        if board[i][j] == "#":
            ans.append("#")
            continue
        cnt = 0
        for mx,my in move:
            x,y = i+mx,j+my
            if 0 > x or x >= h or 0 > y or y >= w:continue
            if board[x][y] == "#":
                cnt += 1
        ans.append(str(cnt))
    print("".join(ans))

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
setrecursionlimit(10 ** 7)

h,w = map(int,stdin.readline().rstrip().split())
s = [stdin.readline().rstrip() for _ in range(h)]

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

x = [-1,0,1]
y = [-1,0,1]

for i in range(h):
    for j in range(w):
        count = 0
        for xx in x:
            for yy in y:
                if xx == 0 and yy == 0:
                    continue
                if j+xx < 0 or j+xx > w-1:
                    continue
                if i+yy < 0 or i+yy > h-1:
                    continue
                if s[i+yy][j+xx] == "#":
                    count += 1
        if s[i][j] == "#":
            li[i][j] = "#"
        else:li[i][j] = str(count)

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

Submission status: Accepted

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

ans = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            for x in (-1, 0, 1):
                for y in (-1, 0, 1):
                    if 0 <= i+x < h and 0 <= j+y < w:
                        if s[i+x][j+y] == "#":
                            ans[i+x][j+y] = "#"
                        else:
                            ans[i+x][j+y] += 1

for i in ans:
    for j in i:
        print(j, end = "")
    print("\n", end = "")

Submission status: Accepted

Solution code:
def resolve():
    h, w = map(int, input().split())
    s = [input() for _ in range(h)]
    ans = [["#"]*w for _ in range(h)]

    for i in range(h):
        for j in range(w):
            if s[i][j] == ".":
                mine = 0
                for k in range(max(0, i-1), min(h, i+2)):
                    for l in range(max(0, j-1), min(w, j+2)):
                        if s[k][l] == "#":
                            mine += 1
                ans[i][j] = str(mine)
    for m in range(h):
        print("".join(ans[m]))

resolve()

Submission status: Accepted

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

for s in S:
    for i in range(W):
        if s[i] == ".": s[i] = 0

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

for s in S:
    print("".join(map(str, s)))

Submission status: Accepted

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

S=[]
for i in range(H):
  s=list(input())
  S.append(s)
D=[(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]
#print(S)
ans=[[] for _ in range(H)]
for h in range(H):
  for w in range(W):
    if S[h][w]=="#":
        ans[h].append("#")
    else:
      count=0
      for d in D:
        dh,dw=d
        nh=h+dh
        nw=w+dw
        if 0<=nh and nh<H and 0<=nw and nw<W and S[nh][nw]=="#":
          count+=1
      ans[h].append(str(count))
for A in ans:
  print("".join(A))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
vec = [(1,0),(0,1),(-1,0),(0,-1),(-1,-1),(1,1),(-1,1),(1,-1)]
ans = []
s = [input()for _ in range(h)]
def check(x,y):
    if x>=0 and y >=0 and x <=h-1 and y <= w-1:
        return s[x][y] == '#'
    else:return 0

for x in range(h):
    next = []
    for y in range(w):
        
        if s[x][y] == '#':
            next.append('#')
            continue
        res = 0
        for tx,ty in vec:
            nx = x+tx
            ny = y + ty
            res +=check(nx,ny)
        next.append(str(res))
    ans.append(next)
for i in range(h):
    print("".join(ans[i]))

Submission status: Accepted

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

for y in range(h):
    for x in range(w):
        if s[y][x]=='.':
            count = 0
            for dy,dx in ((1,0),(1,1),(0,1),(1,-1),(-1,1),(0,-1),(-1,-1),(-1,0)):
                ny,nx = dy+y,dx+x
                if (0<=ny<h and 0<=nx<w and s[ny][nx]=="#"):
                    count += 1
            s[y][x] = str(count)
for i in s:
    print(''.join(i))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = []
ans = [["#"]*w for _ in range(h)]
s.append(["."]*(w+2))
for i in range(h):
    s.append(["."] + list(input()) + ["."])
s.append(["."]*(w+2))
dx = [0,1,1,1,0,-1,-1,-1]
dy = [-1,-1,0,1,1,1,0,-1]
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == ".":
            count = 0
            for k in range(8):
                if s[i+dx[k]][j+dy[k]] == "#":
                    count += 1 
            ans[i-1][j-1] = str(count)
for i in range(h):
    print("".join(ans[i]))

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
f = [list(input()) for _ in range(h)]
for k in range(h):
    for l in range(w):
        if f[k][l] == ".":
            c = 0
            for i in range(-1,2):
                for j in range(-1,2):
                    xi,yi = k+i,l+j
                    #xi,yiが範囲内で水溜りなら
                    if 0 <= xi < h  and 0 <= yi < w and f[xi][yi] == "#":
                        c += 1
            f[k][l] = str(c)

for m in range(h):
    print(''.join(f[m]))

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, input().split())
    grid = [["." for _ in range(w + 2)]]
    for _ in range(h):
        grid.append(["."] + list(input()) + ["."])
    grid.append(grid[0])
    answer = [["0" for _ in range(w)] for _ in range(h)]
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            now_ans = 0
            if grid[i][j] == ".":
                for k in range(-1, 2):
                    now_ans += ("".join(grid[i + k][j - 1:j + 2])).count("#")
                answer[i - 1][j - 1] = str(now_ans)
            else:
                answer[i - 1][j - 1] = "#"
    for ans in answer:
        print("".join(ans))


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] == "#":
            continue
        cnt = 0
        for nxt_i in (i - 1, i, i + 1):
            for nxt_j in (j - 1, j, j + 1):
                if not(0 <= nxt_i < h) or not(0 <= nxt_j < w):
                    continue
                if s[nxt_i][nxt_j] == "#":
                    cnt += 1
        s[i][j] = str(cnt)

for res in s:
    print("".join(res))

Submission status: Accepted

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

def inb(x, y):
    return 0 <= x < h and 0 <= y < w

def count(x, y):
    if board[x][y] == '#':
        return '#'
    ret = 0
    for i in [-1, 0, 1]:
        for j in [-1, 0, 1]:
            if i == 0 and j == 0:
                continue
            nx, ny = x + i, y + j
            if not inb(nx, ny):
                continue
            if board[nx][ny] == '#':
                ret += 1
    return str(ret)

ret = [['?' for _ in range(w)] for _ in range(h)]
for i in range(h):
    for j in range(w):
        ret[i][j] = count(i, j)
for i in ret:
    print(''.join(i))

Submission status: Accepted

Solution code:
# solution
import io
import math

nim, mike = map(int, input().split())
data = [['.'] * (mike+2)]
data += [['.'] + list(input()) + ['.'] for _ in range(nim)]
data += [['.'] * (mike+2)]

result = []
for i in range(1, nim + 1):
  strings = str()
  for j in range(1, mike + 1):
    counter = 0
    if data[i][j] == '.':
      for k in range(-1, 2, 1):
        for l in range(-1, 2, 1):
          if data[i+k][j+l] == '#':
            counter += 1
      strings += str(counter)
    else:
      strings += '#'
  result += [strings]
    
for i in result:
  print (i)