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:
import numpy as np

def check_mass(i,j):
  c=0
  if i>0:
    if j>0:
      if mass[i-1][j-1]=="#":
        c+=1
    if mass[i-1][j]=="#":
        c+=1
    if j<W-1:
      if mass[i-1][j+1]=="#":
        c+=1
  if True:
      if j>0:
        if mass[i][j-1]=="#":
          c+=1
      if mass[i][j]=="#":
          c+=1
      if j<W-1:
        if mass[i][j+1]=="#":
          c+=1
  if i<H-1:
      if j>0:
        if mass[i+1][j-1]=="#":
          c+=1
      if mass[i+1][j]=="#":
          c+=1
      if j<W-1:
        if mass[i+1][j+1]=="#":
          c+=1
  return c

H,W= list(map(int, input().split()))
mass= []
for i in range(H):
  s = list(input()) # stringのままだと、replaceが出来ないのでlistに変換
  mass.append(s)

# print(mass)

for i in range(H):
  for j in range(W):
    if mass[i][j] == ".":
      c=check_mass(i,j)
      mass[i][j] = str(c)

for i in range(H):
  for j in range(W):
    print(mass[i][j], end="")
  print()

Submission status: Accepted

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

ans_field = [['']*(w+2) for i in range(h+2)]

for i in range(1,w+1):
    for j in range(1,h+1):
        if field[j-1][i-1] == '#':
            ans_field[j][i] = '#'




for i in range(1,w+1):
    for j in range(1,h+1):
        if ans_field[j][i] == '#':
            continue
        c = 0
        for k in range(3):
            if ans_field[j-1][i-1+k] == '#':
                c += 1
        
        if  ans_field[j][i-1] == '#':
            c += 1
        
        if  ans_field[j][i+1] == '#':
            c += 1
        
        for k in range(3):
            if ans_field[j+1][i-1+k] == '#':
                c += 1
        
        ans_field[j][i] = str(c)

for i in range(1,h+1):
    s = ''
    for j in range(1,w+1):
        s += ans_field[i][j]
    
    print(s)

Submission status: Accepted

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

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)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            li = []
            for k in range(-1,2):
                li.append((i-1, j+k))
            li.append((i, j-1))
            li.append((i, j+1))
            for k in range(-1, 2):
                li.append((i+1, j+k))
            for k in li:
                if 0 <= k[0] < h and 0 <= k[1] < w:
                    ans[k[0]][k[1]] += 1

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

for i in range(h):
    for j in range(w):
        print(ans[i][j], end = '')
    print()

Submission status: Accepted

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

def count(a,b,H,W):
  if S[a][b]=="#":
    return "#"
  else:
    cnt=0
    if a>0 and b>0:
      if S[a-1][b-1]=="#": cnt +=1     
    if a>0 and b+1<W:
      if S[a-1][b+1]=="#": cnt +=1     
    if a+1<H and b>0:
      if S[a+1][b-1]=="#": cnt +=1     
    if a+1<H and b+1<W:
      if S[a+1][b+1]=="#": cnt +=1     

    if a>0:  
      if S[a-1][b]=="#": cnt +=1
    if a+1<H:
      if S[a+1][b]=="#": cnt +=1
    if b>0:
      if S[a][b-1]=="#": cnt +=1
    if b+1<W:
      if S[a][b+1]=="#": cnt +=1
    return str(cnt)
    

for j in range(H):
  for k in range(W):
    T[j][k]=count(j,k,H,W)
    
for r in range(H):
  print("".join(T[r]))

Submission status: Accepted

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

A = [''] * H

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

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

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

Submission status: Accepted

Solution code:
import sys
from math import sqrt
from collections import Counter

input = sys.stdin.readline


def I():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    h, w = MI()
    s = [["."] + list(input().rstrip()) + ["."] for _ in range(h)]

    empty = ["."] * (w + 2)
    s.append(empty)
    s.insert(0, empty)
    for i in range(1, h + 1):
        for j in range(1, w + 1):
            if s[i][j] == "#":
                continue
            else:
                bomb = 0
                for row in [-1, 0, 1]:
                    for col in [-1, 0, 1]:
                        if s[i + row][j + col] == "#":
                            bomb += 1
                s[i][j] = str(bomb)
        print("".join(s[i][1 : w + 1]))

    pass


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools

H, W = map(int, input().split())
S = []
ans = []
for _ in range(H):
    S.append(list(input()))
    ans.append(['#' for i in range(W)])

op =[]
for i in itertools.product([-1, 0, 1], repeat=2):
    op.append(i)


for i, s1 in enumerate(S):
    for j, s2 in enumerate(s1):
        if(s2 == '.'):
            counter = 0
            for o in op:
                if(i == 0 and o[0] == -1):
                    continue
                elif(i == H-1 and o[0] == 1):
                    continue
                elif(j == 0 and o[1] == -1):
                    continue
                elif(j == W-1 and o[1] == 1):
                    continue
                else:
                    if(S[i + o[0]][j + o[1]] == "#"):
                        counter += 1
            ans[i][j] = str(counter)

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

Submission status: Accepted

Solution code:
def minesweep(s,h,w):
    
    List = [s[h-1][w-1]
            ,s[h-1][w]
            ,s[h-1][w+1]
            ,s[h][w-1]
            ,s[h][w+1]
            ,s[h+1][w-1]
            ,s[h+1][w]
            ,s[h+1][w+1]]
    
    count = 0
    
    for i in range(8):
        if List[i]=="#":
            count+=1
            
    print(count,end="")
    
H,W = map(int,input().split(" "))

s  = [""]*(H+2)
s[0] = "0"*(W+2)
for i in range(1,H+1):
    s[i]="0"+input()+"0"
s[-1]="0"*(W+2)

for i in range(1,H+1):
    for j in range(1,W+1):
        if s[i][j]=="#":
            print("#",end="")
        else:
            minesweep(s,i,j)
    print("\n",end="")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
l =[]
q =[]
n=[]
l.append("."*(w+2))
for i in range(h):
  l.append("."+ input()+".")
l.append("."*(w+2))
for i in range(1,h+1):
  for t in range(1,w+1):
    if l[i][t]==".":
      q.append(l[i][t-1])
      q.append(l[i][t+1])
      q.append(l[i-1][t-1])
      q.append(l[i-1][t])
      q.append(l[i-1][t+1])
      q.append(l[i+1][t-1])
      q.append(l[i+1][t])
      q.append(l[i+1][t+1])
      z = q.count("#")
      n.append(z)
      q = []
    else:
      n.append("#")
for t in range(0,h):
  c = n[w*t:w*(t+1)]
  c = map(str,c)
  print(''.join(c))

Submission status: Accepted

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

for i in range(1, H+1):
    for j in range(1, W+1):
        if S[i][j] != '#':
            tmp = S[i-1][j-1]=='#'
            tmp += S[i][j-1]=='#'
            tmp += S[i+1][j-1]=='#'
            tmp += S[i+1][j]=='#'
            tmp += S[i+1][j+1]=='#'
            tmp += S[i][j+1]=='#'
            tmp += S[i-1][j+1]=='#'
            tmp += S[i-1][j]=='#'
            tmp = str(tmp)
        else:
            tmp = '#'
        ans[i-1][j-1] = tmp
        
for i in ans:
    print(*i, sep='')

Submission status: Accepted

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

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

