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: Wrong Answer

Solution code:
def main():
    H, W = map(int, raw_input().split(" "))
    matrix = []
    for _ in range(H):
        matrix.append(list(raw_input()))

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

            num_bombs = 0
            if i - 1 >= 0 and j - 1 >= 0 and matrix[i - 1][j - 1] == "#":
                num_bombs += 1
            if j - 1 >= 0 and matrix[i][j - 1] == "#":
                num_bombs += 1
            if i - 1 >= 0 and matrix[i - 1][j] == "#":
                num_bombs += 1
            if i + 1 < H and j + 1 < W and matrix[i + 1][j + 1] == "#":
                num_bombs += 1
            if j + 1 < W and matrix[i][j + 1] == "#":
                num_bombs += 1
            if i + 1 < H and matrix[i + 1][j] == "#":
                num_bombs += 1
            if i + 1 >= 0 and j + 1 < W and matrix[i - 1][j + 1] == "#":
                num_bombs += 1
            if i + 1 < H and j + 1 >= 0 and matrix[i + 1][j - 1] == "#":
                num_bombs += 1

            if j == W - 1:
                print num_bombs
            else:
                print num_bombs,

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i in range(h):
  for j in range(w):
    if F[i][j] == ".":
      u = max(i-1,0)
      d = min(i+2,h)
      l = max(j-1,0)
      r = min(j+2,w)
      n = F[u:d][l:r].count(".")
      F[i][j] = str(n)

for line in F:
  print("".join(line))

Submission status: Wrong Answer

Solution code:
def main():
    H, W = map(int, raw_input().split(" "))
    matrix = []
    for _ in range(H):
        matrix.append(list(raw_input()))

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

            num_bombs = 0
            if i - 1 >= 0 and j - 1 >= 0 and matrix[i - 1][j - 1] == "#":
                num_bombs += 1
            if j - 1 >= 0 and matrix[i][j - 1] == "#":
                num_bombs += 1
            if i - 1 >= 0 and matrix[i - 1][j] == "#":
                num_bombs += 1
            if i + 1 < H and j + 1 < W and matrix[i + 1][j + 1] == "#":
                num_bombs += 1
            if j + 1 < W and matrix[i][j + 1] == "#":
                num_bombs += 1
            if i + 1 < H and matrix[i + 1][j] == "#":
                num_bombs += 1
            if i + 1 >= 0 and j + 1 < W and matrix[i - 1][j + 1] == "#":
                num_bombs += 1
            if i + 1 < H and j + 1 >= 0 and matrix[i + 1][j - 1] == "#":
                num_bombs += 1

            print num_bombs,
        print ""

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#https://beta.atcoder.jp/contests/abc075
# -*- coding: utf-8 -*-
h,w = map(int,input().split())
bomb = []
for _ in range(h):
    n = list(list(input()))
    print(n)
    bomb.append(n)

for y in range(h):
    for x in range(w):
        if bomb[y][x] == '.':
            counter = 0
            for yy in list([-1,0,1]):
                if y == 0 and yy == -1: continue
                if y == h-1 and yy == 1: continue
                for xx in list([-1,0,1]):
                    if x == 0 and xx == -1: continue
                    if x == w-1 and xx == 1: continue 
                    try:
                        if bomb[y+yy][x+xx]=='#': counter += 1
                    except IndexError:
                        counter += 0
            bomb[y][x] = counter

for y in range(h):
    string = ''
    for x in range(w): string += str(bomb[y][x])
    print(string)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
print = sys.stdout.write

def checkmine(matrix,r,c):
    if matrix[r][c] == "#":
        return "#"

    dr = [-1,-1,-1,0,0,1,1,1]
    dc = [-1,0,1,-1,1,-1,0,1]
    
    numberOfBombs = 0
    for i in range(8):
        #if r + dr[i] > -1 and r + dr[i] < len(matrix) and c + dc[i] > -1 and c + dc[i] < len(matrix[0]):
        try:
            if matrix[r+dr[i]][c+dc[i]] == "#":
                numberOfBombs += 1
        except:
            pass
    return numberOfBombs

N,M = map(int,input().split())
l = []
for i in range(N):
    l.append(input())
    
for i in range(N):
    for j in range(M):
        print(str(checkmine(l,i,j)))
    print("\n")

