Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.  

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).  

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.  

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.  

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310



Sample input:
3 5
.....
.#.#.
.....


Sample output:
11211
1#2#1
11211

Submission status: Accepted

Solution code:
import sys

def solve(h, w, board):
    rows = []
    for i in range(h):
        row = []
        for k in range(w):
            row.append(0)
        rows.append(row)

    for i in range(h):
        for k in range(w):
            if board[i][k] == 1:
                rows[i][k] =  -1
                for dx in [-1, 0, 1]:
                    for dy in [-1,0,1]:
                        if  (not (dx == 0 and dy ==0)) and 0 <= i + dy and i + dy < h and 0 <= k + dx and k + dx < w:
                            if board[i + dy][k + dx] != 1:
                                rows[i + dy][k + dx] += 1


    return rows

def main():
    s1 = sys.stdin.readline()
    lst = s1.split(' ')
    h = int(lst[0])
    w = int(lst[1])

    rows = []
    for i in range(h):
        s = sys.stdin.readline()
        row = []
        for ch in s:
            if ch == '#':
                row.append(1)
            else:
                row.append(0)

        rows.append(row)

    ans = solve(h, w, rows)
    for ans_nums in ans:
        for num in ans_nums:
            if num == -1:
                sys.stdout.write('#')
            else:
                sys.stdout.write(str(num))
        print()
    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

Solution code:
def B_Minesweeper(H, W, S):
    def is_bound(x, y, H, W):
        if 0 <= x and x <= W - 1 and 0 <= y and y <= H - 1:
            return True
        else:
            return False

    ans = [[None for i in range(W)] for j in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i][j] == '.':
                ret = 0
                for k in [[i - 1, j - 1], [i - 1, j], [i - 1, j + 1], [i, j - 1], [i, j + 1], [i + 1, j - 1], [i + 1, j], [i + 1, j + 1]]:
                    y = k[0]
                    x = k[1]
                    if is_bound(x, y, H, W):
                        if S[y][x] == '#':
                            ret += 1
                ans[i][j] = str(ret)
            else:
                ans[i][j] = '#'
    for s in ans:
        print(''.join(s))
    return None
  
H,W = [int(i) for i in input().split()]
S = [[i for i in input()] for j in range(H)]
B_Minesweeper(H, W, S)

Submission status: Accepted

Solution code:
#! -*- coding:utf-8 -*-
import sys
h,w = map(int,input().split())
S = []
for i in range(h):
    S.append(str(input()))
arr = [[]*w]*h

for i in range(h):
    arr[i] = list(S[i])

if h == 1 and w == 1:
    if arr[0][0] == '#':
        print('#')
    else:
        print(0)
    sys.exit()
elif h == 1:
    for i in range(w):
        c = 0
        if arr[0][i] == '#':
            print('#',sep ='',end = '')
        elif i == 0:
            if arr[0][1] == '#':
                c += 1
            print(c,sep = '', end = '')
        elif i == w-1:
            if arr[0][-2] == '#':
                c += 1
            print(c,sep = '', end = '')
        else:
            if arr[0][i-1] == '#':
                c += 1
            if arr[0][i+1] == '#':
                c += 1
            print(c,sep = '',end = '')
    print('')
    sys.exit()
elif w == 1:
    for i in range(h):
        c = 0
        if arr[i][0] == '#':
            print('#')
        elif i == 0:
            if arr[1][0] == '#':
                c += 1
            print(c)
        elif i == h-1:
            if arr[-2][0] == '#':
                c += 1
            print(c)
        else:
            if arr[i-1][0] == '#':
                c += 1
            if arr[i+1][0] == '#':
                c += 1
            print(c)
    sys.exit()
    
