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: Runtime Error

Solution code:
hw=input().split()
h=int(hw[0])
w=int(hw[1])
mx=[1,1,0,-1,-1,-1,0,1]
my=[0,1,1,1,0,-1,-1,-1]
grid=[]
for t in range(h):
	row=list(input())
	grid.append(row)
for i in range(w):
	for j in range(h):
		if grid[j][i]==".":
			ctr=0
			for k in range(8):
				adx=i+mx[k]
				ady=j+my[k]
				if adx>=0 and adx<w and ady>=0 and ady<h:
					if grid[ady][adx]=="#":
						ctr+=1
			grid[j][i]=ctr
for l in range(h):
    print(''.join(grid[l]))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
int N,M;
char m[55][55];
int main(){
    scanf("%d%d",&N,&M);
    for(int i=0;i<N;i++){
        scanf("%s",m[i+1]+1);
    }
    for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
        int cnt=0;
            if(m[i][j]=='#')putchar('#');
            else {
                if(m[i+1][j]=='#')cnt++;
                if(m[i-1][j]=='#')cnt++;
                if(m[i][j+1]=='#')cnt++;
                if(m[i][j-1]=='#')cnt++;
                if(m[i+1][j+1]=='#')cnt++;
                if(m[i+1][j-1]=='#')cnt++;
                if(m[i-1][j+1]=='#')cnt++;
                if(m[i-1][j-1]=='#')cnt++;
                printf("%d",cnt);
            }
        }
            putchar('\n');
    }
    return 0;
}

Submission status: Runtime Error

Solution code:
H,W = (int(i) for i in input().split())

original = [input() for i in range(H)]
sujimaru = [[0 for i in range(W+2)for j in range(H+2)]]

for j in range(H):
    for i in range(W):
        if(original[i][j] == "#"):
            sujimaru[i][j] += 1
            sujimaru[i+1][j] += 1
            sujimaru[i+2][j] += 1
            sujimaru[i][j+1] += 1
            sujimaru[i+2][j+1] += 1
            sujimaru[i][j+2] += 1
            sujimaru[i+1][j+2] += 1
            sujimaru[i+2][j+2] += 1

d = [""]*H
for j in range(H):
    for i in range(W):
        if(original[j][i] == "."):
            d[j] += str(sujimaru[j+1][i+1])
        else:
            d[j] += "#"

for s in d:
    print(d)

Submission status: Runtime Error

Solution code:
H,W = (int(i) for i in input().split())

original = [input() for i in range(H)]
sujimaru = [[0 for i in range(W+2)for j in range(H+2)]]

for j in range(H):
    for i in range(W):
        if(original[i][j] == "#"):
            sujimaru[i][j] += 1
            sujimaru[i+1][j] += 1
            sujimaru[i+2][j] += 1
            sujimaru[i][j+1] += 1
            sujimaru[i+2][j+1] += 1
            sujimaru[i][j+2] += 1
            sujimaru[i+1][j+2] += 1
            sujimaru[i+2][j+2] += 1

d = [""]*H
for j in range(H):
    for i in range(W):
        if(original[j][i] == "."):
            d[j] += str(sujimaru[j+1][i+1])
        else:
            d[j] += "#"

for s in d:
    print(s)

Submission status: Runtime Error

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

for i in range(0, W):
    for j in range(0, H):
        bomb_num = 0
        if arr[i][j] == '#':
            ans[i] += '#'
        else:
            for k in (-1, 0, 1):
                for l in (-1, 0, 1):
                    if (-1<i+k<W) and (-1<j+l<H):
                        if arr[i+k][j+l] == '#':
                            bomb_num += 1
                        
            ans[i] += str(bomb_num)
            
for i in range(W):
        print(ans[i])

Submission status: Runtime Error

Solution code:
# ABC 075 B

# H, W = 4, 3
# S = "#.."\
#     "..#"\
#     ".#."\
#     ".#."

H, W = map(int, input().split())
S = ''
for _ in range(H):
    S += input()
    

