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:
def mine_count(board, h, w, i, j):
    if board[i][j] == '#':
        return '#'
    else:
        count = 0
        for row in range(max(i - 1, 0), min(i + 2, h)):
            for col in range(max(j - 1, 0), min(j + 2, w)):
                if board[row][col] == '#':
                    count += 1
        return str(count)


if __name__ == '__main__':
    h, w = map(int, input().split())
    board = [input() for _ in range(h)]
    for i in range(h):
        print(''.join([mine_count(board, h, w, i, j) for j in range(w)]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = []
for _ in range(h):
    s.append(input())
v = []
for _ in range(h):
    v.append([0] * w)
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            v[i][j] = '#'
        else:
            c = 0
            if i > 0: c += s[i - 1][j] == '#'
            if j > 0: c += s[i][j - 1] == '#'
            if i < h - 1: c += s[i + 1][j] == '#'
            if j < w - 1: c += s[i][j + 1] == '#'
            if i > 0 and j > 0: c += s[i - 1][j - 1] == '#'
            if i > 0 and j < w - 1: c += s[i - 1][j + 1] == '#'
            if i < h - 1 and j > 0: c += s[i + 1][j - 1] == '#'
            if i < h - 1 and j < w - 1: c += s[i + 1][j + 1] == '#'
            v[i][j] = str(c)
    print(''.join(v[i]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s = [input() for _ in range(h)]
ans = []
for i in range(h):
    buf = []
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for r in range(max(0, i - 1), min(h, i + 2)):
                for c in range(max(0, j - 1), min(w, j + 2)):
                    if s[r][c] == '#':
                        cnt += 1
            buf += [cnt]
        else:
            buf += ['#']
    buf = list(map(str, buf))
    ans.append(buf)
for _ in ans:
    print(''.join(_))

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j] == "#":
            ans[i][j] = "#"
            continue
        for k in way:
            if not (0 <= i+k[0] < h and 0 <= j+k[1] < w):
                continue
            if s[i+k[0]][j+k[1]] == "#":
                ans[i][j] += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

H,W=(int(i) for i in input().split())
Field=[]
for i in range(H):
    Field.append(list(input()))
for i in range(0,H):
    for j in range(W):
        if Field[i][j]=="#":continue
        cnt=0
        for x in range(-1,2):
            for y in range(-1,2):
                if (x==0 and y==0) or 0>x+i or 0>y+j or x+i>=H or y+j>=W:
                    continue
                if Field[i+x][j+y]=="#":
                    cnt+=1
        Field[i][j]=str(cnt)

for i in range(H):
    Out=Field[i][0]
    for j in range(1,W):
        Out+=Field[i][j]
    print(Out)

Submission status: Accepted

Solution code:
# ABC 075 B

# H, W = 4, 3
# S = "#.."\
#     "..#"\
#     ".#."\
#     ".#."

H, W = map(int, input().split())
S = ''
for _ in range(H):
    S += input()
    

def cell_of(x, y):
    return S[W*x + y]

def neighbor_indice_of(x, y):
    return [
        (x+m, y+n)
        for m in range(-1, 2)
        for n in range(-1, 2)
        if 0 <= x+m < H and 0 <= y+n < W
    ]

def neighbors_of(x, y):
    return [cell_of(i, j) for i, j in neighbor_indice_of(x, y)]

def mine(x, y):
    cell = cell_of(x, y)
    if cell != '.':
        return cell
    return str(neighbors_of(x, y).count('#'))


for x in range(H):
    print(''.join([mine(x, y) for y in range(W)]))

Submission status: Accepted

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

way = ((1,1),(1,0),(1,-1),(0,1),(0,-1),(-1,1),(-1,0),(-1,-1))
ans = [[0 for j in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'
            continue
        for k in way:
            if not (0 <= i+k[0] < h and 0 <= j+k[1] < w): continue
            if s[i+k[0]][j+k[1]] == '#': ans[i][j] += 1

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

Submission status: Accepted

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


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

result = [[0 for i in range(W)] for j in range(H)]

for h in range(H):
    for w in range(W):
        if a[h][w] == '#':
            result[h][w] = '#'
        else:
            indice = [(h-1, w-1), (h, w-1), (h-1, w+1), (h-1, w),
                      (h+1, w+1), (h, w+1), (h+1, w-1), (h+1, w)]
            for tp in indice:
                if tp[0] >= 0 and tp[0] < H and tp[1] >= 0 and tp[1] < W \
                   and a[tp[0]][tp[1]] == '#':
                    result[h][w] += 1

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

Submission status: Accepted

Solution code:
h,w = (int(i) for i in input().split())
x = [input() for i in range(h)]
for i in range(h):
	answer = ""
	for j in range(w):
		if x[i][j] == ".":
			num,y = 0,[[i-1,j-1],[i-1,j],[i-1,j+1],[i,j-1],[i,j+1],[i+1,j-1],[i+1,j],[i+1,j+1]]
			for k in y:
				if -1<k[0] and k[0]<h and -1<k[1] and k[1]<w:
					if x[k[0]][k[1]] == "#":
						num += 1
			answer += str(num)
		else:
			answer += "#"
	print(answer)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h,w = na()
mp = []
for i in range(h):
    mp.append([_ for _  in ns().strip()])

for i in range(h):
    for j in range(w):
        if mp[i][j] != "#":
            bo = 0
            for k in range(-1,2):
                for l in range(-1,2):
                    ni = i + k
                    nj = j + l
                    if 0 <= ni < h and 0 <= nj < w and mp[ni][nj] == "#":
                        bo += 1
            mp[i][j] = chr(bo + 48)
for i in mp:
    print("".join(i))

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
lis=[input() for i in range(h)]
outs=[]
for x in range(h):
    s=""
    for y in range(w):
        if lis[x][y]==".":
            c=0
            for xi,yi in [(1,1),(1,0),(1,-1),(0,1),(0,-1),(-1,1),(-1,0),(-1,-1)]:
                if not (xi+x==-1 or yi+y==-1 or xi+x==h or yi+y==w):
                    c+=(1 if lis[xi+x][yi+y]=="#" else 0)
            s+=str(c)
        else:
            s+="#"
    outs.append(s)
for i in outs:
    print(i)

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for _ in range(H)]
for i in range(H):
    for j in range(W):
        count = 0
        if S[i][j] != "#":
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if 0<=i+k<=H-1 and 0<=j+l<=W-1:
                        if S[i+k][j+l] == "#":
                            count += 1
            S[i] = S[i].replace(".",str(count),1)
for i in range(H):
    print(S[i])

Submission status: Accepted

Solution code:
H,W=map(int,input().split())
list = []
for i in range(H):
  list.append(input())
result = ['']*H
for j in range(H):
  for k in range(W):
    if list[j][k] == '#':
      result[j] = result[j] + '#'
    else:
      count = 0
      for n in range(-1,2):
        for m in range(-1,2):
          if 0 <= n+j and n+j <=(H-1) and 0<= m+k and m+k <=(W-1):
            if list[n+j][m+k] == '#':
              count += 1
      result[j] = result[j] + str(count)
for x in range(H):
	print(result[x])

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem B


def is_filled_with_all_bombs(s, height, width):
    bomb_count = 0

    for i in range(height):
        bomb_count += s[i][0].count('#')

    return bomb_count == height * width


def replace_dot_with_number(s, height: int, width: int) -> list:
    results = list()
    mod_s = ''

    for i in range(height):
        for j in range(width):
            if s[i][0][j] == '.':
                bomb_count = count_bombs_in_eigth_cells(s, i, j)
                mod_s += str(bomb_count)
            else:
                mod_s += '#'
        results.append(mod_s)
        mod_s = ''

    return results


def count_bombs_in_eigth_cells(s, x: int, y: int) -> int:
    count = 0
    height = len(s)
    width = len(s[0][0])

    for i in range(x - 1, x + 2):
        if (i == x - 1 < 0):
            continue
        if (i == x + 1 > height - 1):
            continue
        for j in range(y - 1, y + 2):
            if (j == y - 1 < 0):
                continue
            if (j == y + 1 > width - 1):
                continue

            if s[i][0][j] == '#':
                count += 1
    return count


def output(results):
    for line in results:
        print(''.join(line))


if __name__ == '__main__':
    height, width = list(map(int, input().split()))
    s = [list(map(str, input().split())) for _ in range(height)]

    if is_filled_with_all_bombs(s, height, width):
        output(s)
    else:
        results = replace_dot_with_number(s, height, width)
        output(results)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

def ncell(i, j):
    DX = [i-1, i, i+1, i-1, i+1, i-1, i, i+1]
    DY = [j-1, j-1, j-1, j, j, j+1, j+1, j+1]
    XX = []
    YY = []
    for k in range(8):
        if (0<=DX[k]<=H-1 and 0<=DY[k]<=W-1):
            XX.append(DX[k]); YY.append(DY[k])
    return [XX, YY]
for i in range(H):
    st = ""
    for j in range(W):
        if S[i][j] == ".":
            dd = ncell(i, j)
            dx = dd[0]; dy = dd[1];
            m = 0
            for k in range(len(dx)):
                if S[dx[k]][dy[k]] == "#":
                    m += 1
            st += str(m)
        else:
            st += "#"
    print(st)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
H, W = map(int, input().split())
A = [list(input()) for _ in range(H)]
ds = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
for i in range(H):
    for j, c in enumerate(A[i]):
        if c == '.':
            cnt = 0
            for d in ds:
                x = j + d[0]
                y = i + d[1]
                if 0 <= x <= W - 1 and 0 <= y <= H - 1 and A[y][x] == '#':
                    cnt += 1
            A[i][j] = str(cnt)

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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [input() for i in range(H)]
offset_h = (-1,-1,-1,0,0,1,1,1)
offset_w = (-1,0,1,-1,1,-1,0,1)
for h in range(H):
  for w in range(W):
    if S[h][w] == "#":
      print("#", end="")
    else:
      bomb = 0
      for i,j in zip(offset_h, offset_w):
        if h+i >= 0 and h+i < H and w+j >=0 and w+j < W and S[h+i][w+j]=="#":
          bomb += 1
            
      print(bomb, end="")
     
  print("")

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# Set 2 dimension list
def dim2input(N):
    li = []
    for _ in range(N):
        li.append(input().split())
    return li

# --------------------------------------------

dp = None

def main():
    H, W = list(map(int, input().split()))

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

    for idx_h in range(H):
        for idx_w in range(W):
            if S[idx_h][idx_w] == "#":
                continue

            bomb = 0

            for dh in range(-1, 2):
                new_h = idx_h + dh
                for dw in range(-1, 2):
                    new_w = idx_w + dw

                    if dh == 0 and dw == 0:
                        continue

                    if 0 <= new_h < H and 0 <= new_w < W and S[new_h][new_w] == "#":
                        bomb += 1

            S[idx_h][idx_w] = bomb

    for row in S:
        for cell in row:
            print(cell, end="")
        print()

main()

Submission status: Accepted

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

x = [-1, -1, 0, 1, 1, 1, 0, -1]
y = [0, 1, 1, 1, 0, -1, -1, -1]
for px in range(h):
    for py in range(w):
        if s[px][py] == '#': continue

        cou = 0
        for d in range(len(x)):
            tempx = px + x[d]
            tempy = py + y[d]

            if tempx < 0 or tempx >= h or tempy < 0 or tempy >= w:
                continue
            if s[tempx][tempy] == '#':
                cou += 1

        s[px] = s[px].replace('.', str(cou), 1)

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

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
a=""
for i in range(w+2):
    a=a+"a"
S=[a]
for i in range(h):
    S.append("a"+input()+"a")
S.append(a)
for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j]==".":
            num=0
            for u in range(-1,2):
                for v in range(-1,2):
                    if S[i+u][j+v]=="#":
                        num+=1
            S[i]=S[i][:j]+str(num)+S[i][j+1:]
for i in range(1,h+1):
    print(S[i][1:w+1])

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
lis=[]
outp=[[0 for i in range(w)] for j in range(h)]
outs=[""]*h
for i in range(h):
    lis.append(input())
for i in range(h):
    for j in range(w):
        if(lis[i][j]=="#"):
            for k in range(-1,2):
                for l in range(-1,2):
                    if(i+k>=0 and i+k<h and j+l>=0 and j+l<w):
                        outp[i+k][j+l]+=1
for i in range(h):
    for j in range(w):
        if(lis[i][j]=="#"):
            outs[i]+="#"
        else:
            outs[i]+=str(outp[i][j])
for i in range(h):
    print(outs[i])

Submission status: Accepted

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


elif W == 1:
    if S[0][0] == "#":
        if S[1][0]!="#": S[1][0]+= 1
    if S[H-1][0]== "#":
        if S[H-2][0]!="#": S[H-2][0]+= 1
    for i in range(1,H-1):
        if S[i][0] =="#":
            if S[i-1][0]!="#": S[i-1][0]+= 1
            if S[i+1][0]!="#": S[i+1][0]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

    for i in range(1,H-1):
        if S[i][W-1] == "#":
            if S[i-1][W-1] !="#": S[i-1][W-1]+= 1
            if S[i+1][W-1] !="#": S[i+1][W-1]+= 1
            if S[i][W-2] !="#": S[i][W-2]+= 1
            if S[i-1][W-2] !="#": S[i-1][W-2]+= 1
            if S[i+1][W-2] !="#": S[i+1][W-2]+= 1

    for i in range(1,W-1):
        if S[0][i] == "#":
            if S[0][i-1] !="#": S[0][i-1]+= 1
            if S[0][i+1] !="#": S[0][i+1]+= 1
            if S[1][i] !="#": S[1][i]+= 1
            if S[1][i-1] !="#": S[1][i-1]+= 1
            if S[1][i+1] !="#": S[1][i+1]+= 1

    for i in range(1,W-1):
        if S[H-1][i] == "#":
            if S[H-1][i-1] !="#": S[H-1][i-1]+= 1
            if S[H-1][i+1] !="#": S[H-1][i+1]+= 1
            if S[H-2][i] !="#": S[H-2][i]+= 1
            if S[H-2][i-1] !="#": S[H-2][i-1]+= 1
            if S[H-2][i+1] !="#": S[H-2][i+1]+= 1

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))
# # def replace(i,j,S):
#     yp = i + 1
#     ym = i - 1
#     xp = j + 1
#     xm = j - 1
#     count = 0 
#     if ym >= 0:
#         if S[ym][j] == "#":
#             count += 1
#         if S[yp][j] == "#":
#             count += 1
#     return S
# # print(S)
# print(S)

Submission status: Accepted

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

dirx = [-1, -1, 0, 1, 1, 1, 0, -1]
diry = [0, 1, 1, 1, 0, -1, -1, -1]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#': continue
        
        bumb_num = 0
        for d in range(len(dirx)):
            tempi = i + dirx[d]
            tempj = j + diry[d]

            if tempi < 0 or tempi >= h or tempj < 0 or tempj >= w: continue 
            if s[tempi][tempj] == '#': bumb_num += 1
        
        s[i] = s[i].replace('.', str(bumb_num), 1)

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

Submission status: Accepted

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

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

for i in range(H):
	for j in range(W):
		cnt=0
		if S[i][j]!="#":
			for x in dx:
				for y in dy:
					if 0<=i+y<H and 0<=j+x<W:
						if S[i+y][j+x]=="#":
							cnt+=1
			S[i][j]=str(cnt)				
		
for s in S:
	print("".join(s))

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        sum = 0
        if(S[i][j] == '#'):
            print('#', end = "")
            continue
        for a in range(-1, 2):
            for b in range(-1, 2):
                if(i + a < 0 or i + a >= H or j + b < 0 or j + b >= W):
                    continue
                if(S[i + a][j + b] == '#'):
                    sum += 1
        print(sum, end = "")
    print()

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
M = [[j if j == "#" else 0 for j in input()] for i in range(H)]

for i in range(H):
    M[i].append(0)
    M[i].insert(0, 0)
HH = [0 for i in range(W+2)]
M.append(HH)
M.insert(0, HH)

for i in range(1, H+1):
    for j in range(1, W+1):
        if M[i][j] == "#":
            if M[i-1][j-1] != "#":
                M[i-1][j-1] += 1
            if M[i-1][j] != "#":    
                M[i-1][j] += 1
            if M[i-1][j+1] != "#":
                M[i-1][j+1] += 1
            if M[i][j-1] != "#":
                M[i][j-1] += 1
            if M[i][j+1] != "#":
                M[i][j+1] += 1
            if M[i+1][j-1] != "#":
                M[i+1][j-1] += 1
            if M[i+1][j] != "#":
                M[i+1][j] += 1
            if M[i+1][j+1] != "#":
                M[i+1][j+1] += 1
for i in range(1, H+1):
    for j in range(1, W+1):
        print(M[i][j], end="")
    print()

Submission status: Accepted

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

    for x in range(h):
        for y in range(w):
            cnt = 0
            if S[x][y] == ".":
                if x != 0:
                    if S[x - 1][y] == "#":
                        cnt += 1
                if y != 0:
                    if S[x][y - 1] == "#":
                        cnt += 1
                if x != h - 1:
                    if S[x + 1][y] == "#":
                        cnt += 1
                if y != w - 1:
                    if S[x][y + 1] == "#":
                        cnt += 1
                if x >= 1 and y >= 1:
                    if S[x - 1][y - 1] == "#":
                        cnt += 1
                if x >= 1 and y < w - 1:
                    if S[x - 1][y + 1] == "#":
                        cnt += 1
                if x < h - 1 and y >= 1:
                    if S[x + 1][y - 1] == "#":
                        cnt += 1
                if x < h - 1 and y < w - 1:
                    if S[x + 1][y + 1] == "#":
                        cnt += 1
                S[x][y] = cnt 
            print(S[x][y], end = "")
        print()



if __name__ == "__main__":
    main()

Submission status: Accepted

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

    for x in range(h):
        for y in range(w):
            cnt = 0
            if S[x][y] == ".":
                if x != 0:
                    if S[x - 1][y] == "#":
                        cnt += 1
                if y != 0:
                    if S[x][y - 1] == "#":
                        cnt += 1
                if x != h - 1:
                    if S[x + 1][y] == "#":
                        cnt += 1
                if y != w - 1:
                    if S[x][y + 1] == "#":
                        cnt += 1
                if x >= 1 and y >= 1:
                    if S[x - 1][y - 1] == "#":
                        cnt += 1
                if x >= 1 and y < w - 1:
                    if S[x - 1][y + 1] == "#":
                        cnt += 1
                if x < h - 1 and y >= 1:
                    if S[x + 1][y - 1] == "#":
                        cnt += 1
                if x < h - 1 and y < w - 1:
                    if S[x + 1][y + 1] == "#":
                        cnt += 1
                S[x][y] = cnt 

    for x in range(h):
        for y in range(w):
            print(S[x][y], end = "")
        print()

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

def ncell(i, j):
    DX = [i-1, i, i+1, i-1, i+1, i-1, i, i+1]
    DY = [j-1, j-1, j-1, j, j, j+1, j+1, j+1]
    XX = []
    YY = []
    for k in range(8):
        if (0<=DX[k]<=H-1 and 0<=DY[k]<=W-1):
            XX.append(DX[k]); YY.append(DY[k])
    return [XX, YY]
for i in range(H):
    st = ""
    for j in range(W):
        if S[i][j] == ".":
            dd = ncell(i, j)
            dx = dd[0]; dy = dd[1];
            m = 0
            for k in range(len(dx)):
                if S[dx[k]][dy[k]] == "#":
                    m += 1
            st += str(m)
        else:
            st += "#"
    print(st)

Submission status: Accepted

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

S=[list(input()) for i in range(H)]

A=[-1,-1,-1,0,0,1,1,1]
B=[-1,0,1,-1,1,-1,0,1]
Ts=""
T=[]
t=0
for i in range(H):
    for j in range(W):

        if S[i][j]=="#":
            Ts+="#"
            continue

        for a,b in zip(A,B):
            ni=i+a
            nj=j+b

            if ni<0 or ni>=H or nj <0 or nj>=W: continue
            if S[ni][nj]=="#": t+=1
        Ts+=str(t)
        t=0

    T.append(Ts)
    Ts=""

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

Submission status: Accepted

Solution code:
h, w = (int(i) for i in input().split())
s = [input() for i in range(h)]
ans = []
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):
        count = 0
        for k in range(len(dx)):
            x = j+dx[k]
            y = i+dy[k]
            if 0<=x and x<w and 0<=y and y<h and s[y][x] == '#':
                count += 1

        if s[i][j] != '#':
            s[i] = s[i].replace('.',str(count),1)
                