for i in range(h):
    for j in range(w):
        if arr[i][j] == '#':
            print('#',sep = '', end = '')
        else:
            if i == 0 and j == 0:
                c = 0
                if arr[0][1] == '#':
                    c += 1
                if arr[1][0] == '#':
                    c += 1
                if arr[1][1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == 0 and j == w-1:
                c = 0
                if arr[0][-2] == '#':
                    c += 1
                if arr[1][-1] == '#':
                    c += 1
                if arr[1][-2] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == 0:
                c = 0
                if arr[i][j-1] == '#':
                    c+= 1
                if arr[i][j+1] == '#':
                    c +=1
                if arr[i+1][j-1] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i+1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1 and j == 0:
                c = 0
                if arr[-1][1] == '#':
                    c += 1
                if arr[-2][0] == '#':
                    c += 1
                if arr[-2][1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1 and j == w-1:
                c = 0
                if arr[-1][-2] == '#':
                    c += 1
                if arr[-2][-1] == '#':
                    c += 1
                if arr[-2][-2] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif i == h-1:
                c = 0
                if arr[i][j-1] == '#':
                    c+= 1
                if arr[i][j+1] == '#':
                    c +=1
                if arr[i-1][j-1] == '#':
                    c += 1
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i-1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif j == 0:
                c = 0
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i][j+1] == '#':
                    c += 1
                if arr[i-1][j+1] == '#':
                    c += 1
                if arr[i+1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            elif j == w-1:
                c = 0
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i][j-1] == '#':
                    c += 1
                if arr[i-1][j-1] == '#':
                    c += 1
                if arr[i+1][j-1] == '#':
                    c += 1
                print(c,sep = '', end = '')
            else:
                c = 0
                if arr[i-1][j-1] == '#':
                    c += 1
                if arr[i-1][j] == '#':
                    c += 1
                if arr[i-1][j+1] == '#':
                    c += 1
                if arr[i][j-1] == '#':
                    c += 1
                if arr[i][j+1] == '#':
                    c += 1
                if arr[i+1][j-1] == '#':
                    c += 1
                if arr[i+1][j] == '#':
                    c += 1
                if arr[i+1][j+1] == '#':
                    c += 1
                print(c,sep = '', end = '')
    print('')

Submission status: Accepted

Solution code:
import math,heapq,collections,sys,numpy
sys.setrecursionlimit(10**7)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

H,W = LI()
LS = []
LS.append("@"*(W+2))
for _ in range(H):
    LS.append("@" + S() + "@")
LS.append("@"*(W+2))

def count(i,j):
    c = 0
    for k in range(-1,2):
        for l in range(-1,2):
            if LS[i+k][j+l] == "#":
                c +=1
    return c

for i in range(1,H+1):
    for j in range(1,W+1):
        if LS[i][j] == ".":
            LS[i] = LS[i][0:j] + str(count(i,j)) +  LS[i][j+1:W+2]

for i in range(1,H+1):
    print(LS[i][1:W+1])

Submission status: Accepted

Solution code:
H, W = list(map(int,input().split()))
l=[]
for i in range(H):
    l.append(list(input()))
ans=[[0 for i in range(W)] for j in range(H)]
for i in range(H):
    for j in range(W):
        for dx, dy in [(1,0), (0, 1), (0 , -1 ), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]:
            try:
                if j+dx < 0 or i+dy< 0:
                    continue
                if l[i+dy][j+dx]=='#':
                    ans[i][j] += 1
            except:
                pass
for i in range(H):
    for j in range(W):
        if l[i][j]=='#':
            ans[i][j] = '#'
for i in range(H):
    for j in range(W):
        print(ans[i][j],end='')
    print()

Submission status: Accepted

Solution code:
h, w = [int(i) for i in input().split()]
a = []
a.append(['.'] * (w + 2))
for j in range(h):
    a.append(['.'] + list(input()) + ['.'])
a.append(['.'] * (w + 2))

for j in range(1, h + 1):
    for i in range(1, w + 1):
        if a[j][i] == '.':
            n = [
                a[j - 1][i - 1],
                a[j - 1][i],
                a[j - 1][i + 1],
                a[j][i - 1],
                a[j][i + 1],
                a[j + 1][i - 1],
                a[j + 1][i],
                a[j + 1][i + 1],
            ].count('#')
            print(n, end='')
        else:
            print('#', end='')
    print()

Submission status: Accepted

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 = 0
      for line in F[u:d]:
          n += line[l:r].count("#")
      F[i][j] = str(n)

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

Submission status: Accepted

Solution code:
# coding: utf-8
range = xrange
H, W = map(int, raw_input().split(' '))
S = [[c for c in raw_input()] for _ in range(H)]

answer = []
for h in range(H):
    row = []
    for w in range(W):
        if S[h][w] == '.':
            row.append([
                S[i][j]
                for i in range(max(h - 1, 0), min(H, h + 2))
                for j in range(max(w - 1, 0), min(W, w + 2))
            ].count('#'))
        else:
            row.append(S[h][w])
    answer.append(row)

for r in answer:
    print u''.join(map(unicode, r))

Submission status: Accepted

Solution code:
h,w = map(int, raw_input().split())
c = '.'*(w+2)
mass = []
mass.append(c)
for _ in xrange(h):
    s = '.' + raw_input() + '.'
    mass.append(s)
mass.append(c)

ans = [['.' for _ in xrange(w)] for _ in xrange(h)]
for i in xrange(h):
    for j in xrange(w):
        if mass[i+1][j+1] == '#':
            ans[i][j] = '#'
        else:
            count = 0
            for a in [-1, 0, 1]:
                for b in [-1, 0, 1]:
                    if mass[i+1+a][j+1+b] == '#': count += 1
            ans[i][j] = str(count)

for x in ans:
    print "".join(x)

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

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

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

	for val,j in zip(S,range(W)):
		if val == '#':
			bomb.append((i,j))
		else:
			S[j] = 0

	mat.append(S)


def rep (i,j,mat,H,W):
	for k in [i-1,i,i+1]:
		for l in [j-1,j,j+1]:

			if (k < 0) or (k > (H-1)) or (l < 0) or (l > (W-1)):
				continue

			if mat[k][l] == '#':
				continue
			else:
				mat[k][l] += 1

for t in bomb:
	rep(t[0],t[1],mat,H,W)

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

Submission status: Accepted

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

M = [["#" for i in range(w)] for j in range(h)]

for i in range(h):
  for j in range(w):
    count=0
    if s[i][j]==".":
      
      if j-1>=0:
           if s[i][j-1]=="#":
               count=count+1
      if j<w-1:
           if s[i][j+1]=="#":
               count=count+1            
      if i-1>=0:
           if s[i-1][j]=="#":
               count=count+1    
      if i<h-1:
           if s[i+1][j]=="#":
               count=count+1                
              
      if j-1>=0 and i-1>=0:
           if s[i-1][j-1]=="#":
               count=count+1
      if j<w-1 and i-1>=0:
           if s[i-1][j+1]=="#":
               count=count+1            
      if i<h-1 and j-1>=0:
           if s[i+1][j-1]=="#":
               count=count+1    
      if i<h-1 and j<w-1:
           if s[i+1][j+1]=="#":
               count=count+1                     
              
      M[i][j]=count

M_LIST=[]
for i in range(h):
    M_LIST.append("".join(map(str,M[i])))
    print(M_LIST[i])

Submission status: Accepted

Solution code:
a,b=[int(i) for i in input().split()]

x=list()
y=list()

for i in range(a):
    l=input()
    x.append(list(l))
    y.append(x[i][:b])

z=[["" for i in range(b)] for j in range(a)]
for i in range(a):
    for j in range(b):
        if y[i][j]=='#':
            z[i][j]='#'
        else:
            n=0
            if i+1<a:
                    if y[i+1][j]=="#":
                         n+=1
            if j+1<b:
                    if y[i][j+1]=="#":
                         n+=1
            if i+1<a and j+1<b:
                    if y[i+1][j+1]=="#":
                         n+=1
            if i-1>=0:
                    if y[i-1][j]=="#":
                         n+=1
            if j-1>=0:
                    if y[i][j-1]=="#":
                         n+=1
            if i-1>=0 and j-1>=0:
                    if y[i-1][j-1]=="#":
                         n+=1
            if i+1<a and j-1>=0:
                    if y[i+1][j-1]=="#":
                         n+=1
            if i-1>=0 and j+1<b:
                    if y[i-1][j+1]=="#":
                         n+=1
            z[i][j]=str(n)
        #print(i,j,z[i][j])
    print("".join(z[i]))

Submission status: Accepted

Solution code:
def main():
    h,w=[int(i) for i in input().split()]
    field=[]
    for i in range(h):
        field.append(input())

    for i in range(h):
        new_line=[]
        for j in range(w):
            ans=0
            if field[i][j]=='#':
                new_line.append('#')
                continue
            if i-1 >=0 and field[i-1][j]=='#':
                ans+=1
            if i+1 < h  and field[i+1][j]=='#':
                ans+=1
            if j-1 >=0 and field[i][j-1]=='#':
                ans+=1
            if j+1 < w and field[i][j+1]=='#':
                ans+=1
            if i-1 >= 0 and j-1 >= 0 and field[i-1][j-1]=='#':
                ans+=1
            if i-1 >= 0 and j+1 < w and field[i-1][j+1]=='#':
                ans+=1
            if i+1 < h and j-1 >= 0 and field[i+1][j-1]=='#':
                ans+=1
            if i+1 < h and j+1 < w  and field[i+1][j+1]=='#':
                ans+=1
            new_line.append(str(ans))
        print(''.join(new_line))

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
H,W=map(int,input().rstrip().split())
panel=[]
for i in range(H):
    panel.append([x for x in input().rstrip()])
    
#print(panel,flush=True)

def countBomb(y, x):
    cnt=0
    vec=[
        (0,-1),
        (1,-1),
        (1,0),
        (1,1),
        (0,1),
        (-1,1),
        (-1,0),
        (-1,-1)
    ]
    
    for tpl in vec:
        tmpX=x+int(tpl[0])
        tmpY=y+int(tpl[1])
        if(tmpX<0 or tmpY<0 or tmpX>=W or tmpY>=H):
            continue
        if(panel[tmpY][tmpX]=="#"):
            cnt+=1
    return cnt
            

result=[]
for i in range(H):
    row=[]
    for j in range(W):
        puts="#"
        if(panel[i][j]=="."):
            puts=countBomb(i,j)
        row.append(str(puts))
    #result.append(row)
    print("".join(row),flush=True)

Submission status: Accepted

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

T = [['' for j in range(W)] for i in range(H)]

for i, Ti in enumerate(T):
    for j, Tij in enumerate(Ti):
        if S[i][j] == '#':
            T[i][j] = '#'
        else:
            num = 0
            for di in [-1, 0, 1]:
                for dj in [-1, 0, 1]:
                    if 0 <= i + di < H and 0 <= j + dj < W:
                        if S[i + di][j + dj] == '#':
                            num += 1
            T[i][j] = str(num)

print('\n'.join([''.join(T[i]) for i in range(H)]))

Submission status: Accepted

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

m=['.']*((H+2)*(W+2))

for h in range(1,H+1):
    S=raw_input()
    i=0
    for w in range(1,W+1):
        m[h*(W+2)+w]=S[i]
        i+=1

r=[0]*(H*W)
i=0
for h in range(1,H+1):
    for w in range(1,W+1):
        p=h*(W+2)+w
        if m[p]=='#':
            r[i]='#'
        else:
            if m[p-(W+2)-1]=='#':
                r[i]+=1
            if m[p-(W+2)]=='#':
                r[i]+=1
            if m[p-(W+2)+1]=='#':
                r[i]+=1
            if m[p-1]=='#':
                r[i]+=1
            if m[p+1]=='#':
                r[i]+=1
            if m[p+(W+2)-1]=='#':
                r[i]+=1
            if m[p+(W+2)]=='#':
                r[i]+=1
            if m[p+(W+2)+1]=='#':
                r[i]+=1
        i+=1

r=map(str,r)
for h in range(H):
    print ''.join(r[h*W:h*W+W])

Submission status: Accepted

Solution code:
s = input().split(" ")
H = int(s[0])
W = int(s[1])
S = []
for i in range(H):
    S.append(input())
dx = [1, 1, 0, -1, -1, -1, 0, 1]
dy = [0, 1, 1, 1, 0, -1, -1, -1]
res = []
for i in range(H):
    res.append([])
    for j in range(W):
        res[i].append(0)
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        cnt = 0
        for k in range(8):
            tx = i + dx[k]
            ty = j + dy[k]
            if 0 <= tx and tx < H and 0 <= ty and ty < W and S[tx][ty] == "#":
                cnt += 1
        res[i][j] = cnt
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            print(S[i][j], end = '')
        else:
            print(res[i][j], end = '')
    print('')

Submission status: Accepted

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

for i in range(H):
    s = list(input())
    S.append(s)

for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            res[i][j] = '#'
            for x,y in [[j-1,i-1],[j,i-1],[j+1,i-1],[j-1,i],[j+1,i],[j-1,i+1],[j,i+1],[j+1,i+1]]:
                if 0 <= x and 0 <= y and x <= W-1 and y <= H-1:
                    if res[y][x] != '#':
                        res[y][x] += 1
for i in range(H):
    for j in range(W):
        res[i][j] = str(res[i][j])
for i in range(H):
    print("".join(res[i]))

Submission status: Accepted

Solution code:
def main():
    H, W = map(int, input().split())
    S = tuple(input() for _ in [0] * H)

    sq = ['0' * (W + 2)]
    apnd = sq.append
    for s in S:
        apnd('0' + s + '0')
    else:
        apnd('0' * (W + 2))

    for i in range(1, H + 1):
        for j in range(1, W + 1):
            if sq[i][j] == '.':
                n = 0
                if sq[i-1][j-1] in '#': n += 1
                if sq[i-1][j] in '#': n += 1
                if sq[i-1][j+1] in '#': n += 1
                if sq[i][j-1] in '#': n += 1
                if sq[i][j+1] in '#': n += 1
                if sq[i+1][j-1] in '#': n += 1
                if sq[i+1][j] in '#': n += 1
                if sq[i+1][j+1] in '#': n += 1
                print(n, end='')
            else:
                print(sq[i][j], end='')
        print()

main()

Submission status: Accepted

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]] == "#" and r+dr[i] > -1 and c+dc[i] > -1:
                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: Accepted