Submission status: Wrong Answer

Solution code:
#https://beta.atcoder.jp/contests/abc075
# -*- coding: utf-8 -*-
h,w = map(int,input().split())
bomb = []
for _ in range(h):
    bomb.append(list(list(input())))

for y in range(h):
    for x in range(w):
        if bomb[y][x] == '.':
            counter = 0
            for yy in list([-1,0,1]):
                for xx in list([-1,0,1]):
                    try:
                        if bomb[y+yy][x+xx]=='#': counter += 1
                    except IndexError:
                        counter += 0
            bomb[y][x] = counter

for y in range(h):
    string = ''
    for x in range(w): string += str(bomb[y][x])
    print(string)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
h,w=[int(x) for x in input().split()]
s=[""]*h
for i in range(h):
    s[i]=list(input())
from itertools import product
for i,j in product(range(h),range(w)):
    if s[i][j]=="#":
        continue
    ans=0
    for p,k in product([i-1,i,i+1],[j-1,j,j+1]):
        try:
            if s[p][k]=="#":
                ans+=1
        except:
            pass
    s[i][j]=str(ans)
for i in s:
    print("".join(i))

Submission status: Accepted

Solution code:
import numpy as np

h,w = map(int,(input().split()))
s = [list(map(str,input())) for i in range(h)]
l = [[0]*(w+2) for i in range(h+2)]


def addFlag(i,j):
    for x in range(3):
        for y in range(3):
            if l[i+x][j+y] != '#':
                l[i+x][j+y] += 1
                
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            l[i+1][j+1] = '#'
            addFlag(i,j)

l = np.delete(l,[0,h+1],0)
l = np.delete(l,[0,w+1],1)


for i in l:
    ans = ''.join(map(str,i))
    print(ans)

Submission status: Accepted

Solution code:
h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
bomb_count = 0
for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      continue
    else:
      for k in range(3):
        for l in range(3):
          a = i-1+k
          b = j-1+l
          if a < 0:
            continue
          elif a > h-1:
            continue
          if b < 0:
            continue
          elif b > w-1:
            continue
          if s[a][b] == "#":
            bomb_count += 1
      s[i][j] = str(bomb_count)
      bomb_count = 0
for i in range(h):
  print("".join(s[i]))

Submission status: Accepted

Solution code:
h, w = map(int, input().split())
field = [list(input()) for _ in range(h)]
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
for i in range(h):
    for j in range(w):
        if field[i][j] == "#":
            continue
        num = 0
        for k in range(8):
            ni = i + dy[k]
            nj = j + dx[k]
            if ni < 0 or h <= ni:
                continue
            if nj < 0 or w <= nj:
                continue
            if field[ni][nj] == "#":
                num += 1
        field[i][j] = str(num)
for out in field:
    print("".join(out))

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


for i in range(1,h+1):
    s = ''
    for j in range(1,w+1):
        directions = [(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1)]
        alist = [(i+dir[0],j+dir[1]) for dir in directions]
        cnt = 0
        for a in alist:
            if l[a[0]][a[1]] == '#':
                cnt += 1
        if l[i][j] == '#':
            s += '#'
        else:
            s += str(cnt)
    print(s)

Submission status: Accepted

Solution code:
from typing import List


def search_bomb(i: int, j: int, ss: List[str]) -> int:
    count = 0
    for k in range(i - 1, i + 2):
        for l in range(j - 1, j + 2):
            if 0 <= k <= h - 1 and 0 <= l <= w - 1:
                if ss[k][l] == '#':
                    count += 1
    return count


def answer(ss: List[str]) -> List[str]:
    result = list('' for _ in range(h))

    for i, s in enumerate(ss):
        line = ''
        for j, c in enumerate(s):
            if c == '.':
                line += str(search_bomb(i, j, ss))
            elif c == '#':
                line += '#'
        result[i] = line

    return result


def main():
    ss = [input() for _ in range(h)]
    for line in answer(ss):
        print(line)


if __name__ == '__main__':
    h, w = map(int, input().split())
    main()

Submission status: Accepted

Solution code:
#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc075/tasks/abc075_b

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


def check(S, i, j):
    if S[i][j] == '#':
        return '#'

    num = 0
    for _i in range(max(0, i-1), min(H, i+2)):
        for _j in range(max(0, j-1), min(W, j+2)):
            if S[_i][_j] == '#':
                num += 1
    return str(num)


