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 = [list(input()) for i in range(H)]

for i in range(H):
    for j in range(W):
        ans = 0
        if S[i][j]=='.':
            for h in range(max(0,i-1),min(H,i+2)):
                for w in range(max(0,j-1),min(W,j+2)):
                    if S[h][w]=='#':
                        ans += 1
            S[i][j] = str(ans)
        
for i in range(H):
    print(''.join(S[i]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
f = [["."] * (w + 2)] + \
    [list("." + input() + ".") for i in range(h)] + \
    [["."] * (w + 2)]
ans = [['#']*w for i in range(h)]

for ch in range(1, h+1):
    for cw in range(1, w+1):
        if f[ch][cw] == '#':
            continue
        cnt = 0
        for hh in range(ch-1, ch+2,1):
            cnt += f[hh][cw-1:cw+2].count('#')
        ans[ch-1][cw-1] = str(cnt)
for i in range(h):
    print(''.join(ans[i]))

Submission status: Accepted

Solution code:
import sys, re, os
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, bisect_left, bisect_right

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

H, W = MAP()
C = [input() for i in range(H)]
C = ["0"*(W+2)] + ["0"+C[i]+"0" for i in range(H)] + ["0"*(W+2)]
Z = [[0] * (W + 2) for i in range(H + 2)]

def surround_plus(i, j, Z):
    dx = [0, 1, 1, 1, 0,-1,-1,-1]
    dy = [1, 1, 0,-1,-1,-1, 0, 1]
    for idx in range(8):
        Z[i + dx[idx]][j + dy[idx]] += 1
    return Z


for i in range(H + 2):
    for j in range(W + 2):
        if C[i][j] == "#":
            Z = surround_plus(i, j, Z)

A = [[""] * W for i in range(H)]

for i in range(1, H + 1):
    for j in range(1, W + 1):
        if C[i][j] != "#":
            A[i-1][j-1] = str(Z[i][j])
        else:
            A[i-1][j-1] = "#"
print(*["".join(a) for a in A], sep="\n")

Submission status: Accepted

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

S.append(['x'] * (W+2))
around = [[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1],[0,-1]]
for i in range(H+1) :
    for j in range(W+1) :
        if S[i][j] == '.' :
#            print('S[{}][{}]'.format(i,j))
            count = 0
            for k in range(8) :
                if S[i+around[k][0]][j+around[k][1]] == '#' :
#                    print('         around[{}] = {}'.format(k,around[k]))
#                    print('             S[{}][{}] = #'.format(i+around[k][0],j+around[k][1]))
                    count += 1
            S[i][j] = count

ans = ['']* H
for i in range(1,H+1) :
    for j in range(1,W+1) :
        ans[i-1] += str(S[i][j])
    print(ans[i-1])

Submission status: Accepted

Solution code:
def main():
    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] == "#":
                continue

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

            S[i][j] = str(cnt)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    S = [["*" for _ in range(W + 2)] for _ in range(H + 2)]
    for i in range(1, H + 1):
        S[i][1: W + 1] = input()
    dx = [-1, 0, 1, -1, 1, -1, 0, 1]
    dy = [-1, -1, -1, 0, 0, 1, 1, 1]

    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if S[i][j] == "#":
                continue

            cnt = 0
            for k in range(8):
                if S[i + dx[k]][j + dy[k]] == "#":
                    cnt += 1

            S[i][j] = str(cnt)

    for i in range(1, H + 1):
        print("".join(S[i][1: W + 1]))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if S[i][j] == "#":
                continue

            cnt = 0
            if S[i - 1][j - 1] == "#":
                cnt += 1
            if S[i - 1][j] == "#":
                cnt += 1
            if S[i - 1][j + 1] == "#":
                cnt += 1
            if S[i][j - 1] == "#":
                cnt += 1
            if S[i][j + 1] == "#":
                cnt += 1
            if S[i + 1][j - 1] == "#":
                cnt += 1
            if S[i + 1][j] == "#":
                cnt += 1
            if S[i + 1][j + 1] == "#":
                cnt += 1

            S[i][j] = str(cnt)

    for i in range(1, H + 1):
        print("".join(S[i][1: W + 1]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
S=[list(map(str,input().split())) for i in range(H)]
ans=[[0 for i in range(W+2)]for j in range(H+2)]
inf=10
for i in range(H):
    S[i]=list(S[i][0])
for i in range(W):
    for j in range(H):
        if S[j][i]=='#':
            ans[j+1][i+1]=inf

for i in range(W):
    for j in range(H):
        if S[j][i]=='#':
            ans[j][i+1]+=1
            ans[j][i+2]+=1
            ans[j][i]+=1
            ans[j+2][i+1]+=1
            ans[j+1][i]+=1
            ans[j+1][i+2]+=1
            ans[j+2][i+2]+=1
            ans[j+2][i]+=1
del(ans[-1])
del(ans[0])
for i in range(len(ans)):
    del(ans[i][-1])
    del(ans[i][0])
for i in range(W):
    for j in range(H):
        if ans[j][i]>=inf:
            ans[j][i]='#'
        else:
            ans[j][i]=str(ans[j][i])

for i in range(H):
    print("".join(ans[i]))

Submission status: Accepted

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

for i in range(1,h+1):
  for j in range(1,w+1):
    if s[i][j]=='.':
      t=s[i-1][j-1:j+2]+s[i][j-1:j+2]+s[i+1][j-1:j+2]
      s[i]=s[i][:j]+str(t.count('#'))+s[i][j+1:]
  print(s[i][1:-1])

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
r=[[*input()] for _ in range(n)]
ans=[[[0] for _ in range(m)] for _ in range(n)]
dir=[[0,0,1,1,1,-1,-1,-1],[-1,1,-1,0,1,-1,0,1]]
for i in range(n):
  for j in range(m):
    count=0
    if r[i][j]=='#':
      ans[i][j]='#'
      continue
    for k in range(8):
      dx,dy=i+dir[0][k],j+dir[1][k]
      if 0<=dx<n and 0<=dy<m and r[dx][dy]=='#':
        count+=1
    ans[i][j]=count
[print(*i,sep='') for i in ans]

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
r=[[*input()] for _ in range(n)]
ans=[[[0] for _ in range(m)] for _ in range(n)]
dir1=[0,0,1,1,1,-1,-1,-1]
dir2=[-1,1,-1,0,1,-1,0,1]
for i in range(n):
  for j in range(m):
    count=0
    if r[i][j]=='#':
      ans[i][j]='#'
      continue
    for k in range(8):
      dx = i + dir1[k]
      dy = j + dir2[k]
      if 0<=dx<n and 0<=dy<m and r[dx][dy]=='#':
        count+=1
    ans[i][j]=count
[print(*i,sep='') for i in ans]

Submission status: Accepted

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

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

ans = [[" " for i in range(W)] for j in range(H)]

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

        else:
            a = 0
            if i > 0:
                if S[i-1][j] == "#":
                    a += 1
                    
                if j > 0:
                    if S[i-1][j-1] == "#":
                        a += 1

                if j < W-1:
                    if S[i-1][j+1] == "#":
                        a += 1

            if j > 0:
                if S[i][j-1] == "#":
                    a += 1

            if j < W-1:
                if S[i][j+1] == "#":
                    a += 1

            if i < H-1:
                if S[i+1][j] == "#":
                    a += 1
                    
                if j > 0:
                    if S[i+1][j-1] == "#":
                        a += 1

                if j < W-1:
                    if S[i+1][j+1] == "#":
                        a += 1

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

for i in range(H):
  print("".join(ans[i]))

Submission status: Accepted

Solution code:
import numpy as np
from scipy import signal

H, W =map(int, input().split()) 
S = np.array([list(input()) for _ in range(H)])
K = np.array([[1,1,1],[1,0,1],[1,1,1]])
T = signal.convolve2d(S=="#",K,"same").astype(str)
T[S=="#"]="#"
for t in T:
    print("".join(t))

Submission status: Accepted

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

s = [input() for _ in range(h)]
ans = ['' for _ in range(h)]

for y in range(h):
    for x in range(w):
        if s[y][x] == '.':
            cnt = 0
            for dy in range(-1, 2, 1):
                for dx in range(-1, 2, 1):
                    if 0 <= x+dx < w and 0 <= y+dy < h:
                        if s[y+dy][x+dx] == '#':
                            cnt += 1
            ans[y] += str(cnt)
        else:
            ans[y] += '#'

for t in ans:
    print(t)

Submission status: Accepted

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

ans = []

for h in range(1, H + 1):
    t = ''
    cnt = 0
    for w in range(1, W + 1):
        if S[h - 1][w] == '#':
            cnt += 1
        if S[h + 1][w] == '#':
            cnt += 1
        if S[h][w - 1] == '#':
            cnt += 1
        if S[h][w + 1] == '#':
            cnt += 1
        if S[h - 1][w - 1] == '#':
            cnt += 1
        if S[h + 1][w + 1] == '#':
            cnt += 1
        if S[h + 1][w - 1] == '#':
            cnt += 1
        if S[h - 1][w + 1] == '#':
            cnt += 1

        if S[h][w] == '#':
            t += '#'
        else:
            t += str(cnt)
        cnt = 0
    ans.append(t)

for i in ans:
    print(i)

Submission status: Accepted

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

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

ans = []

for h in range(1, H + 1):
    t = ''
    cnt = 0
    for w in range(1, W + 1):
        if S[h - 1][w] == '#':
            cnt += 1
        if S[h + 1][w] == '#':
            cnt += 1
        if S[h][w - 1] == '#':
            cnt += 1
        if S[h][w + 1] == '#':
            cnt += 1
        if S[h - 1][w - 1] == '#':
            cnt += 1
        if S[h + 1][w + 1] == '#':
            cnt += 1
        if S[h + 1][w - 1] == '#':
            cnt += 1
        if S[h - 1][w + 1] == '#':
            cnt += 1

        if S[h][w] == '#':
            t += '#'
        else:
            t += str(cnt)
        cnt = 0
    ans.append(t)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
#
# abc075 b
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 5
.....
.#.#.
....."""
        output = """11211
1#2#1
11211"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 5
#####
#####
#####"""
        output = """#####
#####
#####"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#..."""
        output = """#####3
#8#7##
####5#
4#65#2
#5##21
#4#310"""
        self.assertIO(input, output)


def resolve():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    A = [[0] * (W+2) for _ in range(H+2)]

    for y in range(H):
        for x in range(W):
            if S[y][x] == "#":
                for i in range(y, y+3):
                    for j in range(x, x+3):
                        A[i][j] += 1

    for y in range(H):
        for x in range(W):
            if S[y][x] == "#":
                print("#", end="")
            else:
                print(A[y+1][x+1], end="")
        print()


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
L=[[] for i in range(H)]
for i in range(H):
  L1=list(input())
  L[i]=L1
L=[["$"]+i+["$"] for i in L]
L=[["$" for i in range(W+2)]]+L+[["$" for i in range(W+2)]]
for i in range(1,H+1):
  for j in range(1,W+1):
    if L[i][j]==".":
      L[i][j]=str([L[i][j+1],L[i-1][j+1],L[i+1][j+1],L[i-1][j-1],L[i+1][j],L[i-1][j],L[i][j-1],L[i+1][j-1]].count("#"))
for i in range(1,H+1):
  print("".join(L[i][1:-1]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0]*w for _ in range(h)]
def dfs(x,y):
    if 0 <= x < h and 0 <= y < w:
        if ans[x][y] != "#":
            ans[x][y] += 1        

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'
            dfs(i+1, j)
            dfs(i+1, j+1)
            dfs(i+1, j-1)
            dfs(i-1, j+1)
            dfs(i-1, j)
            dfs(i-1, j-1)
            dfs(i, j+1)
            dfs(i, j-1)
for i in range(h):
    for j in range(w):
        ans[i][j]=str(ans[i][j])
        print(ans[i][j],end="")
    print('')

Submission status: Accepted

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

d = [-1, 0, 1]
for h in range(H):
    for w in range(W):
        if S[h][w] == '#':
            continue
        else:
            count = 0
            for i in range(3):
                for j in range(3):
                    hs = h + d[i]
                    ws = w + d[j]
                    if 0 <= hs < H and 0 <= ws < W and S[hs][ws] == '#':
                        count += 1
                    else:
                        continue
            S[h][w] = str(count)

ans_row = [''.join(S[h]) for h in range(H)]
print('\n'.join(ans_row))

Submission status: Accepted

Solution code:
H, W = map(int,input().split(' '))
S = []
dj = [-1, -1, -1, 0, 0, 1, 1, 1]
dk = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
  S.append(input())
for j in range(H):
  answer = list(S[j])
  for k in range(W):
    cnt = 0
    if S[j][k] == '#':
      pass
    if S[j][k] == '.':
      for d in range(8):
        if 0 <= j+dj[d] <= H-1:
          if 0 <= k+dk[d] <= W-1:
            if S[j + dj[d]][k+dk[d]] == '#':
              cnt += 1
      answer[k] = str(cnt)
  print(''.join(answer))

Submission status: Accepted

Solution code:
H, W = map(int,input().split(' '))
S = []
for i in range(H):
  S.append(input())
for j in range(H):
  answer = list(S[j])
  for k in range(W):
    cnt = 0
    if S[j][k] == '#':
      pass
    if S[j][k] == '.':
      if j-1 >= 0:
        if k-1 >= 0:
          if S[j-1][k-1] == '#':
            cnt += 1
        if S[j-1][k] == '#':
          cnt += 1
        if k+1 <= W-1:
          if S[j-1][k+1] == '#':
            cnt += 1
      if k-1 >= 0:
        if S[j][k-1] == '#':
          cnt += 1
      if k+1 <= W-1:
        if S[j][k+1] == '#':
          cnt += 1
      if j+1 <= H-1:
        if k-1 >= 0:
          if S[j+1][k-1] == '#':
            cnt += 1
        if S[j+1][k] == '#':
          cnt += 1
        if k+1 <= W-1:
          if S[j+1][k+1] == '#':
            cnt += 1
      answer[k] = str(cnt)
  print(''.join(answer))

Submission status: Accepted

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

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

for i in range(0, h):
    for j in range(0, w):
        if s[i][j] != '.':
            continue

        cnt = 0
        for k in range(0, 8):
            ni = i + dy[k]
            nj = j + dx[k]

            if not (ni < 0 or h <= ni) and not (nj < 0 or w <= nj) and s[ni][nj] == '#':
                cnt += 1

        s[i][j] = str(cnt)

[print(''.join(s[ii])) for ii in range(0, h)]

Submission status: Accepted

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

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

for i in range(0, h):
    for j in range(0, w):
        if s[i][j] != '.':
            continue

        cnt = 0
        for k in range(0, 8):
            ni = i + dy[k]
            nj = j + dx[k]

            if not (ni < 0 or h <= ni) and not (nj < 0 or w <= nj) and s[ni][nj] == '#':
                cnt += 1

        s[i][j] = str(cnt)

[print(''.join(s[ii])) for ii in range(0, h)]

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(input())
ans = [[0] * W for _ in range(H)]
for y in range(H):
    for x in range(W):
        if S[y][x] == "#":
            ans[y][x] = "#"
            if not (y == H-1 or x == W-1):
                if S[y+1][x+1] != "#":
                    ans[y+1][x+1] += 1
            if not y == H - 1:
                if S[y+1][x] != "#":
                    ans[y+1][x] += 1
            if not x == W - 1:
                if S[y][x+1] != "#":
                    ans[y][x+1] += 1
            if not (y == H - 1 or x == 0):
                if S[y+1][x-1] != "#":
                    ans[y+1][x-1] += 1
            if not (y == 0 or x == W - 1):
                if S[y-1][x+1] != "#":
                    ans[y-1][x+1] += 1
            if not x == 0:
                if S[y][x-1] != "#":
                    ans[y][x-1] += 1
            if not y == 0:
                if S[y-1][x] != "#":
                    ans[y-1][x] += 1
            if not (y == 0 or x == 0):
                if S[y-1][x-1] != "#":
                    ans[y-1][x-1] += 1

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

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
ls = []
side = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(h):
  ls.append(list(input()))
for i in range(h):
  ans2 = ""
  for j in range(w):
    ans = 0
    if ls[i][j] == "#":
      ans2 += "#"
    else:
      for k in range(8):
        if i+side[k][0] < 0 or j+side[k][1] < 0 or h <=i+side[k][0] or w <= j+side[k][1]:
          continue
        else:
          ans += (1 if ls[i+side[k][0]][j+side[k][1]] == "#" else 0)
      ans2 += str(ans)
  print(ans2)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
ls = []
yside = {-1,-1,-1,0,0,1,1,1}
xside = {-1,0,1,-1,1,-1,0,1}
side = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
for i in range(h):
  ls.append(list(input()))
for i in range(h):
  ans2 = ""
  for j in range(w):
    ans = 0
    if ls[i][j] == "#":
      ans2 += "#"
    else:
      for k in range(8):
        if i+side[k][0] < 0 or j+side[k][1] < 0 or h <=i+side[k][0] or w <= j+side[k][1]:
          continue
        else:
          ans += (1 if ls[i+side[k][0]][j+side[k][1]] == "#" else 0)
      ans2 += str(ans)
  print(ans2)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = ['.'+input()+'.' if i > 0 and i < H + 1 else '.' * (W+2) for i in range(H+2)]
for i in range(1, H+1):
    s = ''
    for j in range(1, W+1):
        if S[i][j] == '#': s += '#'
        else: s += str((S[i-1][j-1]+S[i-1][j]+S[i-1][j+1]+S[i][j-1]+S[i][j+1]+S[i+1][j-1]+S[i+1][j]+S[i+1][j+1]).count('#'))
    print(s)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
t=[["$"]*(w+2) for i in range(h+2)]
for i in range(h):
    for j in range(w):
        t[i+1][j+1]=s[i][j]
v=[1,0,-1]
for i in range(h):
    for j in range(w):
        cnt=0
        if t[i+1][j+1]==".":
            for k in v:
                for l in v:
                    if t[i+1+k][j+1+l]=="#":
                        cnt+=1
            t[i+1][j+1]=str(cnt)
for i in range(h+2):
    t[i]="".join(t[i])
for i in range(1,h+1):
    print(t[i][1:w+1])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
li=[input() for _ in range(h)]
for i in range(h):
    res=""
    for j in range(w):
        cnt=0
        if li[i][j]==".":
            for x in range(-1,2):
                for y in range(-1,2):
                    if 0<=i+x<h and 0<=j+y<w and li[i+x][j+y]=="#":
                        cnt+=1
            res+=str(cnt)
        else:
            res+="#"
    print(res)

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l=[["."]*(w+2)]
for i in range(h):
    l.append(list("."+input()+"."))
l.append(["."]*(w+2))

tate=[-1,-1,-1,0,0,1,1,1]
yoko=[-1,0,1,-1,1,-1,0,1]

for i in range(1,h+1):
    l2=[]
    for j in range(1,w+1):
        if l[i][j]=="#":
            l2.append("#")
        else:
            cnt=0
            for k in range(8):
                if l[i+tate[k]][j+yoko[k]]=="#":
                    cnt+=1
            l2.append(str(cnt))
    print("".join(l2))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
maze=[]
lst=['.']*(w+2)
maze.append(lst)

for i in range(h):
  lst2=list(input())
  lst2=['.']+lst2+['.']
  maze.append(lst2)

maze.append(lst)


ans=[[None]*w for i in range(h)]

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

for i in range(1,h+1):
  for j in range(1,w+1):
    cnt=0
    if maze[i][j]=='#':
      ans[i-1][j-1]='#'
      continue

    for k in range(8):
      s=i+dx[k]
      t=j+dy[k]
      if maze[s][t]=='#':
        cnt+=1
    ans[i-1][j-1]=str(cnt)
    
for i in range(h):
  answer=''
  for j in range(w):
    answer+=ans[i][j]
  
  print(answer)

Submission status: Accepted

Solution code:
import sys
H,W=map(int,input().split())
S=[]
for i in range(H):
  t=input()
  s=[]
  for j in t:
    if j==".":
      s.append(0)
    else:
      s.append(j)
  S.append(s)
#print(S)
#sys.exit()
for k in range(H):
  for l in range(W):
    if S[k][l]=="#":
      try:
#        sys.stderr.write(str(S[k-1][l-1]))
        S[k-1][l-1]+=1
        if k<=0 or l<=0:
          S[k-1][l-1]-=1
      except:
#        sys.stderr.write("1\n")
        pass
      try:
        S[k-1][l]+=1
        if k<=0:
          S[k-1][l]-=1
      except:
#        sys.stderr.write("2\n")
        pass
      try:
        S[k-1][l+1]+=1
        if k<=0:
          S[k-1][l+1]-=1
      except:
#        sys.stderr.write("3\n")
        pass
      try:
        S[k][l-1]+=1
        if l<=0:
          S[k][l-1]-=1
      except:
#        sys.stderr.write("4\n")
        pass
      try:
        S[k][l+1]+=1
      except:
#        sys.stderr.write("5\n")
        pass
      try:
        S[k+1][l-1]+=1
        if l<=0:
          S[k+1][l-1]-=1
      except:
#        sys.stderr.write("6\n")
        pass
      try:
        S[k+1][l]+=1
      except:
#        sys.stderr.write("7\n")
        pass
      try:
        S[k+1][l+1]+=1
      except:
#        sys.stderr.write("8\n")
        pass
for m in S:
  ans=""
  for n in m:
    ans+=str(n)
  print(ans)

Submission status: Accepted

Solution code:
import numpy as np
H,W = (int(T) for T in input().split())
Map = np.zeros((H+2,W+2),dtype=bool)
for TH in range(0,H):
    Map[TH+1,1:W+1] = [True if T=='#' else False for T in input()] 
Disp = np.full((H,W),'#')
for TH in range(0,H):
    for TW in range(0,W):
        if Map[TH+1,TW+1]==False:
            Disp[TH,TW] = np.sum(Map[TH:TH+3,TW:TW+3])
    print(''.join(Disp[TH,:]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
l = [["#"] for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            l[i].append(0)
        else:
            l[i].append("#")
    l[i].append("#")
l = [["#" for i in range((w+2))]] + l + [["#" for i in range(w+2)]]
pos = []
for i in range(1, h+1):
    for j in range(1, w+1):
        if l[i][j] == "#":
            pos.append([i, j])
for i in pos:
    x, y = i
    for j in [[x-1, y-1], [x, y-1], [x+1, y-1], [x-1, y], [x+1, y], [x-1, y+1], [x, y+1], [x+1, y+1]]:
        if l[j[0]][j[1]] != "#":
            l[j[0]][j[1]] += 1
for i in range(1, h+1):
    print("".join(list(map(str, l[i][1:-1]))))

Submission status: Accepted

Solution code:
def chk(s,x,y):
    cnt = 0
    if s[y][x]!='#':
        for i in [-1,0,1]:
            for j in [-1,0,1]:
                if 0<=x+i<w and 0<=y+j<h:
                    if s[y+j][x+i]=='#':
                        cnt += 1
        return str(cnt)
    else:
        return '#'

h,w = map(int,input().split())
gazo = []
for i in range(h):
    gazo.append(str(input()))

#for i in range(h):
#    print(gazo[i])

for i in range(h):
    for j in range(w):
        print(chk(gazo,j,i),end='')
    print('')

Submission status: Accepted

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

sn = [input() for _ in range(h)]
csn = [[0] * w for i in range(h)]

for i in range(h):
  for j in range(w):
    if sn[i][j] == "#" :
      csn[i][j] = -1
      if i-1 >= 0 and sn[i-1][j] == "." :
        csn[i-1][j] += 1
        
      if i+1 <= h-1 and sn[i+1][j] == "." :
        csn[i+1][j] += 1   
        
      if j-1 >= 0 and sn[i][j-1] == "." :
        csn[i][j-1] += 1
        
      if j+1 <= w-1 and sn[i][j+1] == "." :
        csn[i][j+1] += 1        

      if i-1 >= 0 and j-1 >= 0 and sn[i-1][j-1] == "." :
        csn[i-1][j-1] += 1
 
      if i-1 >= 0 and j+1 <= w-1  and sn[i-1][j+1] == "." :
        csn[i-1][j+1] += 1 

      if i+1 <= h-1 and j-1 >= 0 and sn[i+1][j-1] == "." :
        csn[i+1][j-1] += 1        
        
      if i+1 <= h-1 and j+1 <= w-1 and sn[i+1][j+1] == "." :
        csn[i+1][j+1] += 1    
        
for i in range(h):
  for j in range(w):  
    if csn[i][j] == -1 :
      print("#", end='')
    else :
      print(csn[i][j], end='')
  print()

Submission status: Accepted

Solution code:
h,w = map(int, input().split())
s = ["." + str(input()) + "." for i in range(h)]
s.append("."*(w+2))
s.insert(0,"."*(w+2))
l = []

for h in range(1,h+1):
    s1 = ""
    for j in range(1,w+1):
        if s[h][j] == "#":
            s1 += "#"
        else:
            num = 0
            for x in range(-1,2):
                for y in range(-1,2):
                    if s[h+x][j+y] == "#":
                        num += 1
            s1 += str((num))
    l.append(s1)
for z in l:
    print(z)

Submission status: Accepted

Solution code:

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
moves = ((1, 0), (0, 1), (0, -1), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1,1))
c = [[0]*W for _ in range(H)]
for i in range(H):
  for j in range(W):
    cnt = 0
    if S[i][j] == "#":
      c[i][j] = "#"
      continue
    for dy, dx in moves:
      ny = i + dy
      nx = j + dx
      if 0 <= ny < H and 0 <= nx < W and S[ny][nx] == '#':
        cnt += 1
    c[i][j] = cnt

for i in c:
  i = list(map(str, i))
  print("".join(i))

Submission status: Accepted

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



for h in range(H):
  for w in range(W):

    if S[h][w] == ".":
      cnt = 0

      for h1 in range(max(0,h-1),min(H,h+2)):
        for w1 in range(max(0,w-1),min(W,w+2)):
          if S[h1][w1] =="#":
            cnt += 1
      S[h][w] = cnt
    
for h in range(H):
  print("".join(map(str,S[h])))

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if S[h][w] != "#":
            s = 0

            ist = -1
            ien = 2
            jst = -1
            jen = 2

            if h == 0:
                ist = 0
            if h == H - 1:
                ien = 1
            if w == 0:
                jst = 0
            if w == W - 1:
                jen = 1

            for i in range(ist, ien):
                for j in range(jst, jen):
                    if S[h + i][w + j] == "#":
                        s += 1

            S[h][w] = str(s)

for s in S:
    st = "".join(s)
    print(st)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
data = []
for i in range(H):
  data.append(input())
x = [1,1,1,0,-1,-1,-1,0]
y = [1,0,-1,-1,-1,0,1,1]
ans = []
for i in range(H):
  s = ""
  for j in range(W):
    if data[i][j] == "#":
      s += "#"
      continue
    else:
      count = 0
      for k in range(8):
        if 0 <= i + x[k] and i + x[k] < H and 0 <= j + y[k] and j + y[k] < W:
          if data[i + x[k]][j + y[k]] == "#":
            count += 1
      count = str(count)
      s += count
  ans.append(s)
for i in range(H):
  print(ans[i])