Solution code:
import numpy as np

H, W = (list(map(int, input().split())))
S = [[_ for _ in input()] for i in range(H)]
for i in range(H):
  for j in range(W):
    if(S[i][j] == "."):
      count = 0
      if(i - 1 in range(H)):
        if(j - 1 in range(W)):
          if(S[i-1][j-1] == "#"):
            count += 1
        if(j in range(W)):
          if(S[i- 1][j] == "#"):
            count += 1
        if(j + 1 in range(W)):
          if(S[i-1][j + 1] == "#"):
            count += 1
      if(i in range(H)):
        if(j - 1 in range(W)):
          if(S[i][j-1] == "#"):
            count += 1
        if(j in range(W)):
          if(S[i][j] == "#"):
            count += 1
        if(j + 1 in range(W)):
          if(S[i][j + 1] == "#"):
            count += 1
      if(i + 1 in range(H)):
        if(j - 1 in range(W)):
          if(S[i+1][j-1] == "#"):
            count += 1
        if(j in range(W)):
          if(S[i+1][j] == "#"):
            count += 1
        if(j + 1 in range(W)):
          if(S[i+1][j + 1] == "#"):
            count += 1
      S[i][j] = count
for i in range(H):
  out = ""
  for char in S[i]:
    out += str(char)
  print(out)