def cell_of(x, y):
    return S[W*x + y]

def neighbor_indice_of(x, y):
    return [
        (x+m, y+n)
        for m in range(-1, 2)
        for n in range(-1, 2)
        if 0 <= x+m < H and 0 <= y+n < W
    ]

def neighbors_of(x, y):
    return [
        cell_of(i, j)
        for i, j in neighbor_indice(x, y)
    ]

def mine(x, y):
    cell = cell_of(x, y)
    if cell != '.':
        return cell
    return str(neighbors_of(x, y).count('#'))


for x in range(H):
    print(''.join([mine(x, y) for y in range(W)]))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
lis=[]
for _ in range(h):
  lis.append(list(input()))
for hi in range(h):
  for wi in range(w):
    if lis[hi][wi]==".":
      c=0
      for x,y in [[1,1],[1,0],[1,-1],[0,1],[0,-1],[-1,1],[-1,0],[-1,-1]]:
        try:
          if lis[hi+x][y+y]=="#":
            c+=1
        finally:
          pass
      lis[hi][wi]==string(c)
for hi in range(h):
  s=""
  for wi in range(w):
    s+=lis[hi][wi]
  print(s)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
lis = []
for i in range(n):
    a = list(input())
    for l in range(m):
        lis.append(a[l])
for t in range(15):
    if lis[t] == ".":
        cou = 0
        try:
            if lis[t-(m+1)] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t - m] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t -(m-1)] == "#":
              cou += 1
        except:
            cou = cou
        try:
            if lis[t -1] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t+1]== "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t +m] == "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t +(m-1)]== "#":
                cou += 1
        except:
            cou = cou
        try:
            if lis[t+(m+1)]== "#":
                cou += 1
        except:
            cou = cou
        lis[t] = cou

for p in range(n):
    li = []
    for h in range(m):
        li.append(lis[(p * m) + h])
    print("".join(map(str,li)))

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
a=""
for i in range(w+2):
    a=a+"a"
S=[a]
for i in range(h):
    S.append("a"+input()+"a")
S.append(a)
for i in range(1,h+1):
    for j in range(1,w+1):
        if S[i][j]==".":
            num=0
            for u in range(-1,2):
                for v in range(-1,2):
                    if S[i+u][j+v]=="#":
                        num+=1
            S[i]=S[i][:j-1]+str(num)+S[i][:j+1]
for i in range(1,h+1):
    print(S[i][1:w+1])

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
lis=[]
outp=[[0 for i in range(w)] for j in range(h)]
outs=[""]*h
for i in range(h):
    lis.append(input())
for i in range(h):
    for j in range(w):
        if(lis[i][j]=="#"):
            for k in range(-1,2):
                for l in range(-1,2):
                    if(i+k>=0 and i+k<w and j+l>=0 and j+l<h):
                        outp[i+k][j+l]+=1
for i in range(h):
    for j in range(w):
        if(lis[i][j]=="#"):
            outs[i]+="#"
        else:
            outs[i]+=str(outp[i][j])
for i in range(h):
    print(outs[i])

Submission status: Runtime Error

Solution code:
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):
        if S[i][j] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
elif H == 1:
    if S[0][0] == "#":
        if S[0][1]!="#": S[0][1]+= 1
    if S[0][H]== "#":
        if S[0][H-1]!="#": S[0][H-1]+= 1
    for i in range(1,H-1):
        if S[0][i-1]!="#": S[0][i-1]+= 1
        if S[0][i+1]!="#": S[0][i+1]+= 1
    
    for i in range(H):
        print("".join(map(str,S[i][:])))