for i in range(h):
    print(s[i])

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
S = [list(input()) for i in range(H)]
ans = [[0] * (W + 2) for i in range(H + 2)]
mine = []
for i in range(H):
	for j in range(W):
		if S[i][j] != "#":
			continue
		mine.append((i, j))
		for v in range(3):
			for h in range(3):
				ans[i + v][j + h] += 1

for i, j in mine:
	ans[i + 1][j + 1] = "#"
for l in ans[1: H + 1]:
	print("".join(map(str, l[1: W + 1])))

Submission status: Accepted

Solution code:

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

d = {}
for i in range(h):
    for j in range(w):
        d[(i+1, j+1)] = xs[i][j]


def neighbore(d, coor):
    i, j = coor
    mine = 0
    for di in range(-1, 2):
        for dj in range(-1, 2):
            if di == 0 and dj == 0:
                continue
            if d.get((i+di, j+dj), ".") == "#":
                mine  += 1
    return str(mine)


for i in range(1, h+1):
    line = []
    for j in range(1, w+1):
        if d[i, j] == ".":
            line.append(neighbore(d, (i, j)))
        elif d[(i,j)] == "#":
            line.append("#")
        else:
            assert(False)
    print("".join(line))

Submission status: Accepted

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

for _ in range(h):
    arr.append(list('.'+input()+'.'))