for i in range(H):
    for j in range(W):
        S[i][j] = check(S, i, j)

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

Submission status: Accepted

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

for i in range(H):
    for j in range(len(L[i])):
        if L[i][j] == '.':
            cnt = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if ((i+k) < H) and ((j+l) < len(L[i])) and ((j+l) >= 0) and ((i+k) >= 0):
                        if L[i+k][j+l] == '#':
                            cnt += 1
            L[i][j] = str(cnt)

for i in range(len(L)):
    print(''.join(L[i]))

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

import copy

dir = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
ans = copy.deepcopy(s)
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in dir:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '#':
                        cnt += 1
            ans[i][j] = cnt




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

Submission status: Accepted

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

#両端にドットを追加
for i in range(len(x)):
    x[i].insert(0, ".")
    x[i].append(".")
#最上部と最下部の行にドットを追加
x.insert(0, ["."]*(W+2))
x.append(["."]*(W+2))

#3*3→5*5 from[1][1] to[3][3] to[H][W]
tmp = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        if x[i][j] == "#":
            x[i][j] = "#"
            continue
        if x[i-1][j-1] == "#":
            tmp += 1
        if x[i-1][j] == "#":
            tmp += 1
        if x[i-1][j+1] == "#":
            tmp += 1
        if x[i][j-1] == "#":
            tmp += 1
        if x[i][j+1] == "#":
            tmp += 1
        if x[i+1][j-1] == "#":
            tmp += 1
        if x[i+1][j] == "#":
            tmp += 1
        if x[i+1][j+1] == "#":
            tmp += 1
        x[i][j] = str(tmp)
        tmp = 0
#最上部と最下部の行のドットを削除
del x[0]
del x[-1]
#両端のドットを削除
for i in range(len(x)):
    del x[i][0]
    del x[i][-1]

for i in range(len(x)):
    print(''.join(x[i]))

Submission status: Accepted

Solution code:
def input_int():
    return(int(input()))

def input_int_list():
    return(list(map(int,input().split())))

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

def run():
    h, w = input_int_map()
    rows = []

    for _ in range(h):
        rows.append(list(input()))

    for row in range(h):
        for col in range(w):
            replace_space(row, col, rows)

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

def replace_space(row: int, col:int, rows: []):

    if rows[row][col] != '.':
        return

    h = len(rows)
    w = len(rows[0])

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

    bomb_count = 0
    for y_diff, x_diff in nums:
        y = row + y_diff
        x = col + x_diff

        if y >= h or y < 0:
            continue

        if x >= w or x < 0:
            continue

        if rows[y][x] == '#':
            bomb_count += 1

    rows[row][col] = bomb_count

run()

# 1, 2, 3
# 2  3  1

# 1  2

# 1, 2  3, 4
# 2  3  4  1

Submission status: Accepted

Solution code:
import copy

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

dir = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
ans = copy.deepcopy(s)
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in dir:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '#':
                        cnt += 1
            ans[i][j] = cnt
for i in ans:
    print(''.join(map(str, i)))

Submission status: Accepted

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

for i in range(h):
    S = input()
    for j, s in enumerate(S):
        if s == "#":
            ans[i+1][j+1] = "#"
            if ans[i][j+1] != "#":
                ans[i][j+1] += 1
            if ans[i+1][j] != "#":
                ans[i+1][j] += 1
            if ans[i][j] != "#":
                ans[i][j] += 1
            if ans[i+1][j + 2] != "#":
                ans[i+1][j + 2] += 1
            if ans[i][j + 2] != "#":
                ans[i][j + 2] += 1
            if ans[i + 2][j+1] != "#":
                ans[i + 2][j+1] += 1
            if ans[i + 2][j + 2] != "#":
                ans[i + 2][j + 2] += 1
            if ans[i + 2][j] != "#":
                ans[i + 2][j] += 1

for a in ans[1:-1]:
    print("".join(map(str, a))[1:-1])

Submission status: Accepted

Solution code:
from itertools import product
H,W=list(map(int, input().split()))
M=[list(input()) for _ in range(H)]
S=set(product([-1,0,1],repeat=2))-{(0,0)}

