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 = [int(i) for i in input().split()]

s = []

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

def padding(s,h,w):
    res = []
    res_0 = '.'*(w+2)
    for i in range(h+2):
        if i ==0:
            res = res + [res_0]
        elif i == h+1:
            res = res + [res_0]
        else:
            res_add = '.' + s[i-1] + '.'
            res += [res_add]
    return res

def check_mine(s,i,j):
    cout = 0
    for k in [-1+i,i,1+i]:
        for l in [-1+j,j,1+j]:
            if s[k][l] == '
                cout +=1
                
    if s[i][j] == '.':
        return str(cout)
    else:
        return '
    
s = padding(s,h,w)

for i in range(h):
    temp = str()
    for j in range(w):
        temp += check_mine(s,i+1,j+1)
    print(temp)

Solution (Python):

H, W = map(int,input().split())
s = [input() for _ in range(H)]
for h in range(H):
    l = ""
    for w in range(W):
        l += "
                    sum(
                        [t[max(0, w - 1) : min(W, w + 2)]
                            .count("
                         s[max(0, h - 1) : min(H, h + 2)]
                        ]
                        ))
    print(l)

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

pad_S = [['.']*(W+2)] + [['.']+s+['.'] for s in S] + [['.']*(W+2)]

def calc_bomb(r, c, pad_S):
    h = pad_S[r-1][c-1:c+2].count('
    m = pad_S[r][c-1:c+2].count('
    l = pad_S[r+1][c-1:c+2].count('
    return h+m+l

for i, s in enumerate(S):
   r = i+1 
   lis = []
   for k, char in enumerate(s):
       c = k+1
       if char == '
           lis.append('
       else:
           lis.append(str(calc_bomb(r, c, pad_S)))
   print(''.join(lis))

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
dx = [-1, 0, 1]
dy = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            for d_x in dx:
                for d_y in dy:
                    if 0 <= i + d_x < h and 0 <= j + d_y < w and s[i + d_x][j + d_y] != '
                        if s[i + d_x][j + d_y] == '.':
                            s[i + d_x][j + d_y] = 1
                        else:
                            s[i + d_x][j + d_y] += 1

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

Solution (Python):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
dx = [-1, 0, 1]
dy = [-1, 0, 1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '
            for d_x in dx:
                for d_y in dy:
                    if 0 <= i + d_x < h and 0 <= j + d_y < w and s[i + d_x][j + d_y] != '
                        if s[i + d_x][j + d_y] == '.':
                            s[i + d_x][j + d_y] = 1
                        else:
                            s[i + d_x][j + d_y] += 1
                    else:
                        continue

        else:
            continue

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

Solution (Python):

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





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

for i in range(h):
    for j in range(w):
        
        if s[i][j] == "
            mine[i][j]="
            for k in range(len(dx)):
                nx = i + dx[k]
                ny = j + dy[k]
                
                if 0 <= nx < h and 0 <= ny < w:
                    if s[nx][ny] != "
                        mine[nx][ny] += 1
for i in mine:
    print(*i,sep="")

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

def to_bin(x: int) -> str: return format(x, 'b') 
def to_oct(x: int) -> str: return format(x, 'o') 
def to_hex(x: int) -> str: return format(x, 'x') 
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]

from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left


def main():
    h, w = MII()
    s = ['.'*(w+2)]
    for i in range(h):
        s.append('.'+IS()+'.')
    s.append('.'*(w+2))
    ans = []
    for hi in range(1,h+1):
        tmp = ''
        for wi in range(1,w+1):
            if s[hi][wi] == '
                tmp += '
                continue
            cnt = 0
            cnt += sum([1 for c in s[hi-1][wi-1:wi+2] if c == '
            cnt += sum([1 for c in s[hi][wi-1:wi+2] if c == '
            cnt += sum([1 for c in s[hi+1][wi-1:wi+2] if c == '
            tmp += str(cnt)
        ans.append(tmp)
    print(*ans, sep='\n')

if __name__ == '__main__':
    main()

Solution (Python):
h,w=map(int,input().split())
s=[]
for i in range(h):
  s.append(list(input()))
for i in range(0,h):
  for j in range(0,w):
    if s[i][j]==".":
      count=0
      try:
        if s[i-1][j-1]=="
          count+=1
      except IndexError:
        pass
      try:
        if s[i-1][j]=="
          count+=1
      except IndexError:
        pass
      try:
        if s[i-1][j+1]=="
          count+=1
      except IndexError:
        pass
      try:
        if s[i][j-1]=="
          count+=1
      except IndexError:
        pass
      try:
        if s[i][j+1]=="
          count+=1
      except IndexError:
        pass
      try:
        if s[i+1][j-1]=="
          count+=1
      except IndexError:
        pass
      try:
        if s[i+1][j]=="
          count+=1
      except IndexError:
        pass
      try:
        if s[i+1][j+1]=="
          count+=1
      except IndexError:
        pass
      s[i][j]=count

for x in range(0,len(s)):
  for y in range(0,len(s[0])):
    if y!=len(s[0])-1:
      print(s[x][y],end="")
    else:
      print(s[x][y])

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

def count(x, y):
    cnt = 0
    for dx in range(-1, 2):
        for dy in range(-1, 2):
            if not (0<=x+dx<w and 0<=y+dy<h):
                continue
            if s[y+dy][x+dx]=='
                cnt += 1
    return str(cnt)

for i, row in enumerate(s):
    new_str = ''
    for j, e in enumerate(row):
        new_str += count(j, i) if e=='.' else '
    print(new_str)

Solution (Python):
H,W = map(int,input().split())
board =list()
ans =list()
count = 0
for y in range(H):
  board.append (input())
  ans.append(["
for y in range(H):
  for x in range(W):
    if board[y][x] ==".":
      count = 0
      for y2 in range(y-1,y+2):
        for x2 in range(x-1,x+2):
          if y2<0 or y2>= H:
            continue
          elif x2<0 or x2>= W:
            continue
          else:
            if board[y2][x2] == "
              count += 1
      ans[y][x] = count
for y in range(H):
  for x in range(W):
    print(ans[y][x],end="")
  print()

Solution (Python):
H, W = map(int, input().split())
lst = [input() for _ in range(H)]
res = [[0 for i in range(W)] for j in range(H)]
x = [-1, -1, -1, 0, 0, 1, 1, 1]
y = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
    for j in range(W):
        if lst[i][j] == '
            res[i][j] = '
            for k in range(8):
                xk = i + x[k]
                yk = j + y[k]
                if 0<= xk < H and 0 <= yk < W:
                    if res[xk][yk] != '
                        res[xk][yk] += 1
for row in res:
    for i in row:
        print(i, end='')
    print('')

Solution (Python):
H,W=map(int,input().split())
S=['.']*(W+2)
i=0
while i<H:
    S+=['.']+list(input())+['.']
    i+=1
S+=['.']*(W+2)

j=W+3
while j<=(W+2)*(H+1)-2:
    count=0
    if S[j]!='
        if S[j-(W+3)]=='
            count+=1
        if S[j-(W+2)]=='
            count+=1
        if S[j-(W+1)]=='
            count+=1
        if S[j-1]=='
            count+=1
        if S[j+1]=='
            count+=1
        if S[j+(W+1)]=='
            count+=1
        if S[j+(W+2)]=='
            count+=1
        if S[j+(W+3)]=='
            count+=1
        S[j]=str(count)
    j+=1

k=1
while k<=H:
    print(''.join(S[k*(W+2)+1:k*(W+2)+1+W]))
    k+=1

Solution (Python):
H, W = map(int, input().split())
mapp = (['.' * (W + 2)]
        + ['.%s.' % input() for _ in range(H)]
        + ['.' * (W + 2)])

dh = [-1, -1, -1, 0, 0, 1, 1, 1]
dw = [-1, 0, 1, -1, 1, -1, 0, 1]
for h in range(1, H + 1):
    line = ''
    for w in range(1, W + 1):
        if mapp[h][w] == '
            line += '
            continue

        line += str(sum(mapp[h + dhi][w + dwi] == '
    print(line)

Solution (Python):
H, W = map(int, input().split())
mapp = ['.' * (W + 2)]
for _ in range(H):
    mapp += ['.%s.' % input()]
mapp += ['.' * (W + 2)]

dh = [-1, -1, -1, 0, 0, 1, 1, 1]
dw = [-1, 0, 1, -1, 1, -1, 0, 1]
for h in range(1, H + 1):
    line = ''
    for w in range(1, W + 1):
        if mapp[h][w] == '
            line += '
            continue

        line += str(sum(mapp[h + dhi][w + dwi] == '
    print(line)

Solution (Python):
h, w = map(int, input().split())
a = []
a.append(' ' * (w + 2))
for i in range(h):
    a.append(' ' + input() + ' ')
a.append(' ' * (w + 2))
ans = [[None for _ in range(w)] for _ in range(h)]
for i in range(1, h + 1):
    for j in range(1, w + 1):
        if a[i][j] == '
            ans[i - 1][j - 1] = '
        elif a[i][j] == '.':
            ans[i - 1][j - 1] = str(''.join(a[k][l] for k in range(i - 1, i + 2) for l in range(j - 1, j + 2)).count('
for value in ans:
    print(''.join(value))

Solution (Python):
def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    dx_dy = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, -1], [1, 1], [-1, -1], [-1, 1]]
    ans = []
    for nx in range(H):
        l = ''
        for ny in range(W):
            if S[nx][ny] == '.':
                cnt = 0
                for i in range(8):
                    x, y = nx + dx_dy[i][0], ny + dx_dy[i][1]
                    if 0  <= x < H and 0 <= y < W:
                        if S[x][y] == '
                            cnt += 1
                l += str(cnt)
            else:
                l += '
        ans.append(l)
    for a in ans:
        print(a)
                            
    
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    H, W = map(int, input().split())
    S = [list(input()) for _ in range(H)]
    dx_dy = [[1, 0], [-1, 0], [0, 1], [0, -1], [1, -1], [1, 1], [-1, -1], [-1, 1]]
    ans = []
    for nx in range(H):
        l = ''
        for ny in range(W):
            if S[nx][ny] == '.':
                cnt = 0
                for i in range(8):
                    x, y = nx + dx_dy[i][0], ny + dx_dy[i][1]
                    if 0  <= x < H and 0 <= y < W:
                        if S[x][y] == '
                            cnt += 1
                l += str(cnt)
            else:
                l += '
        ans.append(l)
    for a in ans:
        print(a)
                            
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

h,w = map(int,input().split())
g = []
for i in range(h):
    g.append(input())
c = [[0 for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        if g[i][j] == "
            if j < w - 1:
                c[i][j+1] += 1
            if j>0:
                c[i][j-1] += 1
            if i < h-1:
                c[i+1][j] += 1
            if i > 0:
                c[i-1][j] += 1
            if i>0 and j>0:
                c[i-1][j-1] += 1
            if i > 0 and j < w -1:
                c[i-1][j+1] += 1
            if i<h-1 and j>0:
                c[i+1][j-1] += 1
            if i < h-1 and j < w - 1:
                c[i+1][j+1] +=1
        if g[i][j] == ".":
            continue
for i in range(h):
    ans = ""
    for j in range(w):
        if g[i][j] == "
            ans += "
        else:
            ans += str(c[i][j])
    print(ans)

Solution (Python):
from itertools import product
H, W = list(map(int, input().split()))
S = [input() for _ in range(H)]
ans = S[:]
ans = [list(a) for a in ans]
S = ['.' * (W + 2)] + ['.' + s + '.' for s in S] + ['.' * (W + 2)]
for h, w in product(range(1, H + 1), range(1, W + 1)):
    if S[h][w] == '
        continue
    count = 0
    for i, j in product(range(h - 1, h + 2), range(w - 1, w + 2)):
        if (i == h) and (j == w):
            continue
        if S[i][j] == '
            count += 1
    ans[h - 1][w - 1] = str(count)
ans = [''.join(a) for a in ans]
[print(a) for a in ans]

Solution (Python):
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
      
    ans = 0
      
    for tips in range(8):
      ny = y + dy[tips]
      nx = x + dx[tips]
        
      if ny < 0 or ny >= h or nx < 0 or nx >= w:
        continue
      
      if field[ny][nx] == '
        ans += 1
        
    field[y][x] = str(ans)
    
for y in range(h):
  print("".join(field[y]))

Solution (Python):
H, W = map(int, input().split())
lst = [input() for _ in range(H)]
res = [[0 for i in range(W)] for j in range(H)]
x = [-1, -1, -1, 0, 0, 1, 1, 1]
y = [-1, 0, 1, -1, 1, -1, 0, 1]

for i in range(H):
    for j in range(W):
        if lst[i][j] == '
            res[i][j] = '
            for k in range(8):
                xk = i + x[k]
                yk = j + y[k]
                if 0<= xk < H and 0 <= yk < W:
                    if res[xk][yk] != '
                        res[xk][yk] += 1
for row in res:
    for i in row:
        print(i, end='')
    print('')

Solution (Python):
import sys
input = sys.stdin.readline


def read():
    H, W = map(int, input().strip().split())
    S = [['x' for j in range(W+2)] for i in range(H+2)]
    for i in range(H):
        s = input().strip()
        for j in range(W):
            S[i+1][j+1] = s[j]
    return H, W, S


def put_number(y, x, S):
    if S[y][x] == '
        return
    count = 0
    for dy, dx in [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]:
        ny, nx = dy + y, dx + x
        if S[ny][nx] == '
            count += 1
    S[y][x] = str(count)


def solve(H, W, S):
    for i in range(1, H+1):
        for j in range(1, W+1):
            put_number(i, j, S)
    for i in range(1, H+1):
        print(''.join(S[i][1:W+1]))


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

kekka = [["

for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=="
      pass
    else:
      cnt = 0
      for x in range(-1, 1+1):
        for y in range(-1, 1+1):
          if (x!=0 or y!=0) and s[i+x][j+y]=="
            cnt += 1
      kekka[i-1][j-1] = str(cnt)
for i in range(h):
  print("".join(kekka[i]))

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

kekka = [["

for i in range(1, h+1):
  for j in range(1, w+1):
    if s[i][j]=="
      pass
    else:
      cnt = 0
      cnt += [0,1][s[i-1][j-1]=="
      cnt += [0,1][s[i-1][j]=="
      cnt += [0,1][s[i-1][j+1]=="
      cnt += [0,1][s[i][j-1]=="
      cnt += [0,1][s[i][j+1]=="
      cnt += [0,1][s[i+1][j-1]=="
      cnt += [0,1][s[i+1][j]=="
      cnt += [0,1][s[i+1][j+1]=="
      kekka[i-1][j-1] = str(cnt)
for i in range(h):
  print("".join(kekka[i]))

Solution (Python):
h, w = map(int, input().split())
s = [["a"]*(w + 2)]

for i in range(h):
    a = list("a" + input() + "a")
    s.append(a)

s.append(["a"]*(w + 2))

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i][j] == ".":
            cnt = s[i - 1][j - 1: j + 2].count("
            cnt += s[i][j - 1: j + 2].count("
            cnt += s[i + 1][j - 1: j + 2].count("
            s[i][j] = str(cnt)

for i in range(1, h + 1):
    print("".join(s[i][1:-1]))

Solution (Python):

'''
こういうのきれいに速く実装できるようになりたい…
'''
def search_mine(i,j,glid):
    cnt = 0
    if glid[i][j] == "
        return "
    if glid[i-1][j-1] == "
        cnt += 1
    if glid[i-1][j] == "
        cnt += 1
    if glid[i-1][j+1] == "
        cnt += 1
    if glid[i][j-1] == "
        cnt += 1
    if glid[i][j] == "
        cnt += 1
    if glid[i][j+1] == "
        cnt += 1
    if glid[i+1][j-1] == "
        cnt += 1
    if glid[i+1][j] == "
        cnt += 1
    if glid[i+1][j+1] == "
        cnt += 1
    return cnt


H,W = map(int,input().split())
glid =[]
glid.append(list("."*(W+2)))
for i in range(H):
    glid.append(list("." + input() + "."))
glid.append(list("."*(W+2)))



for i in range(1,H+1):
    for j in range(1,W+1):
        glid[i][j] = search_mine(i,j,glid)
for i,v in enumerate(glid):
    if i == 0 or i == H+1:
        continue
    ans = ""
    for j,c in enumerate(v):
        if j == 0 or j == W+1:
            continue
        ans += str(c)
    print(ans)

Solution (Python):
H,W=map(int,input().split())
mat=[[False]*(W+2)]
for i in range(H):
  array=[x=="
  mat.append([False]+array+[False])
mat.append([False]*(W+2))


for i in range(1,H+1):
  ans_list=[]
  for j in range(1,W+1):
    if mat[i][j]:
      ans_list.append("
    else:
      ans=0
      for x in range(-1,2):
        for y in range(-1,2):
          if mat[i+x][j+y]:
            ans+=1
      ans_list.append(str(ans))
  print("".join(ans_list))

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

ANS = [['
nxt = [-1,0,1]
for r in range(H):
  for c in range(W):
    cnt = 0
    if S[r][c] == '
      continue
    for x,y in itertools.product(nxt,repeat=2):
      if x == 0 and y == 0:
        continue
      if not 0 <= r+x < H:
        continue
      if not 0 <= c+y < W:
        continue
      if S[r+x][c+y] == '
        cnt += 1
    ANS[r][c] = cnt

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

Solution (Python):
def myAnswer(H:int,W:int,S:list) -> None:
   XY = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
   index = []
   ans = [[0]*W for _ in range(H)]
   for i in range(H):
      for j in range(W):
         if(S[i][j] == "
            ans[i][j] = "
            index.append([i,j])
   for i,j in index:
      for x,y in XY:
         if(0 > (j + x) or (j + x) >= W or 0 > (i + y) or (i + y) >= H):
            continue
         elif(ans[y+i][x+j]== "
            continue
         else:
            ans[y + i][x + j] += 1
   for a in ans:
      string = ""
      for s in a:
         string += str(s)
      print(string)

def modelAnswer(H:int,W:int,S:list) -> int:
   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(S[i][j] == "

         num = 0
         for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]
            if(ni < 0 or H <= ni):continue
            if(nj < 0 or W <= nj):continue
            if(S[ni][nj] == "
         
         S[i][j] = str(num )
   for s in S:
      ans = ""
      for a in s:
         ans += a
      print(ans)
def main():
   H,W = map(int,input().split())
   S = [list(input()) for _ in range(H)]
   myAnswer(H,W,S[:])
if __name__ == '__main__':
   main()

Solution (Python):
H, W = map(int, input().split())
S = []
for i 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 i in range(H):
  for j in range(W):
    if S[i][j] == '
      continue
    cnt = 0
    for n in range(8):
      ni = i + dx[n]
      nj = j + dy[n]
      if ni < 0 or H <= ni or nj < 0 or W <= nj:
        continue
      if S[ni][nj] == '
        cnt += 1
    S[i][j] = str(cnt)


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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    H, W = map(int, readline().split())
    S = list(list(input()) for _ in range(H))
    ans = [[0]*W for _ in range(H)]
    dx = [-1,0,1]
    dy = [-1,0,1]
    for i in range(H):
        for j in range(W):
            if S[i][j]=='
                ans[i][j] = '
                for x in dx:
                    for y in dy:
                        ny = i + y
                        nx = j + x
                        if 0<=nx<W and 0<=ny<H and ans[ny][nx]!='
                            ans[ny][nx] += 1
    for i in range(H):
        print(''.join(map(str,ans[i])))



if __name__ == '__main__':
    main()

Solution (Python):
h,w=map(int,input().split())
s=[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 s[i][j]=="
            continue

        num=0
        for d in range(8):
            ni=i+dy[d]
            nj=j+dx[d]

            if ni<0 or h<=ni:
                continue
            if nj<0 or w <=nj:
                continue
            if s[ni][nj]=="
                num+=1

        list1=list(s[i])
        list1[j]=str(num)
        s[i]="".join(list1)
for i in range(h):
    print(s[i])

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

for i in range(H):
    S[i] = '.' + S[i] + '.'
S = ['.' * (W + 2)] + S + ['.' * (W + 2)]

for i in range(1,H+1):
    s = ''
    for j in range(1, W+1):
        if S[i][j] == '
            s += '
        else:
            ser = 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]
            s += str(ser.count('
    print(s)

Solution (Python):
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] == '
            ans[i][j] = '
            if i > 0 and j > 0 and ans[i-1][j-1] != '
                ans[i-1][j-1] += 1
            if i > 0 and ans[i-1][j] != '
                ans[i-1][j] += 1
            if i > 0 and j < w-1 and ans[i-1][j+1] != '
                ans[i-1][j+1] += 1
            if j > 0 and ans[i][j-1] != '
                ans[i][j-1] += 1
            if j < w-1 and ans[i][j+1] != '
                ans[i][j+1] += 1
            if i < h-1 and j > 0 and ans[i+1][j-1] != '
                ans[i+1][j-1] += 1
            if i < h-1 and ans[i+1][j] != '
                ans[i+1][j] += 1
            if i < h-1 and j < w-1 and ans[i+1][j+1] != '
                ans[i+1][j+1] += 1

for k in range(h):
    for m in range(w):
        print(str(ans[k][m]), end='')
    print()

Solution (Python):

import numpy as np

H, W = map(int, input().split())
s = np.zeros((H+2, W+2))

for i in range(H):
    S = input()
    for j, v in enumerate(S):
        if v == '
            s[i+1, j+1] = 1

for i in range(H):
    ans = ''
    for j in range(W):
        if s[i+1, j+1] == 1:
            ans += '
        else:
            ans += str(np.sum(s[i:i+3, j:j+3], dtype=np.int))
    print(ans)

Solution (Python):
def count(s,n,i,j):
    if s[i][j]=='
        n[i][j]='
    else:
        count=0
        for a in range(3):
            for b in range(3):
                if (0<=i-1+a<=h-1)and(0<=j-1+b<=w-1):
                    if s[i-1+a][j-1+b]=='
                        count+=1
        n[i][j]=str(count)


h,w=map(int,input().split())
s=[]
for i in range(h):
    s.append(list(input()))
n=s
for i in range(h):
    for j in range(w):
        count(s,n,i,j)
for i in range(h):
    print(''.join(s[i]))

Solution (Python):
h,w = map(int, input().split())
l = [None]*h
for i in range(h):
    l[i] = list(input())

dd=[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]
def check(i,j):
    ret=0
    for d in dd:
        x,y=d
        if 0<=i+x<h and 0<=j+y<w:
            if l[i+x][j+y]=="
                ret+=1
    return ret

for i in range(h):
    row=[]
    for j in range(w):
        if l[i][j]=="
            row.append("
        else:
            ret = check(i,j)
            row.append(ret)
    print(*row,sep="")

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

num=[[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):
    count=0
    if mine2[i][j]=='.':
      for a in range(-1,2):
        for b in range(-1,2):
          if mine2[i+a][j+b]=='
            count+=1
    num[i-1][j-1]=count
for i in range(h):
  for j in range(w):
    if mine[i][j]=='
      print('
    else:
      print(num[i][j],end='')
  print()

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

vector = [[1,0],[0,1],[-1,0],[0,-1],[1,1],[-1,-1],[1,-1],[-1,1]]

for y in range(H):
    tmp = ""
    for x in range(W):
        
        if S[y][x] == "
            tmp += "
        else:
            cnt = 0
            for s,t in vector:
                dx = x + s
                dy = y + t
                if 0 <= dx <= W-1 and 0 <= dy <= H-1:
                    if S[dy][dx] == "
                        cnt += 1
            tmp += str(cnt)
    print(tmp)

Solution (Python):
data = input().split(' ')
h = int(data[0])
w = int(data[1])
s_mat = list()
for _ in range(h):
    s_mat.append(input())

num_mat = [[0 for _ in range(w)] for _ in range(h)]
for hh in range(h):
    for ww in range(w):
        if s_mat[hh][ww] != '
            continue
        
        if hh + 1 <= h - 1 and ww + 1 <= w - 1:
            num_mat[hh + 1][ww + 1] += 1
        if hh + 1 <= h - 1:
            num_mat[hh + 1][ww] += 1
        if hh + 1 <= h - 1 and ww - 1 >= 0:
            num_mat[hh + 1][ww - 1] += 1
        if ww + 1 <= w - 1:
            num_mat[hh][ww + 1] += 1
        if ww - 1 >= 0:
            num_mat[hh][ww - 1] += 1
        if hh - 1 >= 0 and ww + 1 <= w - 1:
            num_mat[hh - 1][ww + 1] += 1
        if hh - 1 >= 0:
            num_mat[hh - 1][ww] += 1
        if hh - 1 >= 0 and ww - 1 >= 0:
            num_mat[hh - 1][ww - 1] += 1

def get_cell(hh, ww):
    if s_mat[hh][ww] == '
        return '
    else:
        return str(num_mat[hh][ww])
for hh in range(h):
    print(''.join([get_cell(hh, ww) for ww in range(w)]))

Solution (Python):
h, w = map(int, input().split())
s = [input() for _ in range(h)]
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]
for i in range(h):
    s[i] = list(s[i])
    l = ""
    for j in range(w):
        if s[i][j] == "
            continue
        cnt = 0
        for k in range(8):
            ii = i + dx[k]
            jj = j + dy[k]
            if ii < 0 or ii > h - 1:
                continue
            elif jj < 0 or jj > w - 1:
                continue
            elif s[ii][jj] == "
                cnt += 1
        s[i][j] = str(cnt)
    l = "".join(s[i])
    print(l)

Solution (Python):
H,W=map(int,input().split())
S=[list(input()) for i in range(H)]
T=[[None]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            tmp=0
            for k in range(j-1,j+2):
                if 0<=i-1<=H-1 and 0<=k<=W-1 and S[i-1][k]=="
                    tmp+=1
                if 0<=i+1<=H-1 and 0<=k<=W-1 and S[i+1][k]=="
                    tmp+=1
                if 0<=i<=H-1 and 0<=k<=W-1 and k!=j and S[i][k]=="
                    tmp+=1
            T[i][j]=tmp
        else:
            T[i][j]="
for i in range(H):
    for j in range(W):
        print(T[i][j],end="")
    print("\n")

Solution (Python):
def func(x,y):
    global s,h,w
    d = [-1,0,1]
    rv = 0
    for i in d:
        for j in d:
            if i == 0 and j == 0:
                continue
            if x+i >= 0 and x+i < h and y+j >= 0 and y+j < w:
                if s[x+i][y+j] == "
                    rv += 1
    return str(rv)

h,w = map(int,input().split())
s = [input() for _ in range(h)]
for i in range(h):
    ans = ""
    for j in range(w):
        if s[i][j] == ".":
            ans += func(i,j)
        else:
            ans += "
    print(ans)

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