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

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

def count_bomb(h_i, w_i):
    sq = ""
    for i in range(3):
        sq += s[h_i + i - 1][w_i - 1: w_i + 2]
    return sq.count("

for h_i in range(1, h + 1):
    for w_i in range(1, w + 1):
        if s[h_i][w_i] == ".":
            print(count_bomb(h_i, w_i), end="")
        else:
            print("
    print()

Solution (Python):




h, w = map(int, input().split())
s = []
waku = "a"*(w+2)
s.append(waku)
for i in range(h):
    kari = "a" + input() + "a"
    s.append(kari)
s.append(waku)



new_s = []
new_kari = ""
for i in range(1,h + 1):
    for j in range(1,w+1):
        count = 0
        if s[i][j] == ".":
            for k in range(-1,2):
                for l in range(-1,2):
                    if s[i+k][j+l] == "
                        count += 1
            new_kari += str(count)
        else:
            new_kari += "
    new_s.append(new_kari)
    new_kari = ""
for i in range(h):
    print(new_s[i])

Solution (Python):
line1 = input().split()
h,w = int(line1[0]), int(line1[1])

input_mass = []
for _ in range(h):
    s = input()
    t = [s[i] for i in range(len(s))]
    input_mass.append(t)

for high in range(h):
    for width in range(w):
        if input_mass[high][width] == '
            print('
        else:
            count = 0
            for i in [high-1,high,high+1]:
                for j in [width-1,width,width+1]:
                    if 0<=i<h and 0<=j<w:
                        if input_mass[i][j] == '
                            count += 1
            print(count,end='')
    print()

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

after = []
for i in range(h):
    row = ''
    for j in range(w):
        tmp = ''
        if before[i][j] == '.':
            for i2 in (i-1, i, i+1):
                for j2 in (j-1, j, j+1):
                    if (i2 == i and j2 == j) or (0 > i2) or (h <= i2) or (0 > j2) or (w <= j2):
                        continue
                    else:
                        tmp += before[i2][j2]
            row += str(tmp.count('
        else:
            row += '
    after.append(row)

for row in after:
    print(row)

Solution (Python):
import sys
import  math
import bisect
from statistics import *
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h, w = na()
li = []
for i in range(h):
    li.append(input())
for i in range(h):
    for j in range(w):
        ct = 0
        if li[i][j] == "
            print("
        else:
            for p in [li[i-1+k][j-1+l] for k in range(3) for l in range(3) if 0<=i-1+k<=h-1 and 0<=j-1+l<=w-1]:
                if p == "
                    ct += 1
            print(ct, end="")
    print()

Solution (Python):
import sys
import  math
import bisect
from statistics import *
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

h, w = na()
li = []
for i in range(h):
    li.append(input())
for i in range(h):
    for j in range(w):
        ct = 0
        if li[i][j] == "
            print("
        else:
            ppp = []
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if 0 <= i + k <= h-1 and 0 <= j + l <= w-1:
                        ppp.append(li[i+k][j+l])
            for p in ppp:
                if p == "
                    ct += 1
            print(ct, end="")
    print()

Solution (Python):
H, W = map(int, input().split())
Slist = list()
HWlist =list()
for i in range(0,H):
    Slist.append(str(input()))
    HWlist.append(list(Slist[i]))

Reslist = list()
for h in range(0,H):
    Reslist.append("")

for i in range(0,H):
    for j in range(0,W):
        if HWlist[i][j] != "
            count = 0
            if i-1 >= 0:
                if HWlist[i-1][j] == "
                    count = count + 1
            if i+1 < H:
                if HWlist[i+1][j] == "
                    count = count + 1
            if j-1 >= 0:
                if HWlist[i][j-1] == "
                    count = count + 1
            if j+1 < W:
                if HWlist[i][j+1] == "
                    count = count + 1
            if i-1 >= 0 and j-1>=0:
                if HWlist[i-1][j-1] == "
                    count = count +1
            if i-1 >=0 and j+1<W:
                if HWlist[i-1][j+1] == "
                    count = count +1
            if i+1 <H and j-1>=0:
                if HWlist[i+1][j-1] == "
                    count = count +1
            if i+1 <H and j+1<W:
                if HWlist[i+1][j+1] == "
                    count = count +1
            Reslist[i] = Reslist[i] + str(count)
        else:
            Reslist[i] = Reslist[i] + "
for l in range (0,H):
    print(Reslist[l])

Solution (Python):

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

if H == 1 and W == 1:
  if S[0][0] == ".":
    L[0][0] = 0

elif H == 1:
  
  if S[0][1]=="
    L[0][0]+=1
  if S[0][W-2]=="
    L[0][W-1]+=1
  for w in range(1,W-1):
    if S[0][w-1]=="
      L[0][w]+=1
    if S[0][w+1]=="
      L[0][w]+=1

elif W == 1:
  
  if S[1][0]=="
    L[0][0]+=1
  if S[H-2][0]=="
    L[H-1][0]+=1
  for h in range(1,H-1):
    if S[h-1][0]=="
      L[h][0]+=1
    if S[h+1][0]=="
      L[h][0]+=1
  
  
else:

  if S[0][1]=="
    L[0][0]+=1
  if S[1][0]=="
    L[0][0]+=1
  if S[1][1]=="
    L[0][0]+=1
  if S[H-1][1]=="
    L[H-1][0]+=1
  if S[H-2][0]=="
    L[H-1][0]+=1
  if S[H-2][1]=="
    L[H-1][0]+=1
  if S[0][W-2]=="
    L[0][W-1]+=1
  if S[1][W-1]=="
    L[0][W-1]+=1
  if S[1][W-2]=="
    L[0][W-1]+=1
  if S[H-1][W-2]=="
    L[H-1][W-1]+=1
  if S[H-2][W-1]=="
    L[H-1][W-1]+=1
  if S[H-2][W-2]=="
    L[H-1][W-1]+=1

  for w in range(1,W-1):
    if S[0][w-1]=="
      L[0][w]+=1
    if S[0][w+1]=="
      L[0][w]+=1
    if S[1][w-1]=="
      L[0][w]+=1
    if S[1][w]=="
      L[0][w]+=1
    if S[1][w+1]=="
      L[0][w]+=1
    if S[H-1][w-1]=="
      L[H-1][w]+=1
    if S[H-1][w+1]=="
      L[H-1][w]+=1
    if S[H-2][w-1]=="
      L[H-1][w]+=1
    if S[H-2][w]=="
      L[H-1][w]+=1
    if S[H-2][w+1]=="
      L[H-1][w]+=1

  for h in range(1,H-1):
    if S[h-1][0]=="
      L[h][0]+=1
    if S[h+1][0]=="
      L[h][0]+=1
    if S[h-1][1]=="
      L[h][0]+=1
    if S[h][1]=="
      L[h][0]+=1
    if S[h+1][1]=="
      L[h][0]+=1
    if S[h-1][W-1]=="
      L[h][W-1]+=1
    if S[h+1][W-1]=="
      L[h][W-1]+=1
    if S[h-1][W-2]=="
      L[h][W-1]+=1
    if S[h][W-2]=="
      L[h][W-1]+=1
    if S[h+1][W-2]=="
      L[h][W-1]+=1

  for w in range(1,W-1):
    for h in range(1,H-1):
      if S[h-1][w-1]=="
        L[h][w]+=1
      if S[h-1][w]=="
        L[h][w]+=1
      if S[h-1][w+1]=="
        L[h][w]+=1
      if S[h][w-1]=="
        L[h][w]+=1
      if S[h][w+1]=="
        L[h][w]+=1
      if S[h+1][w-1]=="
        L[h][w]+=1
      if S[h+1][w]=="
        L[h][w]+=1
      if S[h+1][w+1]=="
        L[h][w]+=1

C = [["" for w in range(W)] for h in range(H)]

for w in range(W):
  for h in range(H):
    if S[h][w]==".":
      C[h][w]=str(L[h][w])
    else:
      C[h][w]="

o = ""
for c in C:
  o = ""
  for cc in c:
    o += cc
  print(o)

Solution (Python):


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

for i in range(h):
    s[i] = list(input())


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

for i in range(h):
    for j in range(w):
        if s[i][j] == "
            continue
        else:
            count = 0
            for d in range(len(dx)):
                x = j + dx[d]
                y = i + dy[d]

                if not 0 <= x < w:
                    continue
                if not 0 <= y < h:
                    continue
                if s[y][x] == "
                    count += 1
            s[i][j] = str(count)

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

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

news = []
for y in range(h):
    tmp = ''
    for x in range(w):
        cnt = 0
        if s[y][x] == '
            tmp += '
            continue
        for dy in range(-1, 2):
            for dx in range(-1, 2):
                if dx == 0 and dy == 0:
                    continue
                nx = dx + x
                ny = dy + y
                if not (0 <= nx < w and 0 <= ny < h):
                    continue
                if s[ny][nx] == '
                    cnt += 1
        tmp += str(cnt)
    news.append(tmp)
for i in range(h):
    print(news[i])

Solution (Python):
H,W = map(int,input().split())
S = []
for i in range(H):
  S.append(list(input()))
  
if H == 1:
  if W == 1:
    if S[0][0] == '.':
      S[0][0] = str(0)
    print("".join(S[0]))
    exit()
  if W == 2:
    if S[0][0] == '.' and S[0][1] == '.':
      S[0][0] = str(0)
      S[0][1] = str(0)
    elif S[0][0] == '.' and S[0][1] == '
      S[0][0] = str(1)
    elif S[0][0] == '
      S[0][1] = str(1)
    else:
      pass
    print("".join(S[0]))
    exit()
  
  if W > 2:
    for i in range(W):
      if S[0][i] == '.':
        if i == 0:
          S[0][i] = str(int(S[0][i+1] == '
        elif 0 < i < W-1:
          S[0][i] = str(int(S[0][i-1] == '
        elif i == W-1:
          S[0][i] = str(int(S[0][i-1] == '

    print("".join(S[0]))
    exit()
    
for i in range(H):
  for j in range(W):
    if S[i][j] == '.':
      if i == 0:
        if j == 0:
          cnt = S[i][:j+2].count('
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i][j-1:j+2].count('
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i][j-1:].count('
          S[i][j] = str(cnt)
      elif 0<i<H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('
          S[i][j] = str(cnt)
      elif i == H-1:
        if j == 0:
          cnt = S[i-1][:j+2].count('
          S[i][j] = str(cnt)
        elif 0 < j < W - 1:
          cnt = S[i-1][j-1:j+2].count('
          S[i][j] = str(cnt)
        elif j ==W -1:
          cnt = S[i-1][j-1:].count('
          S[i][j] = str(cnt)

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

Solution (Python):
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] == "
        else:
            ans = 0
            for k in [-1, 0, 1]:
                for l in [-1, 0, 1]:
                    if k == l == 0: continue
                    if 0 <= i+k < h and 0 <= j+l < w and s[i+k][j+l] == "
                        ans += 1
        print(ans, sep="", end="")
    print("")

Solution (Python):
tmp = [int(i) for i in input().split()]
H, W = tmp[0], tmp[1]

matrix = [[0 for i in range(W+2)] for j in range(H+2)]
real = [input().split() for _ in range(H)]
tmp = []

for rs in real:
  for r in rs:
    tmp.append([1 if l == '

for i in range(H):
    for j in range(W):
        matrix[i+1][j+1] = tmp[i][j]
tmp = [[0 for i in range(W)] for j in range(H)]

for i in range(1, H+1):
    for j in range(1, W+1):
        if not matrix[i][j] == 1:
            tmp[i-1][j-1] = (sum(matrix[i-1][j-1:j+2]) + matrix[i][j-1] + matrix[i][j+1] + matrix[i+1][j] + matrix[i+1][j-1] + matrix[i+1][j+1])
for i in range(1, H+1):
    for j in range(1, W+1):
        if matrix[i][j] == 1:
            tmp[i-1][j-1] = '
        else:
            tmp[i-1][j-1] = str(tmp[i-1][j-1])
for t in tmp:
    mojiretsu = ""
    for i in t:
        mojiretsu += i
    print(mojiretsu)

Solution (Python):
HandW = input().split()
h = HandW[0]
w = HandW[1]

list1 = [["." for i in range(int(w)+2)] for j in range(int(h)+2)]

for i in range(int(h)):
    str1 = input()
    for j in range(int(w)):
        list1[i+1][j+1] = str1[j]

for i in range(int(h)):
    for j in range(int(w)):
        if list1[i+1][j+1] != '
            
            count = sum(l[j:j+3].count('
            list1[i+1][j+1] = count



for i in range(1,int(h) + 1):
    for j in range(1,int(w) + 1):
        print(list1[i][j],end = '')
    print('')

Solution (Python):


def resolve():
    H, W = map(int, input().split())
    s = [[0] * W for _ in range(H)]
    for i in range(H):
        a = input()
        for j in range(W):
            s[i][j] = a[j]

    
    def check(h, w):
        if (h < 0 or w < 0 or h >= H or w >= W):
            return False
        if (s[h][w] == '
            return True
        return False

    for h in range(H):
        for w in range(W):
            if s[h][w] == '
                continue
            mines = 0
            for i in [-1, 0, 1]:
                for j in [-1, 0, 1]:
                    if check(h+i, w+j):
                        mines += 1
            s[h][w] = mines

    for h in range(H):
        for w in range(W):
            print(s[h][w], end='')
        print("")


if __name__ == "__main__":
    resolve()

Solution (Python):
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
d = [[1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,-1],[-1,0],[-1,1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            cnt = 0
            for x in d:
                if 0 <= i+x[0] < H and 0 <= j+x[1] < W and S[i+x[0]][j+x[1]] == "
                    cnt += 1
            S[i][j] = str(cnt)
for i in range(H):
    print("".join(S[i]))

Solution (Python):
h,w=map(int,input().split())
s=[]*h
b=[[0]*w]*h

for i in range(h):
    b[i]=list(input())
       
for y in range(h):
    for x in range(w):
        if b[y][x]==".":
            b_no=0
            for dy in range(-1,2):
                for dx in range(-1,2):
                    if 0 <= y+dy and y+dy < h and 0 <= x+dx and x+dx <w: 
                        if b[y+dy][x+dx]=="
                            b_no += 1
            b[y][x]=b_no
for y in range(h):
    for l in b[y]:
        print(l,end="")
    print()

Solution (Python):

import sys
input = sys.stdin.readline

def check_neighbors(i: int, j: int) -> int:
    
    ret = 0
    for x, y in neighbors:
        a, b = i + x, j + y
        if 0 <= a < H and 0 <= b < W:
            ret += 1 if S[a][b] == "
    return ret


def main():
    global H, W, S, neighbors
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    neighbors = [(-1, -1), (-1, 0), (-1, 1), (0, -1)]
    neighbors += [(0, 1), (1, -1), (1, 0), (1, 1)]
    A = [""] * H
    for i, s in enumerate(S):
        for j, a in enumerate(s):
            if a == "
                A[i] += "
            else:
                cnt = check_neighbors(i, j)
                A[i] += str(cnt)
    print(*A, sep="\n")


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    H, W = tuple(map(int, input().split()))
    S = tuple(input().rstrip() for _ in range(H))
    moves = [(-1, -1), (-1, 0), (-1, 1), (0, -1)]
    moves += [(0, 1), (1, -1), (1, 0), (1, 1)]
    A = [""] * H
    for i, s in enumerate(S):
        for j, a in enumerate(s):
            if a == "
                A[i] += "
            else:
                cnt = sum(
                    S[i + x][j + y] == "
                    for x, y in moves
                    if 0 <= i + x < H and 0 <= j + y < W
                )
                A[i] += str(cnt)
    print(*A, sep="\n")


if __name__ == "__main__":
    main()

Solution (Python):


h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]

ans=[[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=='
      ans[i-1][j-1]='
    else:
      cnt=(S[i-1][j-1]=="
      ans[i-1][j-1]=cnt

for i in range(h):
  print(*ans[i],sep="")

Solution (Python):
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]

ans=[[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=='
      ans[i-1][j-1]='
    else:
      cnt=(S[i-1][j-1]=="
      ans[i-1][j-1]=cnt

for i in range(h):
  print(*ans[i],sep="")

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

ans = [[[] for _ in range(w)] for _ in range(h)]

for i in range(1, h+1):
    for j in range(1, w+1):
        cnt = 0
        if s[i][j] == '.':
            if s[i-1][j-1] == '
                cnt += 1
            if s[i-1][j] == '
                cnt += 1
            if s[i-1][j+1] == '
                cnt += 1
            if s[i][j-1] == '
                cnt += 1
            if s[i][j+1] == '
                cnt += 1
            if s[i+1][j-1] == '
                cnt += 1
            if s[i+1][j] == '
                cnt += 1
            if s[i+1][j+1] == '
                cnt += 1

            ans[i-1][j-1] = cnt
        else:
            ans[i-1][j-1] = '

for x in ans:
    print(*x, sep='')

Solution (Python):
h,w=map(int,input().split())
S=[["."]*(w+2)]+[["."]+list(input())+["."] for _ in range(h)]+[["."]*(w+2)]
ans=[[0 for i in range(w)] for j in range(h)]
for i in range(1,h+1):
  for j in range(1,w+1):
    if S[i][j]=="
      ans[i-1][j-1]="
    else:
      count=(S[i-1][j-1]=="
      ans[i-1][j-1]=count
for i in range(h):
  print(*ans[i],sep="")

Solution (Python):
h,w = map(int,input().split())
count = 0
X = []
p = []
X.append(["@"]*(w+2))
for _ in range(h):
  p = []
  x = input()
  for i in range(w):
    p.append(x[i])
  X.append(["@"]+p+["@"])  
X.append(["@"]*(w+2))
for i in range(1,h+1):
  for j in range(1,w+1):
    count=0
    if X[i][j]==".":
      if X[i-1][j-1]=="
      if X[i-1][j]=="
      if X[i-1][j+1]=="
      if X[i][j-1]=="
      if X[i][j+1]=="
      if X[i+1][j-1]=="
      if X[i+1][j]=="
      if X[i+1][j+1]=="
      count=str(count)
      X[i][j]=count
for k in range(1,h+1):
  print(*X[k][1:w+1],sep="")

Solution (Python):
h, w = map(int, input().split())
s = [ [0] + [0 if a=='.' else 1 for a in input()] + [0] for _ in range(h)]
s = [[0 for _ in range(w+2)]] + s + [[0 for _ in range(w+2)]]

for j in range(1,h+1):
  ans = []
  for i in range(1,w+1):
    if s[j][i] == 0:
      ans.append(str(sum(s[j-1][i-1:i+2])+sum(s[j][i-1:i+2])+sum(s[j+1][i-1:i+2])))
    else:
      ans.append('
  print(''.join(ans))

Solution (Python):
import numpy as np
h,w=map(int,input().split())
a=np.array([list(input()) for _ in range(h)])
for i in range(h):
    for j in range(w):
        if a[i,j]!='
            a[i,j]=np.count_nonzero(a[max(i-1,0):min(i+2,h),max(j-1,0):min(j+2,w)]=='
for i in range(h):
    print(''.join(a[i,:]))

Solution (Python):
H, W = map(int, input().split())
Ss = ["," * (W+2)]
Ss.extend(["," + input() + "," for _ in range(H)])
Ss.extend(["," * (W+2)])


def sweep(x, y):
  count = 0
  
  if Ss[x-1][y-1] == "
    count += 1
  if Ss[x-1][y] == "
    count += 1
  if Ss[x-1][y+1] == "
    count += 1
  if Ss[x][y-1] == "
    count += 1
  if Ss[x][y+1] == "
    count += 1
  if Ss[x+1][y-1] == "
    count += 1
  if Ss[x+1][y] == "
    count += 1
  if Ss[x+1][y+1] == "
    count += 1
  
  return count
  
  
for i in range(1, H+1):
  for j in range(1, W+1):
    if Ss[i][j] == "
      print("
    else:
      print(sweep(i, j), end="")
  print("")

Solution (Python):
h,w = list(map(int, input().split()))
s = [ list(input()) for _ in range(h)]
res = [ [0] * w for _ in range(h)]
for i in range(h):
  for j in range(w):
    if(s[i][j] == "
      res[i][j] = "
      for k in [-1,0,1]:
        for l in [-1,0,1]:
          if(0 <= i + k and i + k < h):
            if(0 <= j + l and j + l < w):
              if(res[i+k][j+l] != "
                res[i+k][j+l] += 1
for i in range(h):
  print("".join(list(map(str, res[i]))))

Solution (Python):
h,w=map(int,input().split())
s=[]
ans=[['' for _ in range(w)] for _ in range(h)]
for i in range(h):
  s.append(list(input()))
for i in range(h):
  for j in range(w):
    if s[i][j]=="
      ans[i][j]="
    else:
      count=0
      for p in range(max(0,i-1),min(h-1,i+1)+1):
        for q in range(max(0,j-1),min(w-1,j+1)+1):
          if s[p][q]=="
            count+=1
      ans[i][j]=str(count)
for i in range(h):
  print("".join(ans[i]))

Solution (Python):
H, W = map(int, input().split())
Ss = []
for i in range(H):
  S = input()
  Ss.append(S)
Ns = [[0]*W for i in range(H)]
for i in range(H):
  for j in range(W):
    if Ss[i][j] == "
      Ns[i][j] = "
    else:
      r = 0
      for p in range(max(0, i-1), min(H-1, i+1)+1):
        for q in range(max(0, j-1), min(W-1, j+1)+1):
          if Ss[p][q] == "
            r += 1
      Ns[i][j] = r
for i in range(H):
  N = Ns[i]
  print("".join(map(str, N)))

Solution (Python):
y, x= map(int,input().split())
bomb = [0 for i in range(y)]
ary = [[0 for i in range(x)]for i in range(y)]

for i in range(y):
    bomb[i] = list(input())

for a in range(y):
    for b in range(x):
        if bomb[a][b] == "
            ary[a][b] = "
        else :
            ary[a][b] = 0

height_ary =[-1,-1,-1, 0,0, 1,1,1]
width_ary  =[-1, 0 ,1,-1,1,-1,0,1]

for a in range(y):
    for b in range(x):
        if ary[a][b] == "
            for i in range(8):
                h = height_ary[i] + a
                w = width_ary [i] + b
                if h < 0 or h >= y: continue
                if w < 0 or w >= x: continue
                if not ary[h][w] == "
                    ary[h][w] += 1

for i in range(y):
    result = map(str,ary[i])
    print("".join(result))

Solution (Python):
h, w = list(map(int, input().split()))
s = [input() for i in range(h)]
cel = [[[""]for i in range(w)] for j in range(h)]
dir = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
for y in range(h):
    for x in range(w):
        if s[y][x] == "
            cel[y][x] = "
for y in range(h):
    for x in range(w):
        if cel[y][x] == "
            continue
        bomb = 0
        for dx, dy in dir:
            cx = x+dx
            cy = y+dy
            if cx >= 0 and cx < w and cy >= 0 and cy < h:
                if cel[cy][cx] == "
                    bomb += 1
        cel[y][x] = bomb
for y in range(h):
    output = ""
    for x in range(w):
        output += str(cel[y][x])
    print(output)

Solution (Python):
h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]
b = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if a[i][j] == "
            for k in range(max(0, i - 1), min(i + 2, h)):
                for l in range(max(0, j - 1), min(j + 2, w)):
                    b[k][l] += 1
for i in range(h):
    t = ""
    for j in range(w):
        if a[i][j] == "
            t += "
        else:
            t += str(b[i][j])
    print(t)

Solution (Python):
H,W = map(int, input().split())
S = []
for h in range(H):
    S.append(input())
def countsh(x,y):
    count=0
    if W != 1:
        if x == 0:
            if S[y][x+1] == '
                count+=1
        elif x == W - 1:
            if S[y][x-1] == '
                count+=1
        else:
            if S[y][x+1] == '
                count+=1
            if S[y][x-1] == '
                count+=1
    if H != 1:
        if y == 0:
            if S[y+1][x] == '
                count+=1
        elif y == H - 1:
            if S[y-1][x] == '
                count+=1
        else:
            if S[y+1][x] == '
                count+=1
            if S[y-1][x] == '
                count+=1
    if W != 1 and H != 1:
        if x > 0 :
            if y > 0:
                if S[y-1][x-1] == '
                    count+=1
            if y < H - 1:
                if S[y+1][x-1] == '
                    count+=1
        if x < W - 1:
            if y > 0:
                if S[y-1][x+1] == '
                    count+=1
            if y < H - 1:
                if S[y+1][x+1] == '
                    count+=1
    return count
result = [[0 for i in range(W)] for j in range(H)]
for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            result[h][w] = str(countsh(w,h))
        else:
            result[h][w] = '
for h in range(H):
    print(''.join(result[h]))

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

ret = [[0] * W for _ in range(H)]
for y in range(H) :
    for x in range(W) :
        if S[y][x] == '
            for yy in range(max(0, y-1), min(H, y+2)) :
                for xx in range(max(0, x-1), min(W, x+2)) :
                    if S[yy][xx] != '
                        ret[yy][xx] += 1
                    else :
                        ret[yy][xx] = '
for i in range(H) :
    s = ''.join(map(str, ret[i]))
    print(s)

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

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

shape = [[j if j == '

for i in range(h):
    for j in range(w):
        if shape[i][j] != '
            for a in range(-1, 2):
                for b in range(-1, 2):
                    if 0 <= i + a < h and 0 <= j + b < w and shape[i + a][j + b] == '
                        shape[i][j] += 1


ans = []
for i in shape:
    ans.append(list(map(str, i)))

for i in ans:
    for j in i:
        print(j, end = '')
    print()

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

s = [list(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 = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j] == '
      ans[i][j] = '
    else:
      count = 0
      for k in range(8):
        if i + dx[k] < 0 or h <= i + dx[k]:
          continue
        elif j + dy[k] < 0 or w <= j + dy[k]:
          continue
        elif s[i+dx[k]][j+dy[k]] == '
          count += 1
      ans[i][j] = str(count)


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

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


def count_bomb(i, j):
    ret = 0
    l = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for dy, dx in l:
        if s[i + dy][j + dx] == '
            ret += 1

    return ret


for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == '.':
            print(count_bomb(i, j), end='')
        else:
            pass
            print(s[i][j], end='')
    print()

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

    mv = [[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]]
    for i in range(H):
        t = ""
        for j in range(W):
            if S[i][j] == "
                t += "
            else:
                count = 0
                for di, dj in mv:
                    if 0<=i+di < H and 0 <= j+dj < W:
                        if S[i+di][j+dj] == "
                            count += 1
                t += str(count)
        print(t)

if __name__ == '__main__':
    main()

Solution (Python):
H,W = map(int,input().split())
mine_map = [list(input()) for x in range(H)]
for i in range(H):
  for j in range(W):
    count = 0
    if mine_map[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:
            if mine_map[i+k][j+l] == "
              count += 1
      mine_map[i][j] = count
    print(mine_map[i][j],end="")
  print()

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

d = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            cnt = 0
            for r in range(8):
                y = i + d[r][0]
                x = j + d[r][1]
                if 0 <= x < W and 0 <= y < H and S[y][x] == '
                        cnt = cnt + 1
            S[i][j] = str(cnt)

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

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

grid = np.array([[0] * (W + 2)] * (H + 2))

bombs = [0] * H
for h in range(H):
    bombs[h] = input()

for h in range(H):
    for w in range(W):
        if bombs[h][w] == '
            grid[h + 2][w + 2] += 1
            grid[h + 2][w + 1] += 1
            grid[h + 2][w + 0] += 1
            grid[h + 1][w + 2] += 1
            grid[h + 1][w + 0] += 1
            grid[h + 0][w + 2] += 1
            grid[h + 0][w + 1] += 1
            grid[h + 0][w + 0] += 1
            grid[h + 1][w + 1] = 100

grid[grid>=100] = 100
arr = grid.astype('str')
arr[arr=='100'] = '
for i in range(1, H + 1):
    print(''.join(arr[i][1:-1]))

Solution (Python):
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):
        count = 0
        if(S[i][j]!="
            for k in [-1,0,1]:
                for l in [-1,0,1]:
                    if(i+k>=H or i+k<0 or j+l>=W or j+l<0):
                        continue
                    elif(S[i+k][j+l]=="
                        count += 1
            S[i][j] = str(count)
for i in range(H):
    print("".join(S[i]))