for i in range(H):
  for j in range(W):
    if M[i][j]=='#':
      continue
    c=0
    for k in S:
      h=i+k[0]
      w=j+k[1]
      if 0<=h<H and 0<=w<W:
        if M[h][w]=='#':
          c+=1
    M[i][j]=str(c)
for i in range(H):
  print(''.join(M[i]))

Submission status: Accepted

Solution code:
import sys

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

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

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

Submission status: Accepted

Solution code:
def resolve():
    h,w=map(int,input().split())
    s=[]
    for _ in range(h):
        s.append(list(input()))
    dx=[-1,-1,-1,0,0,1,1,1]
    dy=[-1,0,1,-1,1,-1,0,1]
    for x in range(h):
        for y in range(w):
            if s[x][y]=='#':
                for k in range(8):
                    nx=x+dx[k]
                    ny=y+dy[k]
                    if not(nx<h and 0<=nx and ny<w and 0<=ny) or s[nx][ny]=='#':
                        continue
                    if s[nx][ny]=='.':
                        s[nx][ny]='1'
                    else:
                        s[nx][ny]=str(ord(s[nx][ny])-ord('0')+1)
    for i in range(h):
        for j in range(w):
            if s[i][j]=='.':
                s[i][j]='0'
    for i in range(h):
        print(''.join(s[i]))
resolve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

H, W = mapint()
S = [list(input()) for _ in range(H)]
ans = [['#']*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        cnt = 0
        for h in range(-1, 2):
            for w in range(-1, 2):
                nh = i + h
                nw = j + w
                if nh<0 or nh>=H or nw<0 or nw>=W:
                    continue
                if S[nh][nw]=='#':
                    cnt += 1
        ans[i][j] = str(cnt)

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

Submission status: Accepted

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

sl = [list(input()) for _ in range(h)]
ans = [[0]*(w+2) for _ in range(h+2)]
# print(sl,ans)

for i in range(h):
    for j in range(w):
        row = i+1
        col = j+1
        # print(sl[i][j])
        if sl[i][j] == '#':
            ans[row][col] = '#'
            if ans[row-1][col] != '#':ans[row-1][col] += 1
            if ans[row+1][col] != '#':ans[row+1][col] += 1
            if ans[row][col-1] != '#':ans[row][col-1] += 1
            if ans[row][col+1] != '#':ans[row][col+1] += 1
            if ans[row-1][col-1] != '#':ans[row-1][col-1] += 1
            if ans[row-1][col+1] != '#':ans[row-1][col+1] += 1
            if ans[row+1][col-1] != '#':ans[row+1][col-1] += 1
            if ans[row+1][col+1] != '#':ans[row+1][col+1] += 1



for i in range(h+2):
    if i != 0 and i != h+1:
        print(*ans[i][1:-1], sep='')

Submission status: Accepted

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

for i in range(h):
    S = input()
    for j, s in enumerate(S):
        if s == "#":
            ans[i][j] = "#"
            if i != 0:
                if ans[i - 1][j] != "#":
                    ans[i - 1][j] += 1
            if j != 0:
                if ans[i][j - 1] != "#":
                    ans[i][j - 1] += 1
            if i != 0 and j != 0:
                if ans[i - 1][j - 1] != "#":
                    ans[i - 1][j - 1] += 1
            if j != w - 1:
                if ans[i][j + 1] != "#":
                    ans[i][j + 1] += 1
            if j != w - 1 and i != 0:
                if ans[i - 1][j + 1] != "#":
                    ans[i - 1][j + 1] += 1
            if i != h - 1:
                if ans[i + 1][j] != "#":
                    ans[i + 1][j] += 1
            if i != h - 1 and j != w - 1:
                if ans[i + 1][j + 1] != "#":
                    ans[i + 1][j + 1] += 1
            if i != h - 1 and j != 0:
                if ans[i + 1][j - 1] != "#":
                    ans[i + 1][j - 1] += 1

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

Submission status: Accepted

Solution code:
import copy

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

a = []
for i in range(-1, 2):
    for j in range(-1, 2):
        a.append((i, j))

ans = [[0]*w for _ in range(h)]
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):
        if s[i][j] == '.':
            cnt = 0
            for y, x in a:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '#':
                        cnt += 1
            ans[i][j] = cnt
for i in ans:
    print(''.join(map(str, i)))

Submission status: Accepted

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

