Problem:


Score : 200 points

Problem Statement

You are given an H × W grid.

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

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

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

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

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

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

Print the strings after the process.  

Constraints

1 \leq H,W \leq 50

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

Input

Input is given from Standard Input in the following format:  

H W
S_1
:
S_H

Output

Print the H strings after the process.

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

Sample Input 1

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

Sample Output 1

11211
1#2#1
11211

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

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

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

Sample Input 2

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

Sample Output 2

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

It is possible that there is no empty square.  

Sample Input 3

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

Sample Output 3

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



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


Sample output:
11211
1#2#1
11211

Submission status: Wrong Answer

Solution code:
import sys
def main():
    in1 = input()
    nums, length = in1.split(' ')
    nums= int(nums)
    length = int(length)
    bombs = []
    # print(nums)
    #
    for i in range(nums):

        bombs.append(input())
    print(bombs)




    checked = [['']*length for i in range(nums)]
    for i in range(nums):
        for j in range(length):
            if bombs[i][j] == '#':
                checked[i][j] = '#'
            else:
                bombnum = 0
                if i != 0:
                    if j != 0:
                        if bombs[i-1][j-1] == '#':
                            bombnum += 1

                    if j != length -1:
                        if bombs[i-1][j+1] == '#':
                            bombnum += 1

                    if bombs[i-1][j] == '#':
                        bombnum += 1


                if j != 0:
                    if bombs[i][j-1] == '#':
                        bombnum += 1

                if j != length-1:
                    if bombs[i][j+1] == '#':
                        bombnum += 1


                if i != nums-1:
                    if j != 0:
                        if bombs[i+1][j-1] == '#':
                            bombnum += 1

                    if j != length -1:
                        if bombs[i+1][j+1] == '#':
                            bombnum += 1

                    if bombs[i+1][j] == '#':
                        bombnum += 1

                checked[i][j] = bombnum
    for i in checked:
        for j in i:
            print(j, end = '')
        print('')

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)
print(1)

Submission status: Wrong Answer

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

tmp = [[0 for i in range(W+2)] for j in range(H+2)]
for i in range(H):
    for j in range(W):
        if map[i][j]=='#':
            tmp[i][j]+=1
            tmp[i][j+1]+=1
            tmp[i][j+2]+=1
            tmp[i+1][j+2]+=1
            tmp[i+2][j+2]+=1
            tmp[i+2][j+1]+=1
            tmp[i+2][j]+=1
            tmp[i+1][j]+=1
            
for i in range(H):
    for j in range(W):
        if map[i][j]=='#':
            tmp[i+1][j+1]='#'
        print(str(tmp[i+1][j+1])+' ', end='')
    print('\n')

Submission status: Wrong Answer

Solution code:
import sys


def main():

    # Get Args
    args = _input_args()  # get arguments as an array from console/script parameters.

    # Call main Logic
    result = _main(args)

    # Output a result in a correct way.
    _output_result(result)


def _main(args):
    """Write Main Logic here for the contest.

    :param args: arguments
    :type args: list
    :return: result
    :rtype: depends on your logic.
    """
    # Extract arguments.
    lines = args
    H, W = int(lines[0].split()[0]), int(lines[0].split()[1])
    field_map = get_field_map(lines[1:])
    result_map = [[0 for col in range(W)] for row in range(H)]

    for h in range(H):
        for w in range(W):
            result_map[h][w] = sweep_perimeters(h, w, field_map, H, W)

    result_lines = convert_map_to_lines(result_map)

    # Return something.
    return result_lines


def sweep_perimeters(h, w, field_map, H, W):
    found_mine = 0

    if field_map[h][w] == '#':
        return '#'

    for i in range(h-1, h+1+1):
        for j in range(w-1, w+1+1):
            if (0 <= i <= H-1) and (0 <= j <= W-1):
                target = field_map[i][j]
                print(target)
                if target == '#':
                    found_mine += 1

    return found_mine


def get_field_map(lines):
    field_maps = []
    for line in lines:
        elements = list(line)
        field_maps.append(elements)
    return field_maps


def convert_map_to_lines(result_map):
    result_lines = []
    for row_array in result_map:
        stringfied_row = ''
        for value in row_array:
            value = str(value)
            stringfied_row += value
        result_lines.append(stringfied_row)

    return result_lines

def _input_args():
    # Comment-out appropriate pattern depends on subject.

    # arguments = sys.argv[1:]  # ptn1: get args from script parameters.
    # arguments = _input().split()  # ptn2: get args from 1 line console prompt with space separated.

    # for multi-line console input, use this.
    arguments = _get_args_from_multiple_lines(end_of_lines_char=[''], limit=10000000)

    # Cast elements If you need.
    # arguments = list(map(int, arguments))  # cast elements to int for example.

    return arguments  # This will be array.


