class Point: def __init__ (self, x, y): self.x = x self.y = y def nextPoints(self, x_max, y_max): nexts = [] if self.x > 0: nexts.append(Point(self.x - 1, self.y)) if self.x < x_max: nexts.append(Point(self.x + 1, self.y)) if self.y > 0: nexts.append(Point(self.x, self.y - 1)) if self.y < y_max: nexts.append(Point(self.x, self.y + 1)) return nexts HW = input() if HW != '': H, W = map(int, HW.split(' ')) gy, gx = map(lambda i: int(i) -1 , input().split(' ')) grid = [] for y in range(H): grid.append(list(map(int, input().split(' ')))) grid_org = tuple([tuple(i) for i in grid]) Q = int(input()) debug_print = False elif HW == '': H = 10 W = 10 gy = 2 - 1 gx = 9 - 1 Q = 1 debug_print = True print ('input start x, y, and k') grid_org = ((9, 9, 9, 9, 9, 9, 9, 9, 9, 9), (9, 9, 9, 9, 9, 9, 9, 9, 1, 9), (9, 9, 9, 9, 9, 9, 9, 9, 1, 9), (9, 9, 9, 9, 9, 9, 9, 9, 1, 9), (9, 9, 9, 9, 9, 9, 9, 9, 1, 9), (9, 9, 9, 9, 9, 9, 9, 9, 1, 9), (9, 9, 9, 9, 9, 9, 9, 9, 1, 9), (9, 9, 0, 9, 9, 9, 9, 9, 1, 9), (9, 9, 1, 9, 9, 9, 9, 9, 1, 9), (9, 9, 1, 1, 1, 1, 1, 1, 1, 9)) result = [] for que in range(Q): sy, sx, k = map(int, input().split()) sy -= 1 sx -= 1 grid = [] for i in range(H): grid.append(list(map(lambda x: x + k**2, grid_org[i]))) count = grid[sy][sx] grid[sy][sx] = 0 if debug_print: print() print('k = ' + str(k)) print('startNum = ' + str(grid[sy][sx])) print ('count = ' + str(count)) while grid[gy][gx] != 0: buffer = [list(i) for i in grid] for y in range(H): for x in range(W): if grid[y][x] <= 0: for p in Point(x,y).nextPoints(W -1, H -1): buffer[p.y][p.x] = grid[p.y][p.x] - 1 count += 1 if debug_print: for y in range(H): for x in range(W): color = '\033[37m' char = str(buffer[y][x]) if y == sy and x == sx: color = '\033[33m' char = 'S' elif y == gy and x == gx: color = '\033[31m' elif buffer[y][x] <= 0: color = '\033[34m' elif buffer[y][x] != grid[y][x]: color = '\033[32m' print(color, end = '') print(char.rjust(5), end = '') print() print('\033[0m') print('count = ' + str(count)) input() grid = tuple([tuple(i) for i in buffer]) result.append(count) for i in result: print(i)