Submission status: Accepted

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

def s(y, x):
  if x >= W or x < 0 or y >= H or y < 0:
    return 0

  return 1 if SS[y][x] == '#' else 0

for i in range(H):
  S = SS[i]
  l = []
  for j in range(W):
    c = S[j]
    if c == '.':
      l.append(str(sum([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)])))
    else:
      l.append('#')
  print(''.join(l))

Submission status: Accepted

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

def getBomb(x, y):
  if x < 0 or y < 0 or x >= W or y >= H:
    return 0
  if S[y][x] == "#":
    return  1
  return 0

def getBombNum(x, y):
  total = 0
  for i in range(-1, 2):
    for j in range(-1, 2):
      if i == 0 and j == 0:
        if getBomb(x, y) == 1:
          return -1
      else:
        total += getBomb(x + i, y + j)
  return total

table = [0] * (W * H)
for y in range(H):
  for x in range(W):
    num = getBombNum(x, y)
    if num < 0:
      print("#", end="")
    else:
      print(num, end="")
  print()

Submission status: Accepted

Solution code:
from collections import Counter
import math
import statistics
import itertools
a,b=map(int,input().split())
# b=input()
# c=[]
# for i in a:
#     c.append(int(i))
# A,B,C= map(int,input().split())
# f = list(map(int,input().split()))
g = [list(map(lambda x: '{}'.format(x), list(input()))) for _ in range(a)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen

for i in range(a):
    for k in range(b):
        count=0
        if g[i][k]==".":
            if k-1>=0:
                if g[i][k-1]=="#":
                    count+=1
            if i-1>=0:
                if g[i-1][k]=="#":
                    count+=1
            if i-1>=0 and k-1>=0:
                if g[i-1][k-1]=="#":
                    count+=1
            if k+1<=b-1:
                if g[i][k+1]=="#":
                    count+=1
            if i+1<=a-1:
                if g[i+1][k]=="#":
                    count+=1
            if i+1<=a-1 and k+1<=b-1:
                if g[i+1][k+1]=="#":
                    count+=1
            if i-1>=0 and k+1<=b-1:
                if g[i-1][k+1]=="#":
                    count+=1
            if i+1<=a-1 and k-1>=0:
                if g[i+1][k-1]=="#":
                    count+=1
            g[i][k]=str(count)


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

Submission status: Accepted

Solution code:
H, W = map(int, input().split())
L = ['.' + input() + '.' for _ in range(H)]
L = ['.' * (W + 2)] + L + ['.' * (W + 2)]
A = []
for i in range(1, H + 1):
  S = ""
  for j in range(1, W + 1):
    if L[i][j] == '.':
      t = [L[i-1][j-1], L[i-1][j], L[i-1][j+1], L[i][j+1], L[i+1][j+1], L[i+1][j], L[i+1][j-1], L[i][j-1]]
      S += str(t.count('#'))
    else:
      S += '#'
  A.append(S)
[print(i) for i in A]

Submission status: Accepted

Solution code:
h,w=map(int,input().split())
l = [input() for _ in range(h)]
k = [['#' for x in range(w) ] for _ in range(h)]
dx = [0,0,1,-1,1,1,-1,-1]
dy = [1,-1,0,0,1,-1,1,-1]
for i in range(h):
    for j in range(w):
        if l[i][j] == '.':
            cnt = 0
            for di in range(8):
                x = j+dx[di]
                y = i+dy[di]
                if x<0 or y<0 or x >=w or y >=h:
                    continue
                if l[y][x] == '#':
                    cnt +=1
            k[i][j] = cnt
for i in range(h):
    print(''.join(map(str, k[i])))

Submission status: Accepted

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

for i in range(h):
  m.append(input())

ans=[["#"]*(w) for _ in range(h)]
for i in range(h):
  for j in range(w):
    cnt = 0
    if h > i-1 >= 0:
      if m[i-1][j] == "#":
        cnt += 1
      if w > j-1 >=0 and m[i-1][j-1] == "#":
        cnt += 1
      if w > j+1 >=0 and m[i-1][j+1] == "#":
        cnt += 1
    if h > i+1 >= 0:
      if m[i+1][j] == "#":
        cnt += 1
      if w > j-1 >=0 and m[i+1][j-1] == "#":
        cnt += 1
      if w > j+1 >=0 and m[i+1][j+1] == "#":
        cnt += 1
      
    if w > j-1 >=0 and m[i][j-1] == "#":
      cnt += 1
    if w > j+1 >=0 and m[i][j+1] == "#":
      cnt += 1
    if m[i][j] != "#":
      ans[i][j] = str(cnt)

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(h):
    s = input()
    sl.append(s)

def scheck(s):
    if s == "#":
        return 1
    else:
        return 0
        
def check(i,j,sl):
    cnt = 0
    for k in range(-1,2):
        for l in range(-1,2):
            if h > i+k >= 0 and w > j+l >= 0:
                cnt += scheck(sl[i+k][j+l])
    return str(cnt)

for i in range(h):
    for j in range(w):
        if sl[i][j] == ".":
            bl[i][j] = check(i,j,sl)
        else:
            bl[i][j] = "#"

for b in bl:
    t = ''.join(b)
    print(t)

Submission status: Accepted

Solution code:
lst = input().split()
H = int(lst[0])
W = int(lst[1])

field = []
for i in range(H):
   l = []
   s = input()
   for j in range(W):
      l.append(s[j])
   field.append(l)

def count(x, y):
   result = 0
   for P 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]]:
      try:
         if (0 <= P[0]) and (0 <= P[1]) and (field[P[1]][P[0]] == '#'):
            result += 1
      except:
         pass
   return result