Submission status: Accepted

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

m = [None for i in range(h)]

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

for i in range(h):
    for j in range(w):
        if m[i][j] == ".":
            a = 0
            for ii in range(-1, 2, 1):
                for jj in range(-1, 2, 1):
                    if i+ii >= 0 and j+jj >= 0 and i+ii < h and j+jj <w:
                        a += (m[i+ii][j+jj] == "#")
            a = str(a)
            m[i] = m[i][:j] + a + m[i][j+1:]

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

Submission status: Accepted

Solution code:
# Minesweeper
h,w = list(map(int, input().split()))
s = []
for i in range(h):
    s.append(input())
    
ss = [[0]*(w +2) for i in range(h+2)]
#print(ss)

for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            ss[i+1][j+1]+=9
            ss[i][j]+=1
            ss[i][j+1]+=1
            ss[i][j+2]+=1
            ss[i+1][j]+=1
            ss[i+1][j+2]+=1
            ss[i+2][j]+=1
            ss[i+2][j+1]+=1
            ss[i+2][j+2]+=1

for i in range(h):
    l = ss[i+1][1:w+1]
    #print(l)
    l = [str(l[i]) if l[i]<9 else "#" for i in range(w)]
    print("".join(l))

Submission status: Accepted

Solution code:
def get_bomb_num(i, j):
    cnt = 0
    for p in range(-1,2):
        for s in range(-1,2):
            l = i + p
            m = j + s
            if (p == 0 and s == 0) or (l < 0) or (l >= H) or (m < 0) or (m >= W):
                continue
            if b[l][m] == '#':
                cnt += 1
    return cnt

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