def _input():
    # If Subject requires interactive input, use this and patch mock in unittest.
    return input()  # Change if necessary.


def _get_args_from_multiple_lines(end_of_lines_char=[''], limit=10000000):
    """Get arguments from multiple lines standard input.

    :param end_of_lines_char: Strings that indicate the end of lines.
    :type end_of_lines_char: list of str
    :param limit: If a number of the input line are certain, you can use this param to close prompt immediately.
    :type limit: int
    :return: args
    :rtype list of str
    """
    args = []
    for i in range(limit):
        try:
            arg = _input()
            if arg in end_of_lines_char:
                break
            args.append(arg)
        except EOFError:  # Supports EOF Style. (Very Rare case)
            break
    return args


def _output_result(result):
    # Comment-out appropriate output pattern depends on subject.

    for line in result:
        print('{}'.format(str(line)))  # Same as above, but more versatile.


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
arr=[]
arr.append(['.']*(w+2))
for _ in range(h):
    arr.append(list('.'+input()+'.'))
arr.append(['.']*(w+2))
ans=[]
for i in range(h):
    ans.append([])
    for j in range(w):
        tmpans=0
        if arr[i+1][j+1]=='#':
            tmpans='#'
        else:
            for hh in range(3):
                for ww in range(3):
                    if not(hh==1 and ww==1) and arr[i+hh][j+ww]=='#':
                        tmpans+=1
            tmpans=str(tmpans)
        ans[i].append(tmpans)

print(arr)
print(ans)
for line in ans:
    print(''.join(line))

Submission status: Wrong Answer

Solution code:
HM = input().split()
H, M = [int(HM[i]) for i in range(len(HM))]

S = [input() for i in range(H)]
print(S)
#print(T[0][1])
#S2 = list([S[i] for i in range(H)] for j in range(M))
S2 = []
for i in range(H):
    S2.append(list(S[i]))
print(S2)

def minesweeper():
    for i in range(H):
        for j in range(M):
            if S[i][j] == ".":
                c = count(i, j)
                #print(c)
                S2[i][j] = c
    for i in range(H):
        for j in range(M-1):
            print(S2[i][j], end="")
        print(S2[i][M-1])
    return 0


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

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 m:
    s = map(str, l)
    print(''.join(s))

Submission status: Wrong Answer

Solution code:
def main():
    H,W = map(int, input().split())
    field = []
    for i in range(H):
        buf=list(input())
        field.append(buf)

    for i in range(H):
        for j in range(W):
            if field[i][j]=="#":
                continue
            num = 0
            for k in range(-1,2):
                for l in range(-1,2):
                    if i+k==0 or i+k==H or j+l==0 or j+l==W:
                        continue
                    if field[i+k][j+l]=="#":
                        num = num +1
            field[i][j]=str(num)
    
    for i in range(H):
        print("".join(field[i]))
  
main()

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split(' '))
mine = []
mine_result = []
 
def count(i, j):
    try:
        if mine[i][j] == '#':
            return 1
    except:
        return None
 
for i in range(0, h):
    inp = list(input())
    mine.append(inp)
    mine_result.append(inp)
 
for i in range(0, h):
    for j in range(0, w):
        sum_ = 0
 
        # count
        sum_ = sum(list(filter(None, [count(i + 1, j), count(i - 1, j), count(i, j + 1), count(i, j - 1), count(i + 1, j + 1), count(i + 1, j - 1), count(i - 1, j + 1), count(i - 1, j - 1)])))
 
        if (mine[i][j] == '.'):
            mine_result[i][j] = sum_
        else:
            mine_result[i][j] = '#'
 
for i in range(0, h):
    for j in range(0, w):
        print(mine_result[i][j], end='')
    print()

Submission status: Wrong Answer

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

def count(i, j):
    try:
        if mine[i][j] == '#':
            return 1
    except:
        return None

for i in range(0, h):
    inp = list(input())
    mine.append(inp)
    mine_result.append(inp)