for i in range(H):
   for j in range(W):
      if field[i][j] == '.':
         field[i][j] = str(count(j, i))

def list2str(L):
   result = ''
   for t in L:
      result += t
   return result

for I in field:
   print(list2str(I))

Submission status: Accepted

Solution code:
import sys
H, W = map(int, input().split())
if H == 1:
    if W == 1:
        s = input()
        if s == "#":
            print("#")
        else:
            print(0)
    else:
        cnt = [0 for _ in range(W)]
        s = list(input())
        for w in range(W):
            if w == 0:
                cnt[w] += s[w+1] == "#"
            elif w == W-1:
                cnt[w] += s[w-1] == "#"
            else:
                cnt[w] += s[w+1] == "#"
                cnt[w] += s[w-1] == "#"
        for w in range(W):
            if s[w] == "#":
                cnt[w] = "#"
        ans = map(str, cnt)
        print("".join(ans))
    sys.exit(0)
if W == 1:
    cnt = [0 for _ in range(H)]
    s = []
    for h in range(H):
        s.append(input())
    for h in range(H):
        if h == 0:
            cnt[h] += s[h+1] == "#"
        elif h == H-1:
            cnt[h] += s[h-1] == "#"
        else:
            cnt[h] += s[h-1] == "#"
            cnt[h] += s[h+1] == "#"
    for h in range(H):
        if s[h] == "#":
            print("#")
        else:
            print(cnt[h])
    sys.exit(0)
    
S = []
cnt = [[0 for _ in range(W)] for _ in range(H)]
for h in range(H):
    s = list(input())
    for w in range(W):
        if w == 0:
            cnt[h][w] += s[w+1] == "#"
        elif w == W-1:
            cnt[h][w] += s[w-1] == "#"
        else:
            cnt[h][w] += s[w+1] == "#"
            cnt[h][w] += s[w-1] == "#"
    S.append(s)
    
for h in range(H):
    for w in range(W):
        if h == 0:
            cnt[h][w] += S[h+1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "#"
            else:
                cnt[h][w] += S[h+1][w-1] == "#"
                cnt[h][w] += S[h+1][w+1] == "#"
        elif h == H-1:
            cnt[h][w] += S[h-1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "#"
            else:
                cnt[h][w] += S[h-1][w-1] == "#"
                cnt[h][w] += S[h-1][w+1] == "#"
        else:
            cnt[h][w] += S[h+1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "#"
            else:
                cnt[h][w] += S[h+1][w-1] == "#"
                cnt[h][w] += S[h+1][w+1] == "#"
            cnt[h][w] += S[h-1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "#"
            else:
                cnt[h][w] += S[h-1][w-1] == "#"
                cnt[h][w] += S[h-1][w+1] == "#"
                
for h in range(H):
    s = S[h]
    for w in range(W):
        if s[w] == "#":
            cnt[h][w] = "#"
    ans = map(str, cnt[h])
    print("".join(ans))