arr.append(['.']*(w+2))

ans = []

for i in range(h):
    ans.append([])
    for j in range(w):
        cnt=0
        if arr[i+1][j+1]=='#':
            cnt='#'
        else:
            for hh in range(3):
                for ww in range(3):
                    if not(hh==1 and ww==1) and arr[i+hh][j+ww]=='#':
                        cnt+=1
            cnt=str(cnt)
        ans[i].append(cnt)
for line in ans:
    print(''.join(line))

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
m = []
m.append(list("." * (w + 2)))
for _ in range(h):
    m.append(["."] + list(input()) + ["."])
m.append(list("." * (w + 2)))
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if m[i][j] == ".":
            c = 0
            if m[i-1][j-1] == "#": c += 1
            if m[i-1][j]   == "#": c += 1
            if m[i-1][j+1] == "#": c += 1
            if m[i][j-1]   == "#": c += 1
            if m[i][j]     == "#": c += 1
            if m[i][j+1]   == "#": c += 1
            if m[i+1][j-1] == "#": c += 1
            if m[i+1][j]   == "#": c += 1
            if m[i+1][j+1] == "#": c += 1
            print(str(c), end="")
        else:
            print("#", end="")
    print()

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

Submission status: Accepted

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

for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            continue
        c=0
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                nx=i+dx
                ny=j+dy
                if nx>=0 and nx<h and ny>=0 and ny<w:
                    if s[nx][ny]=='#':
                        c+=1
        ans[i][j]=str(c)
for i in range(h):
    print(''.join(ans[i]))

Submission status: Accepted

Solution code:

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

S = [input() for _ in range(h)]

for i in range(h):
    s = ''
    for j in range(w):
        if S[i][j] == '#':
            s += S[i][j]
        else:
            x = 0
            for dx in [-1, 0, 1]:
                for dy in [-1, 0, 1]:
                    ii = i + dy
                    jj = j + dx
                    if 0 <= ii < h and 0 <= jj < w and S[ii][jj] == '#':
                        x += 1
            s += str(x)
    print(s)

Submission status: Accepted

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

#for i in s:
#    print(i)

for i in range(h):
    for j in range(w):
        if(s[i][j]=='#'):
            continue
        c=0
        for di in range(-1,2):
            if(i+di<0 or i+di>=h):
                continue
            for dj in range(-1,2):
                if(j+dj<0 or j+dj>=w):
                    continue
                if(s[i+di][j+dj]=='#'):
                    c+=1
        s[i][j]=c

for i in s:
    i=map(str,i)
    si="".join(i)
    print(si)