elif W == 1:
    if S[0][0] == "#":
        if S[0][1]!="#": S[0][1]+= 1
    if S[W][0]== "#":
        if S[W-1][0]!="#": S[W-1][0]+= 1
    for i in range(1,W-1):
        if S[i-1][0]!="#": S[i-1][0]+= 1
        if S[i+1][0]!="#": S[i+1][0]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

    for i in range(1,H-1):
        if S[i][W-1] == "#":
            if S[i-1][W-1] !="#": S[i-1][W-1]+= 1
            if S[i+1][W-1] !="#": S[i+1][W-1]+= 1
            if S[i][W-2] !="#": S[i][W-2]+= 1
            if S[i-1][W-2] !="#": S[i-1][W-2]+= 1
            if S[i+1][W-2] !="#": S[i+1][W-2]+= 1

    for i in range(1,W-1):
        if S[0][i] == "#":
            if S[0][i-1] !="#": S[0][i-1]+= 1
            if S[0][i+1] !="#": S[0][i+1]+= 1
            if S[1][i] !="#": S[1][i]+= 1
            if S[1][i-1] !="#": S[1][i-1]+= 1
            if S[1][i+1] !="#": S[1][i+1]+= 1

    for i in range(1,W-1):
        if S[H-1][i] == "#":
            if S[H-1][i-1] !="#": S[H-1][i-1]+= 1
            if S[H-1][i+1] !="#": S[H-1][i+1]+= 1
            if S[H-2][i] !="#": S[H-2][i]+= 1
            if S[H-2][i-1] !="#": S[H-2][i-1]+= 1
            if S[H-2][i+1] !="#": S[H-2][i+1]+= 1

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

Submission status: Runtime Error

Solution code:
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):
        if S[i][j] == ".":
            S[i][j] = 0
if H == 1 and W == 1:
    print(S[0][0])
else:
    #4隅の処理
    if S[0][0] == "#":
        if S[1][0] !="#": S[1][0]+= 1
        if S[0][1] !="#": S[0][1]+= 1
        if S[1][1] !="#": S[1][1]+= 1
    if S[0][W-1] == "#":
        if S[0][W-2] !="#": S[0][W-2]+= 1
        if S[1][W-2] !="#": S[1][W-2]+= 1
        if S[1][W-1] !="#": S[1][W-1]+= 1
    if S[H-1][0] == "#":
        if S[H-2][0] !="#": S[H-2][0]+= 1
        if S[H-1][1] !="#": S[H-1][1]+= 1
        if S[H-2][1] !="#": S[H-2][1]+= 1
    if S[H-1][W-1] == "#":
        if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
        if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
        if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
    #端の処理
    for i in range(1,H-1):
        if S[i][0] == "#":
            if S[i-1][0] !="#": S[i-1][0]+= 1
            if S[i+1][0] !="#": S[i+1][0]+= 1
            if S[i][1] !="#": S[i][1]+= 1
            if S[i-1][1] !="#": S[i-1][1]+= 1
            if S[i+1][1] !="#": S[i+1][1]+= 1

    for i in range(1,H-1):
        if S[i][W-1] == "#":
            if S[i-1][W-1] !="#": S[i-1][W-1]+= 1
            if S[i+1][W-1] !="#": S[i+1][W-1]+= 1
            if S[i][W-2] !="#": S[i][W-2]+= 1
            if S[i-1][W-2] !="#": S[i-1][W-2]+= 1
            if S[i+1][W-2] !="#": S[i+1][W-2]+= 1

    for i in range(1,W-1):
        if S[0][i] == "#":
            if S[0][i-1] !="#": S[0][i-1]+= 1
            if S[0][i+1] !="#": S[0][i+1]+= 1
            if S[1][i] !="#": S[1][i]+= 1
            if S[1][i-1] !="#": S[1][i-1]+= 1
            if S[1][i+1] !="#": S[1][i+1]+= 1

    for i in range(1,W-1):
        if S[H-1][i] == "#":
            if S[H-1][i-1] !="#": S[H-1][i-1]+= 1
            if S[H-1][i+1] !="#": S[H-1][i+1]+= 1
            if S[H-2][i] !="#": S[H-2][i]+= 1
            if S[H-2][i-1] !="#": S[H-2][i-1]+= 1
            if S[H-2][i+1] !="#": S[H-2][i+1]+= 1

    for i in range(1,H-1):
        for j in range(1,W-1):   
            if S[i][j] == "#":
                if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
                if S[i-1][j] !="#": S[i-1][j]+= 1
                if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
                if S[i][j-1] !="#": S[i][j-1]+= 1
                if S[i][j+1] !="#": S[i][j+1]+= 1
                if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
                if S[i+1][j] !="#": S[i+1][j]+= 1
                if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
    for i in range(H):
        print("".join(map(str,S[i][:])))