for i in range(H):
    for j in range(W):
        if b[i][j] == '#':
            print('#', end ='')
        else:
            print(get_bomb_num(i,j), end='')
    print('')

Submission status: Accepted

Solution code:
board = []
ans = []
H, W = list(map(int, input().split()))

def get_mine_number(j, i):
    mine_num = 0
    for deltaj in [-1, 0, 1]:
        for deltai in [-1, 0, 1]:
            if not -1 < j + deltaj < H or not -1 < i + deltai < W:
                continue
            if deltaj == 0 and deltai == 0:
                continue
            if board[j+deltaj][i+deltai] == "#":
                mine_num += 1
    return str(mine_num)
        
        
    

def main():
    for i in range(H):
        board.append(input())
    for jind, line in enumerate(board):
        ansline = ""
        for iind, point in enumerate(line):
            if point == ".":
                ansline += get_mine_number(jind, iind)
            else:
                ansline += point
        ans.append(ansline)
    for l in ans:
        print(l)
    
main()

Submission status: Accepted

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

for h in range(H):
    for w in range(W):
        if S[h][w] == '.':
            left = False if w == 0 else True
            right = False if w == W - 1 else True
            up = False if h == 0 else True
            down = False if h == H - 1 else True

            count = 0
            count = count + 1 if left and up and S[h - 1][w - 1] == '#' else count
            count = count + 1 if up and S[h - 1][w] == '#' else count
            count = count + 1 if right and up and S[h - 1][w + 1] == '#' else count
            count = count + 1 if left and S[h][w - 1] == '#' else count
            count = count + 1 if right and S[h][w + 1] == '#' else count
            count = count + 1 if left and down and S[h + 1][w - 1] == '#' else count
            count = count + 1 if down and S[h + 1][w] == '#' else count
            count = count + 1 if right and down and S[h + 1][w + 1] == '#' else count

            print(str(count), end='')
        else:
            print("#", end='')
    print()