for i in range(0, h):
    for j in range(0, w):
        sum_ = 0

        # count
        sum_ = sum(list(filter(None, [count(i + 1, j), count(i - 1, j), count(i, j + 1), count(i, j - 1), count(i + 1, j + 1), count(i + 1, j - 1), count(i - 1, j + 1), count(i - 1, j - 1)])))

        if (mine[i][j] == '.'):
            mine_result[i][j] = sum_
        else:
            mine_result[i][j] = '#'

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

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split())
S = []
A = []
sharp_index = []
hh = []
IDX = []
for h in range(H):
    s_h = list(input())
    a_h = [0 for i in range(W)]
    if '#' in s_h:
        hh.append(h)
        indexes = np.array([i for i, x in enumerate(s_h) if x == '#'])
        IDX.append([h, indexes])
        idx_m1 = indexes - 1
        idx_p1 = indexes + 1
        idx_ud = np.r_[idx_m1, idx_p1, indexes]
        idx_ud = np.delete(idx_ud, np.where(idx_ud<0)[0])
        idx_ud = np.delete(idx_ud, np.where(idx_ud>=W)[0])
        for i in idx_ud:
            a_h[i] += 1
    S.append(s_h)
    A.append(a_h)
A = np.array(A)
hh = np.array(hh)

for hhh in hh:
    if 0 < hhh < H-1:
        A[hhh-1] += A[hhh]
        A[hhh+1] += A[hhh]
        
A = A.tolist()
for idx in IDX:
    for idx1 in idx[1]:
        A[idx[0]][idx1] = '#'
        
for a in A:
    b = ''
    for aa in a:
        b += str(aa)
    print(b)

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split())
S = []
A = []
sharp_index = []
hh = []
IDX = []
for h in range(H):
    s_h = list(input())
    a_h = [0 for i in range(W)]
    if '#' in s_h:
        hh.append(h)
        indexes = np.array([i for i, x in enumerate(s_h) if x == '#'])
        IDX.append([h, indexes])
        idx_m1 = indexes - 1
        idx_p1 = indexes + 1
        idx_ud = np.r_[idx_m1, idx_p1, indexes]
        idx_ud = np.delete(idx_ud, np.where(idx_ud<0)[0])
        idx_ud = np.delete(idx_ud, np.where(idx_ud>=W)[0])
        for i in idx_ud:
            a_h[i] += 1
    S.append(s_h)
    A.append(a_h)
A = np.array(A)
hh = np.array(hh)

for hhh in hh:
    if 0 < hhh < H-1:
        A[hhh-1] += A[hhh]
        A[hhh+1] += A[hhh]
        
A = A.tolist()
for idx in IDX:
    for idx1 in idx[1]:
        print(idx[0], idx1)
        A[idx[0]][idx1] = '#'
        
for a in A:
    b = ''
    for aa in a:
        b += str(aa)
    print(b)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import traceback


def main():
    def _check(y,x,h,w,_list,count):
        if x >= 0 and y >= 0 and x < w and y < h:
            if _list[y][x] == '#':
                count += 1

        return count


    h,w = map(int,raw_input().split())
    S = [list(raw_input()) for i in range(h)]
    ans = S

    for i,line in enumerate(S):
        for j,val in enumerate(line):
            count = 0
            if val == '.':
                count = _check(i-1,j-1,h,w,S,count)
                count = _check(i-1,j,h,w,S,count)
                count = _check(i-1,j+1,h,w,S,count)
                count = _check(i,j-1,h,w,S,count)
                count = _check(i,j,h,w,S,count)
                count = _check(i,j+1,h,w,S,count)
                count = _check(i+1,j-1,h,w,S,count)
                count = _check(i+1,j,h,w,S,count)
                count = _check(i+1,j+1,h,w,S,count)

                if count != 0:
                    ans[i][j] = str(count)

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

print main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
table = []
table.append(list("_" * (W + 2)))
for _ in range(H):
    table.append(list("_" + input() + "_"))
table.append(list("_" * (W + 2)))

for h in range(1, H + 1):
    for w in range(1, W + 1):
        if table[h][w] == "#":
            continue
        count = 0
        for _h in [-1, 0, +1]:
            for _w in [-1, 0, +1]:
                print(h + _h, w + _w)
                if table[h + _h][w + _w] == "#":
                    count += 1
        table[h][w] = str(count)

for h in range(1, H + 1):
    print("".join(table[h]).replace("_", ""))

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
S = []
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i] = S[i].replace(".", "0", 1)
            rec = 0
            for d in [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)]:
                nx, ny = i + d[0], j + d[1]
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                    rec += 1
            if rec > 0:
                S[i] = S[i].replace("0", str(rec), 1)

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

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
mass=[]
for _ in range(H):
    mass.append(list(input()))