Submission status: Runtime Error

Solution code:
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):
        if S[i][j] == ".":
            S[i][j] = 0
#4隅の処理
if S[0][0] == "#":
    if S[1][0] !="#": S[1][0]+= 1
    if S[0][1] !="#": S[0][1]+= 1
    if S[1][1] !="#": S[1][1]+= 1
if S[0][W-1] == "#":
    if S[0][W-2] !="#": S[0][W-2]+= 1
    if S[1][W-2] !="#": S[1][W-2]+= 1
    if S[1][W-1] !="#": S[1][W-1]+= 1
if S[H-1][0] == "#":
    if S[H-2][0] !="#": S[H-2][0]+= 1
    if S[H-1][1] !="#": S[H-1][1]+= 1
    if S[H-2][1] !="#": S[H-2][1]+= 1
if S[H-1][W-1] == "#":
    if S[H-2][W-1] !="#": S[H-2][W-1]+= 1
    if S[H-2][W-2] !="#": S[H-2][W-2]+= 1
    if S[H-1][W-2] !="#": S[H-1][W-2]+= 1
#端の処理
for i in range(1,H-1):
    if S[i][0] == "#":
        if S[i-1][0] !="#": S[i-1][0]+= 1
        if S[i+1][0] !="#": S[i+1][0]+= 1
        if S[i][1] !="#": S[i][1]+= 1
        if S[i-1][1] !="#": S[i-1][1]+= 1
        if S[i+1][1] !="#": S[i+1][1]+= 1

for i in range(1,H-1):
    if S[i][W-1] == "#":
        if S[i-1][W-1] !="#": S[i-1][W-1]+= 1
        if S[i+1][W-1] !="#": S[i+1][W-1]+= 1
        if S[i][W-2] !="#": S[i][W-2]+= 1
        if S[i-1][W-2] !="#": S[i-1][W-2]+= 1
        if S[i+1][W-2] !="#": S[i+1][W-2]+= 1

for i in range(1,W-1):
    if S[0][i] == "#":
        if S[0][i-1] !="#": S[0][i-1]+= 1
        if S[0][i+1] !="#": S[0][i+1]+= 1
        if S[1][i] !="#": S[1][i]+= 1
        if S[1][i-1] !="#": S[1][i-1]+= 1
        if S[1][i+1] !="#": S[1][i+1]+= 1

for i in range(1,W-1):
    if S[H-1][i] == "#":
        if S[H-1][i-1] !="#": S[H-1][i-1]+= 1
        if S[H-1][i+1] !="#": S[H-1][i+1]+= 1
        if S[H-2][i] !="#": S[H-2][i]+= 1
        if S[H-2][i-1] !="#": S[H-2][i-1]+= 1
        if S[H-2][i+1] !="#": S[H-2][i+1]+= 1

for i in range(1,H-1):
    for j in range(1,W-1):   
        if S[i][j] == "#":
            if S[i-1][j-1] !="#": S[i-1][j-1]+= 1
            if S[i-1][j] !="#": S[i-1][j]+= 1
            if S[i-1][j+1] !="#": S[i-1][j+1]+= 1
            if S[i][j-1] !="#": S[i][j-1]+= 1
            if S[i][j+1] !="#": S[i][j+1]+= 1
            if S[i+1][j-1] !="#": S[i+1][j-1]+= 1
            if S[i+1][j] !="#": S[i+1][j]+= 1
            if S[i+1][j+1] !="#": S[i+1][j+1]+= 1