Submission status: Accepted

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()))
    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: Accepted

Solution code:
i, j = [int(x) for x in raw_input().split()]
l = []
for x in xrange(i):
	l.append(list(raw_input()))
o = []
for x in xrange(i):
	for y in xrange(j):
		if l[x][y] == '#':
			o.append('#')
			continue
		#is '.'
		temp = 0
		for d_x in xrange(-1, 2):
			for d_y in xrange(-1, 2):
				if x + d_x < 0 or x + d_x >= i or y + d_y < 0 or y + d_y >= j:
					continue
				try:
					if l[x + d_x][y + d_y] == '#':
						temp += 1
				except:
					continue
		s = 0
		o.append(str(temp))
	o.append('\n')
print ''.join(o[:-1])

Submission status: Accepted

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

grid = []

grid.append('.'*W+'..')
for i in range(H):
    inputs = input().strip()
    inputs = '.' + inputs + '.'
    grid.append(inputs)
grid.append('.'*W+'..')


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

ans = [[0 for x in range(W)] for y in range(H)]

for row in range(len(grid)-2):
    for col in range(W):
        if grid[row+1][col+1] == '#':
            ans[row][col] = '#'
        for i in range(8):
            if grid[row+1 + dy[i]][col+1 + dx[i]] == '#':
                if ans[row][col] != '#':
                    ans[row][col] += 1

for row in ans:
    print(''.join(map(lambda x: str(x), row)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

h, w = [int(i) for i in input().split()]
all_map = []
for _ in range(h):
    all_map.append([i for i in input()])
for i in range(h):
    for j in range(w):
        if all_map[i][j] == ".":
            counter = 0
            for c_height in range(max(0, i -1), min(h, i + 1 + 1)):
                for c_width in range(max(0, j -1), min(w, j + 1 + 1)):
                    if all_map[c_height][c_width] == "#":
                        counter += 1
            all_map[i][j] = counter
for item in all_map:
    print("".join([str(i) for i in item]))

Submission status: Accepted

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

Map = [['0']*W]*H

for i in range(H):
    Map[i] = list(input())
    
for i in range(H):
    for j in range(W):
        bomb = 0
        if Map[i][j] == '.':
            if i-1>=0 and  j-1>=0 and Map[i-1][j-1] == '#':
                bomb+=1
            if i-1>=0 and  Map[i-1][j] == '#':
                bomb+=1
            if i-1>=0 and  j+1<W and Map[i-1][j+1] == '#':
                bomb+=1
            if j+1<W and Map[i][j+1] == '#':
                bomb+=1
            if i+1<H and  j+1<W and Map[i+1][j+1] == '#':
                bomb+=1
            if i+1<H and Map[i+1][j] == '#':
                bomb+=1
            if i+1<H and  j-1>=0 and Map[i+1][j-1] == '#':
                bomb+=1
            if j-1>=0 and Map[i][j-1] == '#':
                bomb+=1
            Map[i][j] = str(bomb)
        
for i in range(H):
    print(''.join(Map[i]))

Submission status: Accepted

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


def solve(h_, w_):
    count = 0
    for h in range(max(0, h_ - 1), min(h_ + 2, H)):
        for w in range(max(0, w_ - 1), min(w_ + 2, W)):
            if S[h][w] is '#':
                count += 1
    return count


for n in range(H):
    ans = []
    for i in range(W):
        if S[n][i] is '#':
            ans.append('#')
        else:
            ans.append(str(solve(n, i)))
    print(''.join(ans))