for i in range(H):
    for j in range(W):
        if mass[i][j]=="#":
            print("#",end="")
        else:
            counter = 0
            for k in range(-1,2,1):
                for l in range(-1,2,1):
                    if k==0 and l==0:
                        continue
                    try:
                        if mass[i+k][j+l]=="#":
                            counter+=1
                    except:
                        pass
            print(counter,end="")
    print()

Submission status: Wrong Answer

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

offset_h = (-1,-1,-1,0,0,1,1,1)
offset_w = (-1,0,1,-1,1,-1,0,1)
for h in range(H):
  for w in range(W):
    if S[h][w] == "#":
      print("#", end="")
    else:
      bomb = 0
      for i,j in zip(offset_h, offset_w):
        if h+i >= 0 and h+i < H and w+j >=0 and w+j < W and S[h+i][w+j]==".":
          bomb += 1
            
      print(bomb, end="")
     
  print("")

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys

# スペース区切りの整数の入力
H, W = map(int, raw_input().split())

S = []
for i in range(0, H):
	# 文字列の入力
	S.append(raw_input())

ans = [["" for i in range(W)] for j in range(H)]

for i in range(0, H):
	for j in range(0, W):
		if S[i][j] == "#":
			ans[i][j] = "#"
		else:
			count = 0

			# i-1
			if i-1 > 0:
				if j-1 > 0:
					if S[i-1][j-1] == "#":
						count+=1
				if S[i-1][j] == "#":
					count+=1
				if j+1 < W:
					if S[i-1][j+1] == "#":
						count+=1
			# i
			if j-1 > 0:
				if S[i][j-1] == "#":
					count+=1
			if j+1 < W:
				if S[i][j+1] == "#":
					count+=1
			# i+1
			if i+1 < H:
				if j-1 > 0:
					if S[i+1][j-1] == "#":
						count+=1
				if S[i+1][j] == "#":
					count+=1
				if j+1 < W:
					if S[i+1][j+1] == "#":
						count+=1
			ans[i][j] = str(count)

# 出力
for i in range(0, H):
	for j in range(0, W):
		sys.stdout.write(ans[i][j])
	print

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys

# スペース区切りの整数の入力
H, W = map(int, raw_input().split())

S = []
for i in range(0, H):
	# 文字列の入力
	S.append(raw_input())

ans = [["" for i in range(W)] for j in range(H)]

for i in range(0, H):
	for j in range(0, W):
		if S[i][j] == "#":
			ans[i][j] = "#"
		else:
			count = 0

			# i-1
			if i-1 > 0:
				if j-1 > 0:
					if S[i-1][j-1] == "#":
						count+=1
				if S[i-1][j] == "#":
					count+=1
				if j+1 < W:
					if S[i-1][j+1] == "#":
						count+=1
			# i
			if j-1 > 0:
				if S[i][j-1] == "#":
					count+=1
			if j+1 > W:
				if S[i][j+1] == "#":
					count+=1
			# i+1
			if i+1 > H:
				if j-1 > 0:
					if S[i+1][j-1] == "#":
						count+=1
				if S[i+1][j] == "#":
					count+=1
				if j+1 < W:
					if S[i+1][j+1] == "#":
						count+=1
			ans[i][j] = str(count)

# 出力
for i in range(0, H):
	for j in range(0, W):
		sys.stdout.write(ans[i][j])
	print

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys

H, W = map(int, raw_input().split())

S = []
for i in range(0, H):
	S.append(raw_input())

ans = [["" for i in range(W)] for j in range(H)]

for i in range(0, H):
	for j in range(0, W):
		if S[i][j] == "#":
			ans[i][j] = "#"
		else:
			count = 0

			# i-1
			if i-1 > 0:
				if j-1 > 0:
					if S[i-1][j-1] == "#":
						count+=1
				if S[i-1][j] == "#":
					count+=1
				if j+1 < W:
					if S[i-1][j+1] == "#":
						count+=1
			# i
			if j-1 > 0:
				if S[i][j-1] == "#":
					count+=1
			if j+1 < W:
				if S[i][j+1] == "#":
					count+=1
			# i+1
			if i+1 < H:
				if j-1 > 0:
					if S[i+1][j-1] == "#":
						count+=1
				if S[i+1][j] == "#":
					count+=1
				if j+1 < W:
					if S[i+1][j+1] == "#":
						count+=1
			ans[i][j] = str(count)

for i in range(0, H):
	for j in range(0, W):
		sys.stdout.write(ans[i][j])
	print

Submission status: Wrong Answer

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

field = []

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