for i in range(H):
    print("".join(map(str,S[i][:])))

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
ss = [list(input()) for i in range(h)]
s = list(map(lambda x: list(map(lambda y: 1 if y == '#' else 0, x)), ss))

def calc(x, y):
    if (x+1) == 1 and (y+1) == 1:
        return s[x+1][y] + s[x][y+1] + s[x+1][y+1]
    elif (x+1) == h and (y+1) == 1:
        return s[x-1][y] + s[x][y+1] + s[x-1][y+1]
    elif (x+1) == 1 and (y+1) == w:
        return s[x+1][y] + s[x][y-1] + s[x+1][y-1]
    elif (x+1) == h and (y+1) == w:
        return s[x-1][y] + s[x][y-1] + s[x-1][y-1]
    elif (x+1) == 1:
        return s[x+1][y] + s[x][y-1] + s[x][y+1] + s[x+1][y+1] + s[x+1][y-1]
    elif (x+1) == h:
        return s[x-1][y] + s[1][y-1] + s[x][y+1] + s[x-1][y+1] + s[x-1][y-1]
    elif (y+1) == 1:
        return s[x+1][y] + s[x-1][y] + s[x][y+1] + s[x-1][y+1] + s[x+1][y+1]
    elif (y+1) == w:
        return s[x+1][y] + s[x-1][y] + s[x][y-1] + s[x-1][y-1] + s[x+1][y-1]
    else:
        return s[x-1][y] + s[x+1][y] + s[x][y-1] + s[x][y+1] + s[x+1][y+1] + s[x+1][y-1] + s[x-1][y+1] + s[x-1][y-1]

a = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        if ss[i][j] == '#': a[i][j] = '#'
        else: a[i][j] = calc(i, j)

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

Submission status: Runtime Error

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

def search(h,w):
        if s[h][w]=='#':
                return '#'
        count=0
        for i in range(-1,2):
                for j in range(-1,2):
                        if (i!=0 or j!=0) and 0<=h+i and h+i<H and 0<=w+j and w+j<W:                                if s[h+i][w+j]=='#':
                                        count+=1
        return count

for h in range(H):
        for w in range(W):
                print(search(h,w),end='')
        print()

Submission status: Runtime Error

Solution code:
h, w = map(int, raw_input().split())
table = list()
table.append([0]*(w+2))
for _ in xrange(h):
    table.append([0]+list(map(lambda c:0 if c == "." else c, list(raw_input())))+[0])
table.append([0]*(w+2))

xy = [0, 1, 0, -1, 1, 1, -1, -1, 0]
for i in xrange(1, h+1):
    for j in xrange(1, w+1):
        if table[i][j] == "#":
            for k in xrange(8):
                table[i+xy[k]][j+xy[k+1]] += 1


for i in xrange(1, h+1):
    print "".join(map(str, table[i][1:-1]))

Submission status: Runtime Error

Solution code:
print(2/0)

#  random shit  random shit  random shit  random shit  random shit  random shit

Submission status: Runtime Error

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

for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      continue
    count=0
    
    for dx in [-1,0,1]:
      for dy in [-1,0,1]:
        if 0<=i+dy<h and 0<=j+dx<w and s[i+dy][j+dx]=="#":
          count+=1
    s[i][j]=str(count)
    
for a in s:
  print(''.join(a))

Submission status: Runtime Error

Solution code:
 coding:utf-8
H, W = list(map(int, input().split()))
s = []
ans = []
for i in range(H):
    s.append(input())
    ans.append([])
for i in range(H):
    for j in range(W):
        if(s[i][j] == "#"):
            ans[i].append("#")
            continue
        ans[i].append(0)

for i in range(H):
	for j in range(W):
		if s[i][j] == '#':
			for h in range(-1,2):
				for k in range(-1,2):
					if h == 0 and k == 0:
						continue
					if (i == 0 and h == -1) or (i == H -1 and h == 1) or (j == 0 and k == -1) or (j == W-1 and k == 1):
						continue

					if (s[i+h][j+k]) != '#':
 						ans[i+h][j+k] += 1  
			ans[i][j] = '#'
		