for i in range(h):
    print("".join(map(str, s[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, acos, atan, asin
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
def input(): return sys.stdin.readline().strip()
def STR(): return input()
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 = sys.maxsize
mod = 10 ** 9 + 7

h, w = MAP()
s = [STR() for _ in range(h)]
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            ans = 0
            for k in range(8):
                nx = j + dx[k]
                ny = i + dy[k]
                if 0 <= nx < w and 0 <= ny < h and s[ny][nx] == '#':
                    ans += 1
            print(ans, end='')
        else:
            print('#', end='')
    print('')

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
s = [list(input()) for _ in range(H)]
a = [[0] * (W + 2) for _ in range(H + 2)]
for i in range(H):
    for j in range(W):
        tmp_i = i + 1
        tmp_j = j + 1

        if s[i][j] == "#":
            a[tmp_i + 1][tmp_j] += 1
            a[tmp_i - 1][tmp_j] += 1
            a[tmp_i][tmp_j + 1] += 1
            a[tmp_i][tmp_j - 1] += 1
            a[tmp_i + 1][tmp_j + 1] += 1
            a[tmp_i + 1][tmp_j - 1] += 1
            a[tmp_i - 1][tmp_j + 1] += 1
            a[tmp_i - 1][tmp_j - 1] += 1
for i in range(H):
    for j in range(W):
        tmp_i = i + 1
        tmp_j = j + 1
        if s[i][j] == "#":
            a[tmp_i][tmp_j] = "#"
ans = []
for i in range(1,H+1):
    for j in range(1,W+1):
        ans.append(str(a[i][j]))
    print("".join(ans))
    ans = []

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 dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i + dy < h or not 0 <= j + dx < w:
                    continue
                if S[i + dy][j + dx] == '#':
                    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())
lists = [list(map(str, input())) for i in range(H)]

# 下、右、上、左、右下、右上、左下、左上
dh = [1, 0, -1, 0, 1, -1, 1, -1]
dw = [0, 1, 0, -1, 1, 1, -1, -1]

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

        count = 0

        for k in range(8):
            nh = i + dh[k]
            nw = j + dw[k]

            if nh < 0 or H <= nh or nw < 0 or W <=nw:
                continue

            if lists[nh][nw] == "#":
                count += 1

        lists[i][j] = str(count)

for i in range(H):
    print("".join(lists[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)]
#print(ans)
for i in range(h):
    for j in range(w):
        if s[i][j]=='#':
            ans[i][j]='#'
        else:
            cnt=0
            for ii in [-1,0,1]:
                for jj in [-1,0,1]:
                    def chk(a,b):
                        if a>=0 and a<h and b>=0 and b<w:
                            return True
                        else:
                            return False
                    if chk(i+ii,j+jj):
                        if s[i+ii][j+jj]=='#':
                            cnt+=1
            ans[i][j]=cnt
for i in range(h):
    print(''.join(list(map(str,ans[i]))))

Submission status: Accepted

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

copyS=copy.deepcopy(s)


def check(y,x,s):
	ans=0
	for i in range(3):
		for j in range(3):
			X=x+i-1
			Y=y+j-1
			if 0<=X<w and 0<=Y<h:
				if s[Y][X]=="#":
					ans+=1
	return ans

for i in range(h):
	for j in range(w):
		if s[i][j]==".":
			copyS[i][j]=str(check(i,j,s))

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(H: int, W: int, S: "List[str]"):

    def isbomb(i: int, j: int):
        return 0 <= i < H and 0 <= j < W and S[i][j] == "#"

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

    for i, Si in enumerate(S):
        print("".join(
            c if c == "#" else
            str(sum(
                isbomb(i + di, j + dj)
                for di, dj in ds
            ))
            for j, c in enumerate(Si)
        ))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    S = [next(tokens) for _ in range(H)]  # type: "List[str]"
    solve(H, W, S)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

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

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

    def put(s, i, j):
        if 0 <= i-1:
            s[i-1][j] += 1
        if 0 <= i-1 and 0 <= j-1:
            s[i-1][j-1] += 1
        if 0 <= j-1:
            s[i][j-1] += 1
        if i+1 < h:
            s[i+1][j] += 1
        if i+1 < h and j+1 < w:
            s[i+1][j+1] += 1
        if j+1 < w:
            s[i][j+1] += 1
        if 0 <= i-1 and j+1 < w:
            s[i-1][j+1] += 1
        if 0 <= j-1 and i+1 < h:
            s[i+1][j-1] += 1
        return s
    
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans = put(ans, i, j)
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                ans[i][j] = '#'
    
    ans_str = ''
    for i in range(h):
        for j in range(w):
            ans_str += str(ans[i][j])
        ans_str += '\n'
    ans_str = ans_str[:-1]
    print(ans_str)
    



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)


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

Submission status: Accepted

Solution code:
h, w = [int(x) for x in input().split()]
matrix = [list(input()) for _ in range(h)]
surrounding_distances = (-1, 0, 1)


def count_bombs(i, j):
    for da in surrounding_distances:
        for db in surrounding_distances:
            if da == db == 0:
                # Not surrounidings.
                continue
            si = i + da
            sj = j + db
            if si < 0 or si >= h or sj < 0 or sj >= w:
                continue
            if matrix[si][sj] == '#':
                yield 1


for i in range(h):
    for j in range(w):
        if matrix[i][j] == '.':
            matrix[i][j] = str(sum(count_bombs(i, j)))

[print(''.join(row)) for row in matrix]

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        count=0
        if S[h][w]=='#':
            continue
        # left_up
        if w!=0 and h!=0 and S[h-1][w-1]=='#':
            count+=1
        # Left
        if w!=0 and S[h][w-1]=='#':
            count+=1
        # Left_down
        if w!=0 and h!=H-1 and S[h+1][w-1]=='#':
            count+=1
        # Up
        if h!=0 and S[h-1][w]=='#':
            count+=1
        # Right_up
        if h!=0 and w!=W-1 and S[h-1][w+1]=='#':
            count+=1
        # Right
        if w!=W-1 and S[h][w+1]=='#':
            count+=1
        # Right_down
        if w!=W-1 and h!=H-1 and S[h+1][w+1]=='#':
            count+=1
        # Down
        if h!=H-1 and S[h+1][w]=='#':
            count+=1
        
        S[h][w]=str(count)

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

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        count=0
        if S[h][w]=='#':
            continue
        # left up
        if w==0 or h==0:
            pass
        elif S[h-1][w-1]=='#':
            count+=1

        # Left
        if w==0:
            pass
        elif S[h][w-1]=='#':
            count+=1

        # Left down
        if w==0 or h==H-1:
            pass
        elif S[h+1][w-1]=='#':
            count+=1

        # Up
        if h==0:
            pass
        elif S[h-1][w]=='#':
            count+=1

        # Right up
        if h==0 or w==W-1:
            pass
        elif S[h-1][w+1]=='#':
            count+=1

        # Right
        if w==W-1:
            pass
        elif S[h][w+1]=='#':
            count+=1

        # Right down
        if w==W-1 or h==H-1:
            pass
        elif S[h+1][w+1]=='#':
            count+=1

        # Down
        if h==H-1:
            pass
        elif S[h+1][w]=='#':
            count+=1
        
        S[h][w]=str(count)

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

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 x in [-1, 0, 1]:
      for y in [-1, 0, 1]:
        if not 0 <= i+y < h or not 0 <= j+x < w:
          continue
        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 = list(map(int, input().split()))
rows = []
for i in range(H):
    rows.append(input())

output_rows = [['#' for _ in range(W)] for __ in range(H)]

for i in range(H):
    for j in range(W):
        if rows[i][j] == '#':
            continue
        else:
            total = 0
            for dy in range(-1, 1 + 1):
                for dx in range(-1, 1 + 1):

                    y, x = i + dy, j + dx
                    if x < 0 or x > W - 1 or y < 0 or y > H - 1:
                        continue
                    else:
                        if rows[y][x] == '#':
                            total += 1
            output_rows[i][j] = str(total)

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

Submission status: Accepted

Solution code:
#8方向走査
#下、右、上、左、右下、右上、左上、左下
dy=[1,0,-1,0,1,-1,-1,1]
dx=[0,1,0,-1,1,1,-1,-1]

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

#文字列→作成後変更不可
#文字列をリスト化し変更可にする
field=[]
for _ in range(H):
  field.append(list(input()))

for y in range(H):
  for x in range(W):
    if field[y][x]=="#":
      continue
    count=0
    for k in range(8):
      tmp_y=y+dy[k]
      tmp_x=x+dx[k]

      if tmp_y<0 or tmp_y>=H or tmp_x<0 or tmp_x>=W:
        continue
      if field[tmp_y][tmp_x]=="#":
        count+=1
    field[y][x]=str(count)

#"".join(list)→listの各要素を区切り文字なしで結合して出力
#" ".join(list)→listの各要素を半角スペースで結合して出力
for y in range(H):
  print("".join(field[y]))

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 k in [-1,0,1]:
            if i+k<0 or h<=i+k:
                continue
            for l in [-1,0,1]:
                if j+l<0 or j+l>=w:
                    continue
                cnt+=1*(s[i+k][j+l]=='#')
        s[i][j]=str(cnt)
for i in range(h):
    print("".join(s[i]))

Submission status: Accepted

Solution code:
H, W = map(int, input().split(' '))
masu = [input() for i in range(H)]
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, -1, -1, -1, 0, 1, 1, 1]
ans = []
for i in range(H):
    a = []
    for j in range(W):
        if (masu[i][j] == '#'):
            a.append('#')
            continue
        cnt = 0
        for d in range(len(dx)):
            x = i + dx[d]
            y = j + dy[d]
            if (x < 0 or H <= x):
                continue
            if (y < 0 or W <= y):
                continue
            if (masu[x][y] == '#'):
                cnt += 1
        a.append(str(cnt))
    ans.append(a)
for b in ans:
    print("".join(b))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
ans = [[0]*w for i in range(h)]
for i in range(h):
         for j in range(w):
                  if s[i][j] == "#":
                           for k in range(max(0,i-1),min(h,(i+1)+1)):
                                    for l in range(max(0,j-1),min(w,(j+1)+1)):
                                             ans[k][l] += 1
for i in range(h):
         for j in range(w):
                  if s[i][j] == "#":
                           ans [i][j] = "#"
for i in range(h):
         answer = list(map(str,ans[i]))
         answer=''.join(answer)
         print(answer)

Submission status: Accepted

Solution code:
tate, yoko = map(int,input().split())
map_list = []
for t in range(tate):
    map_list.append(list(input()))

new_map_list = [[0] * (yoko + 1) for i in range(tate + 1)]

def outofrange_check(t, y):
    if t >= tate or y >= yoko:
        return True


for t in range(tate):
    for y in range(yoko):
        if map_list[t][y] == '#':
            for t_2 in range(t-1, t+2):
                for y_2 in range(y-1, y+2):
                    new_map_list[t_2][y_2] += 1


for t in range(tate):
    for y in range(yoko):
        if map_list[t][y] == '#':
            new_map_list[t][y] = '#'

for i in range(tate):
    for j in range(yoko):
        print(new_map_list[i][j], end='')
    print()

Submission status: Accepted

Solution code:
h, n = (int(x) for x in input().split())
dp = [[0 for i in range(n+1)] for j in range(h+1)]
for i in range(h):
    t=input()
    t=list(t)
    for j in range(n):
        if t[j] == '#':
            dp[i][j] = '#'

for i in range(h):
    for j in range(n):
        if dp[i][j] == '#':
            if dp[i-1][j-1] != '#':
                dp[i-1][j-1]+=1
            if dp[i][j-1] != '#':
                dp[i][j-1]+=1
            if dp[i+1][j-1] != '#':
                dp[i+1][j-1]+=1
            if dp[i-1][j] != '#':
                dp[i-1][j]+=1
            if dp[i-1][j+1] != '#':
                dp[i-1][j+1]+=1
            if dp[i+1][j] != '#':
                dp[i+1][j]+=1
            if dp[i][j+1] != '#':
                dp[i][j+1]+=1
            if dp[i+1][j+1] != '#':
                dp[i+1][j+1]+=1

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

Submission status: Accepted

Solution code:
def masucnt(masu,i,j):
  if i<0 or j<0:
    return 0
  s="."
  try:
    s=masu[i][j]
  except:
    s=""
  return 1 if s=="#" else 0

h,w=map(int,input().split())
masu=[""]*h

for i in range(h):
  masu[i]=input().strip()

for i in range(h):
  for j in range(len(masu[i])):
    n=0
    s=masu[i][j]
    if s=="#":
      if j==(len(masu[i])-1):
        print("#")
      else:
        print("#",end="")
    else:
      n+=masucnt(masu,i-1,j-1)
      n+=masucnt(masu,i-1,j)
      n+=masucnt(masu,i-1,j+1)
      n+=masucnt(masu,i,j-1)
      n+=masucnt(masu,i,j+1)
      n+=masucnt(masu,i+1,j-1)
      n+=masucnt(masu,i+1,j)
      n+=masucnt(masu,i+1,j+1)
      if j==(len(masu[i])-1):
        print(n)
      else:
        print(n,end="")

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
#mine = [[0 for i in range(w)] for i in range(h)]
mine = []
for i in range(h):
    mine.append(list(input().replace(".","0")))

for i in range(h):
    for j in range(w):
        if mine[i][j] == "#":
            for k in range(i-1,i+2):
                if k > (h-1) or k < 0:
                    continue
                for l in range(j-1,j+2):
                    if l > (w-1) or l < 0:
                        continue
                    elif mine[i][j] == mine[k][l]:
                        continue
                    mine[k][l] = int(mine[k][l])
                    mine[k][l] += 1
for i in mine:
    print("".join(map(str,i)))

Submission status: Accepted

Solution code:

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

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

def prt_bomb(a, b):
    if(s[a][b] == '#'):
        mine_s[a][b] = '#'
    else:
        ret = 0
        for i in range(max(0, a-1), min(a+1, h-1)+1):
            for j in range(max(0, b-1), min(b+1, w-1)+1):
                if(s[i][j] == '#'):
                    ret += 1
        mine_s[a][b] = str(ret)

for i in range(h):
    for j in range(w):
        prt_bomb(i, j)

for i in range(h):
    print(''.join(mine_s[i]))

Submission status: Accepted

Solution code:
# from math import factorial,sqrt
# from itertools import permutations as permus
# from fractions import gcd
# from collections import deque,Counter
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

import numpy as np
# import scipy as scp

# 盤面の受け取り
h,w = map(int,input().split())
board = [[] for _ in range(h)]
for i in range(h):
    board[i] = list(input())

moves = [(-1,-1),(-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0),(1,1)]
for ih in range(h):
    for iw in range(w):
        # print(ih,iw)
        if board[ih][iw] == "#":
            continue
        else:
            board[ih][iw] = 0
            for move in moves:
                mh = ih+move[0]
                mw = iw+move[1]
                if mh>=0 and mh<h and mw>=0 and mw<w:
                    if board[mh][mw] == "#":
                        board[ih][iw] += 1
                    else:
                        continue
                else:
                    continue
            continue

for row in board:
    print("".join(map(str,row)))

# print(ans)
# print("{:.10f}".format(ans))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
s0 = ["."] * (w + 2)
s = [["."] + list(input()) + ["."] for _ in range(h)]
s.insert(0, s0)
s.append(s0)
v = [[-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0]]
ans = []
for i in range(1, h + 1):
    a = []
    for j in range(1, w + 1):
        c = 0
        if s[i][j] == ".":
            for u in v:
                if s[i + u[0]][j + u[1]] == "#":
                    c += 1
            a.append(str(c))
        else:
            a.append("#")
    ans.append(a)
for b in ans:
    print("".join(b))

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s0 = "."*(w+2)
S = [s0]

for i in range(h):
    S.append("."+input()+".")
    
S.append(s0)

for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j] == ".":
            num = 0
            for k in range(3):
                if S[i-1][j-1+k] == "#":
                    num += 1
            if S[i][j-1] == "#":
                num += 1
            if S[i][j+1] == "#":
                num += 1
            for k in range(3):
                if S[i+1][j-1+k] == "#":
                    num += 1
            S[i] = list(S[i])
            S[i][j] = str(num)
            S[i] = "".join(S[i])
            
for i in range(1,h+1):
    print(S[i][1:w+1])

Submission status: Accepted

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



move=[[1,0],[0,1],[-1,0],[0,-1],[-1,1],[-1,-1],[1,1],[1,-1]]
for i in range(h):
    for j in range(w):
        if maze[i][j]=="#":
            continue
        else:
            cnt=0
            for dy,dx in move:
                y=i+dy
                x=j+dx
                if (0<=y<h and 0<=x<w)==False:
                    continue
                else:
                    if maze[y][x]=="#":
                        cnt+=1
            maze[i][j]=str(cnt)
for i in range(h):
    ans=""
    for j in range(w):
        ans+=maze[i][j]
    print(ans)