for i in range(h):
	for j in range(w):
		if field[i][j] == ".":
			hmin = max(i-1, 0)
			hmax = min(i+1, h-1)
			wmin = max(j-1, 0)
			wmax = min(j+1, w-1)
			count = 0
			for m in range(hmin, hmax+1):
				for n in range(wmin, wmax+1):
					if field[m][n] == "#":
						count+=1

			print (count)
			# field[i][j] = count
			field[i] = field[i][0:j]+ str(count) + field[i][j+1:]


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

Submission status: Wrong Answer

Solution code:
H,W=list(map(int,raw_input().split()))
a = []
for i in range(H):
    a.append(list(raw_input()))
#print a

cnt=0
for i in range(H):
	for j in range(W):
		#print i,j
		if i-1>-1 and j-1>-1 and a[i-1][j-1]=="#":cnt+=1
		if a[i-1][j]=="#":cnt+=1
		if j+1<W and a[i-1][j+1]=="#":cnt+=1
		if j+1<W and a[i][j+1]=="#":cnt+=1
		if a[i][j-1]=="#":cnt+=1
		if i+1<H and  a[i+1][j-1]=="#":cnt+=1
		if i+1<H and a[i+1][j]=="#":cnt+=1
		if i+1<H and j+1<W and a[i+1][j+1]=="#":cnt+=1
		
		#print i,j,cnt
		if a[i][j]==".":
			a[i][j]=cnt
		cnt=0
			


for i in range(H):
	maped_num = map(str, a[i])
	s = ''.join(maped_num)
	print s

Submission status: Wrong Answer

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

S = [[]]

for h in range(H):
    S.append([])
    S[-1] = list(input())

del S[0]

for i in range(H):
    for j in range(W):
        if S[i][j] == '.': S[i][j] = 0
        if S[i][j] == '#': S[i][j] = -100
            
for i in range(H):
    for j in range(W):
        if S[i][j] < 0:
            
            if i != 0:
            
                try: S[i-1][j] += 1
                except: x=1

                try: S[i-1][j-1] += 1
                except: x=1

                try: S[i-1][j+1] += 1
                except: x=1
                    
            if j != 0:
                try: S[i][j-1] += 1
                except: x=1

                try: S[i+1][j-1] += 1
                except: x=1

                
            try: S[i+1][j] += 1
            except: x=1
            
            try: S[i][j+1] += 1
            except: x=1
            
            try: S[i+1][j+1] += 1
            except: x=1
            
            
for i in range(H):
    for j in range(W):
        if S[i][j] < 0: S[i][j] = '#'

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

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s = []
n = []
for i in range(H):
    s.append(input())
    n.append([])
    for j in range(W):
        if s[i][j] == ".":
            n[i].append(0)
        else:
            n[i].append(-1)

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

            matrix[i][j] = str(num_bombs)

    for i in range(H):
        for j in range(W):
            sys.stdout.write(matrix[i][j])
        print ""

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s = []
n = []
for i in range(H):
    s.append(input())
    n.append([])
    for j in range(W):
        if s[i][j] == ".":
            n[i].append(0)
        else:
            n[i].append(-1)

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

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
s = []
n = []
for i in range(H):
    s.append(input())
    n.append([])
    for j in range(W):
        if s[i][j] == ".":
            n[i].append(0)
        else:
            n[i].append(-1)

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

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
S=[]
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i] = S[i].replace(".","0",1)
            rec = 0
            for d in [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]:
                nx,ny = i+d[0],j+d[1]
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                    rec += 1
            S[i] = S[i].replace("0",str(rec), 1)

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

Submission status: Wrong Answer

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

S = [[]]

for h in range(H):
    S.append([])
    S[-1] = list(input())

del S[0]

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

for i in range(H):
    for j in range(W):
        if S[i][j] < 0: 
            try:
                S[i-1][j] += 1
            except:
                S=S
            try:
                S[i][j-1] += 1
            except:
                S=S
            try:
                S[i+1][j] += 1
            except:
                S=S
            try:
                S[i][j+1] += 1
            except:
                S=S

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

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

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
S=[]
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i] = S[i].replace(".","0",1)
            rec = 0
            for d in [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]:
                nx,ny = i+d[0],j+d[1]
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                    rec += 1
            S[i] = S[i].replace("0",str(rec),1)

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

Submission status: Wrong Answer

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

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

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

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
S=[]
for i in range(H):
    S.append(input())
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            S[i] = S[i].replace(".","0",1)
            rec = 0
            for d in [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]:
                nx,ny = i+d[0],j+d[1]
                if 0 <= nx < H and 0 <= ny < W and S[nx][ny] == "#":
                    rec += 1
            S[i] = S[i].replace("0",str(rec))

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

Submission status: Wrong Answer

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