print (ans)

Submission status: Runtime Error

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

for i in range(H):
  a=input()
  a=list(a)
  if(i!=H-1):
   a.pop()   
  A.append(a)

for i in range(H):
  for j in range(W):
    if(A[i][j]=="."):
     A[i][j]=0

for i in range(H):
  if((i!=0)and(i!=H-1)):
   for j in range(W):
     if((j!=0)and(j!=W-1)):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

for i in range(H):
   for j in range(W): 
      if( (i==0)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
      if( (j==0)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (j==W-1)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
          
      if( (i==0)and(j==0) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==0)and(j==W-1) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==0) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==W-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

for i in range(H):
 for j in range(W): 
  A[i][j]=str(A[i][j])

X=""
for i in range(H):
 for j in A[i]:
  X += j
 print(X)
 X=""

Submission status: Runtime Error

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

for i in range(H):
  a=input()
  a=list(a)
  if(i!=H-1):
   a.pop()   
  A.append(a)

for i in range(H):
  for j in range(W):
    if(A[i][j]=="."):
     A[i][j]=0

for i in range(H):
  if((i!=0)and(i!=H-1)):
   for j in range(W):
     if((j!=0)and(j!=W-1)):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

for i in range(H):
   for j in range(W): 
      if( (i==0)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
      if( (j==0)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (j==W-1)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
          
      if( (i==0)and(j==0) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==0)and(j==W-1) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==0) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==W-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

print(A)

Submission status: Runtime Error

Solution code:
H, W = list(map(int, input().split()))
S = []
for _ in range(H):
    S.append(list(input()))
    
for w in range(W):
    if S[0][w] == ".":
        S[0][w] = str(S[0][max(0,w-1):min(W, w+2)].count("#")
                    +S[1][max(0,w-1):min(W, w+2)].count("#"))

for h in range(1, H-1):
    for w in range(W):
        if S[h][w] == ".":
            S[h][w] = str(S[h-1][max(0,w-1):min(W, w+2)].count("#")
                        +S[h][max(0,w-1):min(W, w+2)].count("#")
                        +S[h+1][max(0,w-1):min(W, w+2)].count("#"))
            
for w in range(W):
        if S[H-1][w] == ".":
            S[H-1][w] = str(S[H-2][max(0,w-1):min(W, w+2)].count("#")
                        +S[H-1][max(0,w-1):min(W, w+2)].count("#"))
            
for h in range(H):
    print("".join(S[h]))

Submission status: Runtime Error

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

a=[['.' for x in range(w+2)]]

for i in range(h):
    b=list(str(input()))
    b= ['.']+b+['.']
    a.append(b)

footer=['.' for x in range(w+2)]
a.append(footer)
    

for i in range(1,w+1):
    ans=''
    for j in range(1,h+1):
        count=0
        if a[i][j]=='#':
            ans += '#'
        else:
            if a[i-1][j-1]=='#':
                count += 1
            if a[i][j-1]=='#':
                count += 1
            if a[i+1][j-1]=='#':
                count += 1
            if a[i-1][j]=='#':
                count += 1
            if a[i+1][j]=='#':
                count += 1
            if a[i-1][j+1]=='#':
                count += 1
            if a[i][j+1]=='#':
                count += 1
            if a[i+1][j+1]=='#':
                count += 1

            ans += str(count)

    print(ans)

Submission status: Runtime Error

Solution code:
HM = input().split()
H, M = [int(HM[i]) for i in range(len(HM))]
#print(H,M)
S = [input().split() for i in range(H)]
#print(S)
#print(S[0][1])
S2 = S

def minesweeper():
    for i in range(H):
        for j in range(M):
            if S[i][j] == '.':
                C = count(i,j)
                S2[i][j] = C
    for i in range(H):
        for k in range(M-1):
            Shai = S2[i][k]
            print(Shai, end=" ")
        print(S2[i][M-1])
    return S2


def count(x,y):
    c = 0
    #print("x={}".format(x))
    if (x >= 1 and y >= 1):
        if S[x-1][y-1] == '#':
            c += 1
            #print('1')
    if (x >= 1):
        if S[x-1][y] == "#":
            c += 1
            #print('2')
    if x >= 1 and y <= M - 2:
        if S[x-1][y+1] == "#":
            c += 1
            #print("x={}".format(x))
            #print('3')
    if (y >= 1):
        if S[x][y-1] == "#":
            c += 1
            #print('4')
    if (y <= M - 2):
        if S[x][y+1] == "#":
            c += 1
            #print('5')
    if (x <= H-2 and y >= 1):
        if S[x+1][y-1] == "#":
            c += 1
            #print('6')
    if (x <= H-2):
        if S[x+1][y] == "#":
            c += 1
            #print('7')
    if (x <= H-2 and y <= M-2):
        if S[x+1][y+1] == "#":
            c += 1
            #print('8')
    return c

minesweeper()

Submission status: Runtime Error

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



m = []

for i in range(h):
    l = list(input())
    m.append([ '#' if i=='#' else 0 for i in l])

for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            for u in range(3):
                for v in range(3):
                    if u==1 and v==1:
                        pass
                    else:
                        try:
                            m[i + u -1][j + v-1] += 1
                        except:
                            pass





for l in r:
    s = map(str, l)
    print(''.join(s))

Submission status: Runtime Error

Solution code:
def check(t):
    return 1 if t is '#' else 0
    
def main(H, W, S):
    for i in range(H):
        for j in range(W):
            if S[i][j] is '.':
                S[i][j] = check(S[i][max(j-1, 0)]) + check(S[i][min(j+1, W-1)]) + check(S[max(i-1, 0)][j]) + check(S[min(i+1, H-1)][j])
    return S


H, W = map(int, input().split())
S = [input().split() for i in range(H)]
print(main(H, W, S))

Submission status: Runtime Error

Solution code:
def check(t):
    return 1 if t is '#' else 0
    
def main(H, W, S):
    for i in range(H):
        for j in range(W):
            if S[i][j] is '.':
                S[i][j] = check(S[i][max(j-1, 0)]) + check(S[i][min(j+1, W-1)]) + check(S[max(i-1, 0)][j]) + check(S[min(i+1, H-1)][j])
    return S


H, W = map(int, input().split())
S = [input().split() for i in H]
print(main(H, W, S))

Submission status: Runtime Error

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

ans = []
for row in src:
    ans.append(list(row))

dxy = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
for y in range(H):
    for x in range(W):
        if src[y][x] == '#':
            continue
        c = 0
        for dx,  dy in dxy:
            if x + dx < 0 or x + dx > W - 1 or y + dy < 0 or y + dy > H - 1:
                continue
            if src[y + dy][x + dx] == '#':
                c += 1
        ans[y][x] = c


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

Submission status: Runtime Error

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

for i in range(H):
    for j in range(W):
        if S[i][j]=='#':

            continue
for h in range(H):
    l = ''
    for w in range(W):
        if S[h][w] == '#':
            l += '#'
        else:
            x = 0
            for dh in [ -1, 0, 1]:
                for dw in [-1, 0, 1]:
                    if 0 <= h + dh < H and 0 <= w + dw < W and S[h+dh][w+dw] == '#':
                        x += 1
            l += str(x)
    print(l)

Submission status: Runtime Error

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

table = [i for i in range(h)]
for i in range(w):
    table[i] = input()

for i in range(h):
    for j in range(w):
        if table[i][j] == '.':
            c = 0
            for k in [i-1, i, i+1]:
                for l in [j-1, j, j+1]:
                    if 0 <= k < h and 0 <= l < w:
                        c += int(table[k][l] == '#')
        table[i][j] = str(c)

print(table)