Madison is a little girl who is fond of toys. Her friend Mason works in a toy manufacturing factory . Mason has a 2D board $\mbox{A}$ of size $H\times W$ with $\mbox{H}$ rows and $\mbox{W}$ columns. The board is divided into cells of size $1\times1$ with each cell indicated by its coordinate $(i,j)$. The cell $(i,j)$  has an integer $A_{ij}$ written on it. To create the toy Mason stacks $A_{ij}$ number of cubes of size $1\times1\times1$ on the cell $(i,j)$. 

Given the description of the board showing the values of $A_{ij}$ and that the price of the toy is equal to the 3d surface area find the price of the toy. 

Input Format

The first line contains two space-separated integers $\mbox{H}$ and $\mbox{W}$ the height and the width of the board respectively.

The next  $\mbox{H}$ lines contains $\mbox{W}$ space separated integers. The $j^{th}$ integer in $i^{\mbox{th}}$ line denotes $A_{ij}$.

Constraints

$1\leq H,W\leq100$
$1\le A_{i,j}\le100$

Output Format

Print the required answer, i.e the price of the toy, in one line.

Sample Input 0
1 1
1

Sample Output 0
6

Explanation 0

The surface area of $1\times1\times1$ cube is 6.

Sample Input 1
3 3
1 3 4
2 2 3
1 2 4

Sample Output 1
60

Explanation 1

The object is rotated so the front row matches column 1 of the input, heights 1, 2, and 1.  

The front face is 1 + 2 + 1 = 4 units in area.   
The top is 3 units.
The sides are 4 units.   
None of the rear faces are exposed.  
The underside is 3 units.  

The front row contributes 4 + 3 + 4 + 3 = 14 units to the surface area.

Difficulty: MEDIUM
Tags: Implementation

Starter code:

Solutions:
import sys

def surfaceArea(A):
	ans = 0
	for i in range(len(A)):
		for j in range(len(A[0])):
			for (dx, dy) in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
				nx = j + dx
				ny = i + dy
				if 0 <= nx < len(A[0]) and 0 <= ny < len(A):
					if nx >= j and ny >= i:
						ans += abs(A[i][j] - A[ny][nx])
				else:
					ans += A[i][j]
	return ans
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result + H * W + H * W)


import sys

def surfaceArea(A):
	area = 0
	for y in range(H):
		for x in range(W):
			h = A[y][x]
			area += 2 + 4 * h
			if x > 0:
				hl = A[y][x - 1]
				area -= min(hl, h)
			if x < W - 1:
				hr = A[y][x + 1]
				area -= min(hr, h)
			if y > 0:
				hu = A[y - 1][x]
				area -= min(hu, h)
			if y < H - 1:
				hd = A[y + 1][x]
				area -= min(hd, h)
	return area
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A, H, W):
	sol = 2 * W * H
	for i in range(H):
		for j in range(W + 1):
			a = A[i][j - 1] if j > 0 else 0
			b = A[i][j] if j < W else 0
			sol += abs(a - b)
	for i in range(H + 1):
		for j in range(W):
			a = A[i - 1][j] if i > 0 else 0
			b = A[i][j] if i < H else 0
			sol += abs(a - b)
	return sol
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = [[] for j in range(H)]
for y in range(H):
	A[y] = [int(A_temp) for A_temp in input().strip().split(' ')]
area = 0
for y in range(H):
	for x in range(W):
		if A[y][x]:
			area += 2
			if y > 0:
				if A[y][x] > A[y - 1][x]:
					area += A[y][x] - A[y - 1][x]
			else:
				area += A[y][x]
			if y < H - 1:
				if A[y][x] > A[y + 1][x]:
					area += A[y][x] - A[y + 1][x]
			else:
				area += A[y][x]
			if x > 0:
				if A[y][x] > A[y][x - 1]:
					area += A[y][x] - A[y][x - 1]
			else:
				area += A[y][x]
			if x < W - 1:
				if A[y][x] > A[y][x + 1]:
					area += A[y][x] - A[y][x + 1]
			else:
				area += A[y][x]
print(area)


import sys

def surfaceArea(A):
	n = len(A)
	m = len(A[0])
	A = [[0] * m] + A + [[0] * m]
	for i in range(len(A)):
		A[i] = [0] + A[i] + [0]
	sol = 0
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if A[i][j] > A[i - 1][j]:
				sol += A[i][j] - A[i - 1][j]
			if A[i][j] > A[i + 1][j]:
				sol += A[i][j] - A[i + 1][j]
			if A[i][j] > A[i][j - 1]:
				sol += A[i][j] - A[i][j - 1]
			if A[i][j] > A[i][j + 1]:
				sol += A[i][j] - A[i][j + 1]
	sol += 2 * (n * m)
	return sol
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	total = len(A) * len(A[0]) * 2
	for row in A:
		cur = 0
		for val in row:
			total += abs(cur - val)
			cur = val
		total += cur
	trans_A = [[row[i] for row in A] for i in range(len(A[0]))]
	for row in trans_A:
		cur = 0
		for val in row:
			total += abs(cur - val)
			cur = val
		total += cur
	return total
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for _ in range(H):
	row = [int(val) for val in input().strip().split(' ')]
	A.append(row)
result = surfaceArea(A)
print(result)


(h, w) = map(int, input().split())
a = []
for i in range(h):
	a.append(tuple(map(int, input().split())))
ans = 0
for i in range(h):
	for j in range(w):
		if i == 0:
			ans += a[i][j]
		if i == h - 1:
			ans += a[i][j]
		if j == 0:
			ans += a[i][j]
		if j == w - 1:
			ans += a[i][j]
		if a[i][j]:
			ans += 2
		if i < h - 1:
			ans += abs(a[i][j] - a[i + 1][j])
		if j < w - 1:
			ans += abs(a[i][j] - a[i][j + 1])
print(ans)


(h, w) = [int(x) for x in input().split()]
a = []
for _ in range(h):
	a.append(list(map(int, input().split())))
ans = 2 * h * w
for i in range(h):
	for j in range(w):
		if i > 0:
			ans += max(a[i][j] - a[i - 1][j], 0)
		else:
			ans += a[0][j]
		if j > 0:
			ans += max(a[i][j] - a[i][j - 1], 0)
		else:
			ans += a[i][0]
		if i < h - 1:
			ans += max(a[i][j] - a[i + 1][j], 0)
		else:
			ans += a[h - 1][j]
		if j < w - 1:
			ans += max(a[i][j] - a[i][j + 1], 0)
		else:
			ans += a[i][w - 1]
print(ans)


import sys

def surfaceArea(A):
	rows = len(A)
	if rows == 0:
		return 0
	cols = len(A[0])
	s = 2 * rows * cols
	for i in range(0, rows):
		for j in range(0, cols):
			up = 0 if i == 0 else A[i - 1][j]
			down = 0 if i == rows - 1 else A[i + 1][j]
			left = 0 if j == 0 else A[i][j - 1]
			right = 0 if j == cols - 1 else A[i][j + 1]
			s += max(A[i][j] - up, 0)
			s += max(A[i][j] - down, 0)
			s += max(A[i][j] - left, 0)
			s += max(A[i][j] - right, 0)
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def get(A, row, col):
	if len(A) == 0:
		return 0
	if row >= len(A) or col >= len(A[0]):
		return 0
	if row < 0 or col < 0:
		return 0
	return A[row][col]

def surfaceArea(A, H, W):
	s = 0
	for r in range(H):
		for c in range(W):
			v = A[r][c]
			if v > 0:
				s += 2
			s += max(0, v - get(A, r - 1, c))
			s += max(0, v - get(A, r + 1, c))
			s += max(0, v - get(A, r, c - 1))
			s += max(0, v - get(A, r, c + 1))
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A, H, W):
	res = H * W
	for (i, row) in enumerate(A):
		for (j, el) in enumerate(row):
			res += 1
			if i == 0:
				res += el
			else:
				res += abs(el - A[i - 1][j])
			if i == H - 1:
				res += el
			if j == 0:
				res += el
			else:
				res += abs(el - A[i][j - 1])
			if j == W - 1:
				res += el
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A):
	n = len(A)
	m = len(A[0])
	h_dict = {}
	for x in range(n):
		for y in range(m):
			h_dict[x, y] = A[x][y]
	delta = [(-1, 0), (1, 0), (0, -1), (0, 1)]
	total = 2 * n * m
	for x in range(n):
		for y in range(m):
			for (dx, dy) in delta:
				neighbor = (x + dx, y + dy)
				if h_dict.get(neighbor) is None:
					total += h_dict[x, y]
					continue
				dh = h_dict[x, y] - h_dict[neighbor]
				if dh > 0:
					total += dh
	return total
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	res = 0
	res += len(A) * len(A[0]) * 2
	h = len(A)
	w = len(A[0])
	for i in range(h):
		res += A[i][0] + A[i][-1]
	for j in range(w):
		res += A[0][j] + A[-1][j]
	for i in range(h):
		for j in range(1, w):
			res += abs(A[i][j] - A[i][j - 1])
	for i in range(1, h):
		for j in range(w):
			res += abs(A[i][j] - A[i - 1][j])
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


(H, W) = map(int, input().split())
board = []
for h0 in range(H):
	board.append(list(map(int, input().split())))
ssurf = 2 * H * W
for i in range(H):
	for j in range(W):
		c = 0
		c += max(board[i][j] - (board[i - 1][j] if i > 0 else 0), 0)
		c += max(board[i][j] - (board[i + 1][j] if i < H - 1 else 0), 0)
		c += max(board[i][j] - (board[i][j - 1] if j > 0 else 0), 0)
		c += max(board[i][j] - (board[i][j + 1] if j < W - 1 else 0), 0)
		ssurf += c
print(ssurf)


import sys

def surfaceArea(A):
	surface = 0
	h = len(A)
	w = len(A[0])
	for i in range(h):
		for j in range(w):
			surface += 2
			if i == 0:
				surface += A[i][j]
			if j == 0:
				surface += A[i][j]
			if i == h - 1:
				surface += A[i][j]
			else:
				surface += abs(A[i][j] - A[i + 1][j])
			if j == w - 1:
				surface += A[i][j]
			else:
				surface += abs(A[i][j] - A[i][j + 1])
	return surface
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	H = len(A)
	W = len(A[0])
	ans = 2 * H * W
	for i in range(H):
		for j in range(W):
			if i == 0:
				ans += A[i][j]
			if j == 0:
				ans += A[i][j]
			if j == W - 1:
				ans += A[i][j]
			else:
				ans += abs(A[i][j] - A[i][j + 1])
			if i == H - 1:
				ans += A[i][j]
			else:
				ans += abs(A[i][j] - A[i + 1][j])
	return ans
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys
DD = [(-1, 0), (1, 0), (0, -1), (0, 1)]

def surfaceArea(A, H, W):
	res = 0
	for r in range(H):
		for c in range(W):
			res += 2
			for (dr, dc) in DD:
				(nr, nc) = (r + dr, c + dc)
				if nr < 0 or nc < 0 or nr >= H or (nc >= W):
					res += A[r][c]
				elif A[nr][nc] < A[r][c]:
					res += A[r][c] - A[nr][nc]
	return res
(H, W) = map(int, input().strip().split(' '))
A = []
for _ in range(H):
	a = tuple(map(int, input().strip().split(' ')))
	A.append(a)
result = surfaceArea(A, H, W)
print(result)


(h, w) = list(map(int, input().split()))
a = [list(map(int, input().split())) for i in range(h)]
price = 0
for i in range(h):
	for j in range(w):
		for k in range(a[i][j]):
			price += 4
			if i > 0 and a[i - 1][j] - 1 >= k:
				price -= 2
			if j > 0 and a[i][j - 1] - 1 >= k:
				price -= 2
price += 2 * h * w
print(price)


import sys

def count_row(X):
	t = len(X)
	rv = X[0] + X[t - 1]
	for i in range(t - 1):
		rv += abs(X[i] - X[i + 1])
	return rv

def surfaceArea(A):
	num_rows = len(A)
	num_cols = len(A[0])
	count = 2 * num_rows * num_cols
	for i in range(num_rows):
		count += count_row(A[i])
	for j in range(num_cols):
		X = [A[i][j] for i in range(num_rows)]
		count += count_row(X)
	return count
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def f(A, i, j, H, W):
	if (i < 0) | (j < 0):
		return 0
	if (i > H - 1) | (j > W - 1):
		return 0
	return A[i][j]

def surfaceArea(A, W, H):
	s = 0
	for i in range(H):
		for j in range(W):
			s += 2
			for a in [-1, 1]:
				s += max(A[i][j] - f(A, i + a, j, H, W), 0)
				s += max(A[i][j] - f(A, i, j + a, H, W), 0)
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, W, H)
print(result)


import sys

def surfaceArea(A):
	ans = W * H * 2
	for i in range(H):
		ans += A[i][0] + A[i][W - 1]
	for i in range(W):
		ans += A[0][i] + A[H - 1][i]
	for i in range(H):
		for j in range(W - 1):
			ans += abs(A[i][j] - A[i][j + 1])
	for j in range(W):
		for i in range(H - 1):
			ans += abs(A[i][j] - A[i + 1][j])
	return ans
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


def getIntList():
	return list(map(int, input().split(' ')))

def paint(row):
	tot = 0
	prev = 0
	for x in row:
		if x > prev:
			tot += x - prev
		prev = x
	return tot
(h, w) = getIntList()
toy = [getIntList() for x in range(h)]
s1 = sum([x > 0 for i in toy for x in i])
s2 = sum([paint(row) for row in toy])
s3 = sum([paint(row) for row in zip(*toy)])
print(2 * (s1 + s2 + s3))


import sys

def surfaceArea(A):
	price = 0
	rows_count = len(A)
	for i in range(rows_count):
		columns_count = len(A[i])
		for j in range(columns_count):
			current_value = A[i][j]
			if i - 1 < 0:
				price += current_value
			if j - 1 < 0:
				price += current_value
			if j + 1 == columns_count:
				price += current_value
			else:
				price += abs(A[i][j + 1] - current_value)
			if i + 1 == rows_count:
				price += current_value
			else:
				price += abs(A[i + 1][j] - current_value)
			price += 2
	return price
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(a, r, c):
	co = 0
	for i in range(r):
		for j in range(c):
			if j - 1 < 0:
				co = co + a[i][j]
			elif a[i][j - 1] < a[i][j]:
				co = co + a[i][j] - a[i][j - 1]
			if j + 1 > c - 1:
				co = co + a[i][j]
			elif a[i][j + 1] < a[i][j]:
				co = co + a[i][j] - a[i][j + 1]
			if i - 1 < 0:
				co = co + a[i][j]
			elif a[i - 1][j] < a[i][j]:
				co = co + a[i][j] - a[i - 1][j]
			if i + 1 > r - 1:
				co = co + a[i][j]
			elif a[i + 1][j] < a[i][j]:
				co = co + a[i][j] - a[i + 1][j]
	co = co + 2 * r * c
	return co
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A, H, W):
	sum = 2 * H * W
	for i in range(H):
		for j in range(W):
			value = A[i][j]
			if i - 1 < 0:
				sum += value
			else:
				sum += max(0, value - A[i - 1][j])
			if j + 1 > W - 1:
				sum += value
			else:
				sum += max(0, value - A[i][j + 1])
			if i + 1 > H - 1:
				sum += value
			else:
				sum += max(0, value - A[i + 1][j])
			if j - 1 < 0:
				sum += value
			else:
				sum += max(0, value - A[i][j - 1])
	return sum
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A, L, W):
	SA = 0
	for i in range(0, L):
		for j in range(0, W):
			if i - 1 >= 0 and j - 1 >= 0:
				SA += 2 + abs(A[i][j] - A[i - 1][j]) + abs(A[i][j] - A[i][j - 1])
			elif i - 1 >= 0 and j - 1 == -1:
				SA += 2 + abs(A[i][j] - A[i - 1][j]) + abs(A[i][j])
			elif j - 1 >= 0 and i - 1 == -1:
				SA += 2 + abs(A[i][j]) + abs(A[i][j] - A[i][j - 1])
			elif i == 0 and j == 0:
				SA += 2 + abs(A[i][j]) + abs(A[i][j])
	for i in range(0, W):
		SA += A[L - 1][i]
	for i in range(0, L):
		SA += A[i][W - 1]
	return SA
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(a, h, w):
	su = sum(a[-1]) + sum(a[0]) + 2 * h * w
	for j in range(h):
		su = su + a[j][0] + a[j][-1]
	for j in range(h):
		for j1 in range(w - 1):
			su = su + abs(a[j][j1] - a[j][j1 + 1])
	for j in range(h - 1):
		for j1 in range(w):
			su = su + abs(a[j][j1] - a[j + 1][j1])
	return su
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A):
	totalArea = 0
	iMax = len(A) - 1
	jMax = len(A[0]) - 1
	for i in range(len(A)):
		for j in range(len(A[i])):
			totalArea += 2
			if i - 1 >= 0:
				if A[i][j] > A[i - 1][j]:
					totalArea += A[i][j] - A[i - 1][j]
			else:
				totalArea += A[i][j]
			if i + 1 <= iMax:
				if A[i][j] > A[i + 1][j]:
					totalArea += A[i][j] - A[i + 1][j]
			else:
				totalArea += A[i][j]
			if j - 1 >= 0:
				if A[i][j] > A[i][j - 1]:
					totalArea += A[i][j] - A[i][j - 1]
			else:
				totalArea += A[i][j]
			if j + 1 <= jMax:
				if A[i][j] > A[i][j + 1]:
					totalArea += A[i][j] - A[i][j + 1]
			else:
				totalArea += A[i][j]
	return totalArea
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def removen(A, i, j, H, W):
	here = A[i][j]
	north = 0
	south = 0
	east = 0
	west = 0
	n = 0
	if here != 0:
		if i > 0:
			north = A[i - 1][j]
		if j > 0:
			west = A[i][j - 1]
		if i < H - 1:
			south = A[i + 1][j]
		if j < W - 1:
			east = A[i][j + 1]
		n = min(north, here) + min(south, here) + min(east, here) + min(west, here)
	return n

def remove_neighbors(A, H, W):
	tot = 0
	for i in range(H):
		for j in range(W):
			tot += removen(A, i, j, H, W)
	return tot

def surfaceArea(A, H, W):
	total_without_neighbors = 0
	for i in range(H):
		for j in range(W):
			if A[i][j] == 1:
				total_without_neighbors += 6
			elif A[i][j] == 2:
				total_without_neighbors += 10
			elif A[i][j] > 2:
				total_without_neighbors += 10 + 4 * (A[i][j] - 2)
	neigh = remove_neighbors(A, H, W)
	total_without_neighbors -= neigh
	return total_without_neighbors
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A):
	top = bottom = len(A) * len(A[0])
	left = sum(A[0])
	right = sum(A[-1])
	front = 0
	back = 0
	middle = 0
	for elem in range(len(A)):
		front += A[elem][0]
		back += A[elem][-1]
		for i in range(len(A[elem])):
			if i != len(A[elem]) - 1:
				middle += abs(A[elem][i + 1] - A[elem][i])
			if elem != len(A) - 1:
				middle += abs(A[elem + 1][i] - A[elem][i])
	return top + bottom + left + right + front + back + middle
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	H = len(A) - 2
	W = len(A[0]) - 2
	s = H * W * 2
	for i in range(1, H + 1):
		for j in range(1, W + 1):
			for k in range(1, A[i][j] + 1):
				if A[i - 1][j] < k:
					s += 1
				if A[i + 1][j] < k:
					s += 1
				if A[i][j - 1] < k:
					s += 1
				if A[i][j + 1] < k:
					s += 1
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = [[0] * (W + 2)]
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A_t.insert(0, 0)
	A_t.append(0)
	A.append(A_t)
A.append([0] * (W + 2))
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A, h, w):
	su = 2 * h * w
	su += sum(A[0]) + sum(A[-1])
	for j in range(h):
		su += A[j][0] + A[j][-1]
	for i in range(h):
		for j in range(w - 1):
			su += abs(A[i][j] - A[i][j + 1])
	for i in range(h - 1):
		for j in range(w):
			su += abs(A[i][j] - A[i + 1][j])
	return su
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A):
	answer = len(A) * len(A[0]) * 2
	before = None
	for i in range(len(A)):
		for j in range(len(A[i])):
			if j == 0:
				answer += A[i][j]
			elif before > A[i][j]:
				pass
			else:
				answer += A[i][j] - before
			before = A[i][j]
	for i in range(len(A)):
		for j in range(len(A[i]) - 1, -1, -1):
			if j == len(A[i]) - 1:
				answer += A[i][j]
			elif before > A[i][j]:
				pass
			else:
				answer += A[i][j] - before
			before = A[i][j]
	for j in range(len(A[0])):
		for i in range(len(A)):
			if i == 0:
				answer += A[i][j]
			elif before > A[i][j]:
				pass
			else:
				answer += A[i][j] - before
			before = A[i][j]
	for j in range(len(A[0])):
		for i in range(len(A) - 1, -1, -1):
			if i == len(A) - 1:
				answer += A[i][j]
			elif before > A[i][j]:
				pass
			else:
				answer += A[i][j] - before
			before = A[i][j]
	return answer
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


def s(x, y):
	if x - y > 0:
		return x - y
	else:
		return 0
(h, w) = map(int, input().split())
cube = []
cube.append([0] * (w + 2))
for i in range(h):
	cube.append(list(map(int, input().split())))
cube.append([0] * (w + 2))
for i in range(1, h + 1):
	cube[i].insert(0, 0)
	cube[i].append(0)
area = 0
area += 2 * h * w
h = h + 2
w = w + 2
for i in range(1, h - 1):
	for j in range(1, w - 1):
		area += s(cube[i][j], cube[i][j + 1])
		area += s(cube[i][j], cube[i][j - 1])
		area += s(cube[i][j], cube[i + 1][j])
		area += s(cube[i][j], cube[i - 1][j])
print(area)


import sys

def surfaceArea(A):
	bottom = top = H * W
	left = right = front = back = 0
	for i in range(H):
		for j in range(W):
			if j == 0:
				left += A[i][j]
			else:
				calculate = A[i][j] - A[i][j - 1]
				if calculate > 0:
					left += calculate
			if j == W - 1:
				right += A[i][j]
			else:
				calculate = A[i][j] - A[i][j + 1]
				if calculate > 0:
					right += calculate
			if i == 0:
				front += A[i][j]
			else:
				calculate = A[i][j] - A[i - 1][j]
				if calculate > 0:
					front += calculate
			if i == H - 1:
				back += A[i][j]
			else:
				calculate = A[i][j] - A[i + 1][j]
				if calculate > 0:
					back += calculate
	return top + bottom + left + right + front + back
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	bottom_top = H * W * 2
	top_side = sum(A[0])
	bottom_side = sum(A[-1])
	left_side = 0
	right_side = 0
	hori_diff = 0
	vert_diff = 0
	for i in range(len(A)):
		left_side += A[i][0]
		right_side += A[i][-1]
	for i in A:
		hori_diff += sum([abs(j - i) for (i, j) in zip(i[:-1], i[1:])])
	swapped = zip(*A[::-1])
	for i in swapped:
		vert_diff += sum([abs(j - i) for (i, j) in zip(i[:-1], i[1:])])
	return left_side + right_side + hori_diff + vert_diff + bottom_top + top_side + bottom_side
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	s = 0
	for i in range(H):
		s += A[i][0]
	for i in range(W):
		s += A[0][i]
	for i in range(H):
		s += A[i][W - 1]
	for i in range(W):
		s += A[H - 1][i]
	for i in range(1, H):
		for j in range(W):
			s += abs(A[i][j] - A[i - 1][j])
	for i in range(H):
		for j in range(1, W):
			s += abs(A[i][j] - A[i][j - 1])
	s += 2 * H * W
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	ans = 0
	nans = 0
	for i in range(1, len(A)):
		for j in range(0, len(A[i]) - 1):
			ans = ans + abs(A[i][j] - A[i][j + 1])
	for j in range(1, len(A[0])):
		for i in range(0, len(A) - 1):
			nans = nans + abs(A[i][j] - A[i + 1][j])
	k = (len(A) - 2) * (len(A[0]) - 2) * 2
	return ans + nans + k
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
A_ = [0 for i in range(W + 2)]
A.append(A_)
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A_t.append(0)
	A_t.insert(0, 0)
	A.append(A_t)
A.append(A_)
result = surfaceArea(A)
print(result)


def surfaceArea(A):
	c = 2 * len(A) * len(A[0])
	for i in range(len(A)):
		c += A[i][0]
		for j in range(1, len(A[i])):
			c += abs(A[i][j] - A[i][j - 1])
		c += A[i][-1]
	for i in range(len(A[0])):
		c += A[0][i]
		for j in range(1, len(A)):
			c += abs(A[j][i] - A[j - 1][i])
		c += A[-1][i]
	return c
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A, H, W):
	raw_area = 0
	flat_a = [item for sublist in A for item in sublist]
	for a in flat_a:
		raw_area += a * 4 + 2
	for i in range(H - 1):
		for j in range(W):
			shared = min(A[i][j], A[i + 1][j])
			raw_area -= shared * 2
	for i in range(H):
		for j in range(W - 1):
			shared = min(A[i][j], A[i][j + 1])
			raw_area -= shared * 2
	return raw_area
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(a, r, c):
	ans = sum(a[0]) + sum(a[r - 1]) + 2 * r * c
	for i in range(r):
		for j in range(c):
			if j == 0 or j == c - 1:
				ans += a[i][j]
				if c - 1 == 0:
					ans += a[i][j]
			if 0 < j:
				ans += abs(a[i][j] - a[i][j - 1])
	for i in range(c):
		for j in range(1, r):
			ans += abs(a[j][i] - a[j - 1][i])
	return ans
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A, W, H):
	ans = 0
	top_bottom = 0
	for i in range(1, H + 1):
		for j in range(1, W + 1):
			ans += max(0, A[i][j] - A[i - 1][j])
			ans += max(0, A[i][j] - A[i + 1][j])
			ans += max(0, A[i][j] - A[i][j - 1])
			ans += max(0, A[i][j] - A[i][j + 1])
			if A[i][j] != 0:
				top_bottom += 2
	return ans + top_bottom
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
A.append([0] * (W + 2))
for A_i in range(H):
	A_t = [0] + [int(A_temp) for A_temp in input().strip().split(' ')] + [0]
	A.append(A_t)
A.append([0] * (W + 2))
result = surfaceArea(A, W, H)
print(result)


import sys

def surfaceArea(A, H, W):
	(s1, s2, s3, s4) = (0, 0, 0, 0)
	for i in range(H):
		s1 = s1 + A[i][0]
		s2 = s2 + A[i][W - 1]
		for j in range(W):
			if j < W - 1:
				s3 = s3 + abs(A[i][j] - A[i][j + 1])
			if i < H - 1:
				s4 = s4 + abs(A[i][j] - A[i + 1][j])
	return sum(A[0]) + sum(A[H - 1]) + 2 * H * W + s1 + s2 + s3 + s4
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(a, h, w):
	price = 2 * h * w
	for y in range(h):
		for x in range(w):
			if x == 0:
				price += a[y][x]
			if x == w - 1:
				price += a[y][x]
			if y == 0:
				price += a[y][x]
			if y == h - 1:
				price += a[y][x]
			if x > 0 and a[y][x] > a[y][x - 1]:
				price += a[y][x] - a[y][x - 1]
			if x + 1 < w and a[y][x] > a[y][x + 1]:
				price += a[y][x] - a[y][x + 1]
			if y > 0 and a[y][x] > a[y - 1][x]:
				price += a[y][x] - a[y - 1][x]
			if y + 1 < h and a[y][x] > a[y + 1][x]:
				price += a[y][x] - a[y + 1][x]
	return price
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surf(H, W, A, i, j):
	cur = A[i][j]
	if cur == 0:
		return 0
	top = A[i - 1][j] if i > 0 else 0
	bottom = A[i + 1][j] if i + 1 < H else 0
	left = A[i][j - 1] if j > 0 else 0
	right = A[i][j + 1] if j + 1 < W else 0
	return 2 + max(0, cur - left) + max(0, cur - right) + max(0, cur - top) + max(0, cur - bottom)

def surfaceArea(H, W, A):
	res = 0
	for i in range(H):
		for j in range(W):
			res += surf(H, W, A, i, j)
	return res
(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
result = surfaceArea(H, W, A)
print(result)


import sys

def surfaceArea(A, H, W):
	solution = 2 * H * W
	if H == 1 and W == 1:
		return solution + 4 * A[0][0]
	if H == 1:
		for j in range(W):
			if j == 0:
				solution += 3 * A[0][j] + max(0, A[0][j] - A[0][j + 1])
			elif j == W - 1:
				solution += 3 * A[0][j] + max(0, A[0][j] - A[0][j - 1])
			else:
				solution += 2 * A[0][j] + max(0, A[0][j] - A[0][j - 1]) + max(0, A[0][j] - A[0][j + 1])
		return solution
	if W == 1:
		for i in range(H):
			if i == 0:
				solution += 3 * A[i][0] + max(0, A[i][0] - A[i + 1][0])
			elif i == H - 1:
				solution += 3 * A[i][0] + max(0, A[i][0] - A[i - 1][0])
			else:
				solution += 2 * A[i][0] + max(0, A[i][0] - A[i - 1][0]) + max(0, A[i][0] - A[i + 1][0])
		return solution
	for i in range(H):
		for j in range(W):
			if i == 0 and j == 0:
				solution += 2 * A[i][j] + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j + 1])
			elif i == 0 and j == W - 1:
				solution += 2 * A[i][j] + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j - 1])
			elif i == H - 1 and j == 0:
				solution += 2 * A[i][j] + max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i][j + 1])
			elif i == H - 1 and j == W - 1:
				solution += 2 * A[i][j] + max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i][j - 1])
			elif i == 0:
				solution += A[i][j] + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j + 1]) + max(0, A[i][j] - A[i][j - 1])
			elif i == H - 1:
				solution += A[i][j] + max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i][j + 1]) + max(0, A[i][j] - A[i][j - 1])
			elif j == 0:
				solution += A[i][j] + max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j + 1])
			elif j == W - 1:
				solution += A[i][j] + max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j - 1])
			else:
				solution += max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j + 1]) + max(0, A[i][j] - A[i][j - 1])
	return solution
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for _ in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys
from collections import defaultdict

def surfaceArea(M):
	i = 1
	j = 1
	su = 0
	while M[i, j]:
		while M[i, j]:
			su += M[i, j] * 4 + 2
			su -= min(M[i, j], M[i - 1, j])
			su -= min(M[i, j], M[i + 1, j])
			su -= min(M[i, j], M[i, j - 1])
			su -= min(M[i, j], M[i, j + 1])
			j += 1
		i += 1
		j = 1
	return su
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
M = defaultdict(int)
i = 1
for _ in range(H):
	r = map(int, input().strip().split(' '))
	j = 1
	for c in r:
		M[i, j] = c
		j += 1
	i += 1
result = surfaceArea(M)
print(result)


import sys

def surfaceArea(A):
	m = len(A)
	n = len(A[0])
	base = m * n
	sides = extras = 0
	for c in range(n):
		sides += A[0][c] + A[m - 1][c]
	for r in range(m):
		sides += A[r][0] + A[r][n - 1]
	for r in range(m):
		for c in range(1, n):
			extras += abs(A[r][c - 1] - A[r][c])
	for r in range(1, m):
		for c in range(n):
			extras += abs(A[r - 1][c] - A[r][c])
	return base * 2 + sides + extras
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


(r, c) = map(int, input().split())
a = []
for i in range(r):
	a.append(list(map(int, input().split())))
if r == 1 and c == 1:
	print(2 + 4 * a[0][0])
elif r == 1:
	pa = 2 * c + 3 * a[0][0] + 3 * a[0][c - 1]
	for i in range(1, c - 1):
		pa += 2 * a[0][i]
	for i in range(c - 1):
		pa += abs(a[0][i + 1] - a[0][i])
	print(pa)
elif c == 1:
	pa = 2 * r + 3 * a[0][0] + 3 * a[r - 1][0]
	for i in range(1, r - 1):
		pa += 2 * a[i][0]
	for i in range(r - 1):
		pa += abs(a[i + 1][0] - a[i][0])
	print(pa)
else:
	ans = 2 * r * c + 2 * a[0][0] + 2 * a[r - 1][c - 1] + 2 * a[0][c - 1] + 2 * a[r - 1][0]
	for i in range(1, r - 1):
		ans += a[i][0] + a[i][c - 1]
	for i in range(1, c - 1):
		ans += a[0][i] + a[r - 1][i]
	for j in range(r):
		for i in range(c - 1):
			ans += abs(a[j][i + 1] - a[j][i])
	for j in range(c):
		for i in range(r - 1):
			ans += abs(a[i + 1][j] - a[i][j])
	print(ans)


(H, W) = list(map(int, input().strip().split()))
A = [[None for j in range(W)] for i in range(H)]
Area = [[None for j in range(W)] for i in range(H)]
for i in range(H):
	A[i] = list(map(int, input().strip().split()))
	Area[i] = [2 * (1 + 2 * h) for h in A[i]]
Total_area = 0
for i in range(H):
	for j in range(W):
		if i < H - 1:
			if A[i + 1][j] > A[i][j]:
				Area[i][j] -= A[i][j]
			else:
				Area[i][j] -= A[i + 1][j]
		if i > 0:
			if A[i - 1][j] > A[i][j]:
				Area[i][j] -= A[i][j]
			else:
				Area[i][j] -= A[i - 1][j]
		if j < W - 1:
			if A[i][j + 1] > A[i][j]:
				Area[i][j] -= A[i][j]
			else:
				Area[i][j] -= A[i][j + 1]
		if j > 0:
			if A[i][j - 1] > A[i][j]:
				Area[i][j] -= A[i][j]
			else:
				Area[i][j] -= A[i][j - 1]
	Total_area += sum(Area[i])
print(Total_area)


import sys

def surfaceArea(A):
	(X, Y, s) = (len(A) - 1, len(A[0]) - 1, 0)
	for (x, r) in enumerate(A):
		for (y, c) in enumerate(r):
			s += 2
			if x == 0:
				s += c
			if x == X:
				s += c
			if y == 0:
				s += c
			if y == Y:
				s += c
			if x != 0 and A[x - 1][y] < c:
				s += c - A[x - 1][y]
			if x != X and A[x + 1][y] < c:
				s += c - A[x + 1][y]
			if y != 0 and A[x][y - 1] < c:
				s += c - A[x][y - 1]
			if y != Y and A[x][y + 1] < c:
				s += c - A[x][y + 1]
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def get_diff(A, h, w, i, j):
	dx = [1, 0, 0, -1]
	dy = [0, 1, -1, 0]
	diff_sum = 0
	for di in range(len(dx)):
		pi = i + dx[di]
		pj = j + dy[di]
		if pi < 0 or pi >= h or pj < 0 or (pj >= w):
			continue
		diff = max(0, A[i][j] - A[pi][pj])
		diff_sum += diff
	return diff_sum

def surfaceArea(A, h, w):
	top_bottom = h * w * 2
	sides = 0
	for i in range(h):
		sides += A[i][0] + A[i][w - 1]
	for j in range(w):
		sides += A[0][j] + A[h - 1][j]
	diff_sum = 0
	for i in range(h):
		for j in range(w):
			diff = get_diff(A, h, w, i, j)
			diff_sum += diff
	return top_bottom + sides + diff_sum
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys
import math

def surfaceArea(A):
	h = len(A)
	w = len(A[0])
	totalprice = 0
	for i in range(h):
		price = 0
		for j in range(w):
			price += 2
			if i == 0:
				price += A[i][j]
			if j == 0:
				price += A[i][j]
			if j == w - 1:
				price += A[i][j]
			if i == h - 1:
				price += A[i][j]
			if j < w - 1:
				price += abs(A[i][j] - A[i][j + 1])
			if i < h - 1:
				price += abs(A[i][j] - A[i + 1][j])
		totalprice += price
	return totalprice
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


(h, w) = map(int, input().split())
(l, s) = ([], 0)
for i in range(h):
	x = list(map(int, input().split()))
	s += sum(x)
	l.append(x)
price = s * 6 - (s - h * w) * 2
m = 0
for i in range(h):
	for j in range(w - 1):
		m += min(l[i][j], l[i][j + 1])
for i in range(h - 1):
	for j in range(w):
		m += min(l[i][j], l[i + 1][j])
price -= m * 2
print(price)


import sys

def side(A, curr, i, j):
	if i < 0 or j < 0 or i >= len(A) or (j >= len(A[i])):
		return curr
	elif curr <= A[i][j]:
		return 0
	else:
		return curr - A[i][j]

def surfaceArea(A):
	surface = len(A) * len(A[0]) * 2
	for i in range(len(A)):
		for j in range(len(A[i])):
			surface += side(A, A[i][j], i - 1, j)
			surface += side(A, A[i][j], i + 1, j)
			surface += side(A, A[i][j], i, j + 1)
			surface += side(A, A[i][j], i, j - 1)
	return surface
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A, n, m):
	total = 2 * n * m
	curret = 0
	face = 0
	for i in range(m):
		curret = 0
		for j in range(n):
			if A[j][i] > curret:
				face += A[j][i] - curret
			curret = A[j][i]
	total += face
	face = 0
	for i in range(m):
		curret = 0
		for j in range(n):
			if A[n - j - 1][m - i - 1] > curret:
				face += A[n - j - 1][m - i - 1] - curret
			curret = A[n - j - 1][m - i - 1]
	total += face
	face = 0
	for i in range(n):
		curret = 0
		for j in range(m):
			if A[i][j] > curret:
				face += A[i][j] - curret
			curret = A[i][j]
	total += face
	face = 0
	for i in range(n):
		curret = 0
		for j in range(m):
			if A[n - i - 1][m - j - 1] > curret:
				face += A[n - i - 1][m - j - 1] - curret
			curret = A[n - i - 1][m - j - 1]
	total += face
	face = 0
	return total
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A):
	(H, W) = (len(A), len(A[0]))
	area = 2 * H * W
	for i in range(H):
		for j in range(W):
			h = A[i][j]
			east = h if j == 0 else max(0, A[i][j] - A[i][j - 1])
			west = h if j == W - 1 else max(0, A[i][j] - A[i][j + 1])
			north = h if i == 0 else max(0, A[i][j] - A[i - 1][j])
			south = h if i == H - 1 else max(0, A[i][j] - A[i + 1][j])
			area += east + west + north + south
	return area
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A, H, W):
	res = 2 * H * W
	for i in range(H):
		for j in range(W):
			if j == 0:
				res += A[i][j]
			if j == W - 1:
				res += A[i][j]
			if i == 0:
				res += A[i][j]
			if i == H - 1:
				res += A[i][j]
			if j > 0:
				res += abs(A[i][j - 1] - A[i][j])
			if i > 0:
				res += abs(A[i - 1][j] - A[i][j])
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys
(n, m) = [int(i) for i in input().strip().split()]
arr = [[int(i) for i in input().strip().split()] for i in range(n)]
ans = 0
for i in range(n):
	for j in range(m):
		temp = 6
		k = arr[i][j]
		if k > 1:
			temp = k * 6 - (k - 2) * 2 - 2
		if i - 1 >= 0:
			if arr[i - 1][j] > k:
				temp -= k
			else:
				temp -= arr[i - 1][j]
		if i + 1 < n:
			if arr[i + 1][j] > k:
				temp -= k
			else:
				temp -= arr[i + 1][j]
		if j - 1 >= 0:
			if arr[i][j - 1] > k:
				temp -= k
			else:
				temp -= arr[i][j - 1]
		if j + 1 < m:
			if arr[i][j + 1] > k:
				temp -= k
			else:
				temp -= arr[i][j + 1]
		ans += temp
print(ans)


(m, n) = list(map(int, input().split()))
a = []
for i in range(m):
	a.append(list(map(int, input().split())))
ans = 2 * m * n
for i in range(m):
	ans += a[i][0] + a[i][n - 1]
for i in range(n):
	ans += a[0][i] + a[m - 1][i]
tempans = ans
ans = 0
for i in range(m):
	for j in range(n):
		if i > 0:
			ans += abs(a[i][j] - a[i - 1][j])
		if i < m - 1:
			ans += abs(a[i][j] - a[i + 1][j])
		if j > 0:
			ans += abs(a[i][j] - a[i][j - 1])
		if j < n - 1:
			ans += abs(a[i][j] - a[i][j + 1])
print(tempans + ans // 2)


(m, n) = [int(x) for x in input().strip().split(' ')]
matrix = []
for _ in range(m):
	matrix.append([int(x) for x in input().strip().split(' ')])
permutations = [(-1, 0), (0, -1), (1, 0), (0, 1)]
surfaces = 0
for i in range(m):
	for j in range(n):
		curr = matrix[i][j]
		if curr > 0:
			surfaces += curr * 4 + 2
		else:
			surfaces += 0
			continue
		for k in permutations:
			a = i + k[0]
			b = j + k[1]
			if not (a >= 0 and a < m and (b >= 0) and (b < n)):
				continue
			val = matrix[i + k[0]][j + k[1]]
			if curr > val:
				surfaces -= val
			else:
				surfaces -= curr
print(surfaces)


import sys

def surfaceArea(A):
	AA = []
	adjacency = [(i, j) for i in (-1, 0, 1) for j in (-1, 0, 1) if not i == j == 0]
	total = 0
	for i in range(0, len(A)):
		AA.append([])
		for j in range(0, len(A[i])):
			aij = A[i][j]
			aijss = aij * 4 + 2
			for adj in adjacency:
				(adjx, adjy) = adj
				if abs(adjx) == abs(adjy) or i + adjx < 0 or j + adjy < 0 or (i + adjx >= len(A)) or (j + adjy >= len(A[i])):
					continue
				adjaij = A[i + adjx][j + adjy]
				aijss -= min(aij, adjaij)
			AA[i].append(aijss)
		total += sum(AA[i])
	return total
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


(h, w) = [int(x) for x in input().split()]
grid = []
for _ in range(h):
	grid.append([int(x) for x in input().split()] + [0])
grid.append([0] * (w + 1))
area = w * h * 2
for i in range(h + 1):
	for j in range(w + 1):
		area += abs(grid[i][j] - grid[i - 1][j])
		area += abs(grid[i][j] - grid[i][j - 1])
print(area)


import sys

def getExposedArea(arr):
	value = 0
	for i in range(len(arr) - 1):
		value = value + abs(arr[i] - arr[i + 1])
	return value

def surfaceArea(H, W, A):
	areaTopBottom = 2 * H * W
	tempSum = 0
	for row in A:
		tempSum = tempSum + row[0] + row[-1]
	areaSides = sum(A[0]) + sum(A[-1]) + tempSum
	horizontalSum = 0
	verticalSum = 0
	for row in A:
		horizontalSum = horizontalSum + getExposedArea(row)
	myList = []
	for j in range(len(A[0])):
		tempList = []
		for row in A:
			tempList.append(row[j])
		myList.append(tempList)
	for vRow in myList:
		verticalSum = verticalSum + getExposedArea(vRow)
	sumHV = horizontalSum + verticalSum
	return sumHV + areaSides + areaTopBottom
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(H, W, A)
print(result)


import sys

def surfaceArea(A, W, H):
	sides = 0
	for row in range(H):
		for col in range(W):
			if row == 0:
				sides += A[row][col]
			if col == 0:
				sides += A[row][col]
			if row == H - 1:
				sides += A[row][col]
			if col == W - 1:
				sides += A[row][col]
			if row > 0:
				sides += abs(A[row][col] - A[row - 1][col])
			if col > 0:
				sides += abs(A[row][col] - A[row][col - 1])
			if A[row][col] != 0:
				sides += 2
	return sides
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
print(surfaceArea(A, W, H))


import sys

def surfaceArea(H, W, A):
	s = 0
	for i in range(H):
		r = 0
		rr = []
		for j in range(W):
			r += A[i][j] if i == 0 else abs(A[i - 1][j] - A[i][j])
			r += A[i][j] if i == H - 1 else 0
			r += A[i][j] if j == 0 else abs(A[i][j] - A[i][j - 1])
			r += A[i][j] if j == W - 1 else 0
			r += 2 if A[i][j] > 0 else 0
		s += r
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(H, W, A)
print(result)


import sys

def surfaceArea(A, H, W):
	price = 0
	for i in range(H):
		for j in range(W):
			val = 4 * A[i][j] + 2
			if i < H - 1:
				val -= min(A[i][j], A[i + 1][j])
			if i > 0:
				val -= min(A[i][j], A[i - 1][j])
			if j < W - 1:
				val -= min(A[i][j], A[i][j + 1])
			if j > 0:
				val -= min(A[i][j], A[i][j - 1])
			price += val
	return price
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for _ in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


(h, w) = map(int, input().split())
data = []
data.append([0 for i in range(w + 2)])
for i in range(h):
	lst = list(map(int, input().split()))
	lst.insert(0, 0)
	lst.append(0)
	data.append(lst)
ans = h * w * 2
data.append([0 for i in range(w + 2)])
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if data[i][j] - data[i][j - 1] > 0:
			ans += data[i][j] - data[i][j - 1]
		if data[i][j] - data[i - 1][j] > 0:
			ans += data[i][j] - data[i - 1][j]
		if data[i][j] - data[i][j + 1] > 0:
			ans += data[i][j] - data[i][j + 1]
		if data[i][j] - data[i + 1][j] > 0:
			ans += data[i][j] - data[i + 1][j]
print(ans)


import sys

def surfaceArea(A):
	m = len(A)
	n = len(A[0])
	ans = 0
	for i in range(m):
		for j in range(n):
			if j > 0 and A[i][j] > A[i][j - 1]:
				ans += A[i][j] - A[i][j - 1]
			if j == 0:
				ans += A[i][j]
			if j == n - 1:
				ans += A[i][j]
			if j < n - 1 and A[i][j] > A[i][j + 1]:
				ans += A[i][j] - A[i][j + 1]
			if i == 0:
				ans += A[i][j]
			if i > 0 and A[i][j] > A[i - 1][j]:
				ans += A[i][j] - A[i - 1][j]
			if i == m - 1:
				ans += A[i][j]
			if i < m - 1 and A[i][j] > A[i + 1][j]:
				ans += A[i][j] - A[i + 1][j]
			ans += 2
	return ans
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def check_side(A, i, j, i2, j2):
	if A[i][j] > A[i2][j2]:
		return A[i][j] - A[i2][j2]
	return 0

def surfaceArea(H, W, A):
	result = 2 * H * W
	for i in range(1, H + 1):
		for j in range(1, W + 1):
			result += check_side(A, i, j, i + 1, j)
			result += check_side(A, i, j, i - 1, j)
			result += check_side(A, i, j, i, j + 1)
			result += check_side(A, i, j, i, j - 1)
	return result
(H, W) = [int(i) for i in input().strip().split(' ')]
A = [[0 for _ in range(W + 2)]]
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append([0] + A_t + [0])
A.append([0 for _ in range(W + 2)])
result = surfaceArea(H, W, A)
print(result)


import sys

def initialize(A):
	l = []
	for i in range(len(A)):
		A[i].append(0)
		l = [0]
		A[i] = l + A[i]
	l = []
	for i in range(len(A[0])):
		l.append(0)
	temp = []
	temp.append(l)
	for row in A:
		temp.append(row)
	A = temp
	A.append(l)
	return A

def checkAround(up, down, left, right, center, level):
	s = 0
	if center > level:
		center = level
	if center > up:
		s += 1
	if center > left:
		s += 1
	if center > down:
		s += 1
	if center > right:
		s += 1
	return s

def surfaceArea(A):
	h = len(A)
	w = len(A[0])
	top = h * w
	base = h * w
	highest_block = 1
	surface = 0
	for row in A:
		temp = max(row)
		if temp > highest_block:
			highest_block = temp
	A = initialize(A)
	for level in range(highest_block):
		for i in range(1, h + 1):
			for j in range(1, w + 1):
				if A[i][j] > level:
					current = checkAround(A[i - 1][j], A[i + 1][j], A[i][j - 1], A[i][j + 1], A[i][j], level + 1)
					surface += current
	surface = surface + top + base
	return surface
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	total = 0
	for i in range(len(A)):
		for j in range(len(A[i])):
			if i == 0:
				total += A[i][j]
			else:
				total += max(0, A[i][j] - A[i - 1][j])
			if i + 1 == len(A):
				total += A[i][j]
			else:
				total += max(0, A[i][j] - A[i + 1][j])
			if j == 0:
				total += A[i][j]
			else:
				total += max(0, A[i][j] - A[i][j - 1])
			if j + 1 == len(A[i]):
				total += A[i][j]
			else:
				total += max(0, A[i][j] - A[i][j + 1])
	return total
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A) + 2 * H * W
print(result)


import sys

def surfaceArea(A, H, W):
	sTop = H * W
	sBottom = H * W
	sHLeft = sum(A[0])
	sHRight = sum(A[H - 1])
	sWInside = sum([A[i][0] for i in range(H)])
	sWOutside = sum([A[i][W - 1] for i in range(H)])
	sumShips = 0
	sumShips += sum([abs(A[i][j] - A[i][j + 1]) for i in range(H) for j in range(W - 1)])
	sumShips += sum([abs(A[i][j] - A[i + 1][j]) for i in range(H - 1) for j in range(W)])
	price = sTop + sBottom + sHLeft + sHRight + sWInside + sWOutside + sumShips
	return price
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def exposedArea(array):
	val = 0
	for i in range(len(array) - 1):
		val = val + abs(array[i] - array[i + 1])
	return val

def surfaceArea(H, W, A):
	areaofTopBottom = 2 * H * W
	sidesum = 0
	for row in A:
		sidesum = sidesum + row[0] + row[-1]
		areasides = sum(A[0]) + sum(A[-1]) + sidesum
	horsum = 0
	versum = 0
	for row in A:
		horsum = horsum + exposedArea(row)
	mylist = []
	for j in range(len(A[0])):
		normallist = []
		for row in A:
			normallist.append(row[j])
		mylist.append(normallist)
	for verrow in mylist:
		versum = versum + exposedArea(verrow)
	sumHV = horsum + versum
	return sumHV + areasides + areaofTopBottom
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(H, W, A)
print(result)


import sys
NEIGHBS = [(0, 1), (0, -1), (1, 0), (-1, 0)]

def getHeight(A, x, y, H, W):
	if x < 0 or y < 0 or x >= W or (y >= H):
		return 0
	else:
		return A[y][x]

def surfaceArea(A, H, W):
	res = 0
	for x in range(W):
		for y in range(H):
			mH = A[y][x]
			if mH != 0:
				res += 2
				for n in NEIGHBS:
					nH = getHeight(A, x + n[0], y + n[1], H, W)
					res += max(mH - nH, 0)
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def findArea(B):
	prev = B[0]
	s = prev
	for i in range(1, len(B)):
		if B[i] > prev:
			s += B[i] - prev
		prev = B[i]
	return s

def surfaceArea(A, H, W):
	s = 0
	for i in range(H):
		s += findArea(A[i])
	for i in range(W):
		s += findArea([row[i] for row in A])
	return s * 2 + H * W * 2
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def main():
	buffer = map(int, sys.stdin.read().split())
	H = next(buffer)
	W = next(buffer)
	board = [[0] * (W + 2) for _ in range(H + 2)]
	for i in range(1, H + 1):
		for j in range(1, W + 1):
			board[i][j] = next(buffer)
	total = 2 * H * W
	for i in range(1, H + 1):
		for j in range(1, W + 1):
			total += max(0, board[i][j] - board[i + 1][j])
			total += max(0, board[i][j] - board[i - 1][j])
			total += max(0, board[i][j] - board[i][j + 1])
			total += max(0, board[i][j] - board[i][j - 1])
	print(total)
main()


import sys

def surfaceArea(A, H, W):
	ans = 2 * H * W
	for i in range(1, H + 2):
		for j in range(1, W + 2):
			ans += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])
	return ans
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = [[0 for _ in range(W + 2)]]
for A_i in range(H):
	A_t = [0] + [int(A_temp) for A_temp in input().strip().split(' ')] + [0]
	A.append(A_t)
A.append([0 for _ in range(W + 2)])
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(A):
	height = len(A)
	width = len(A[0])
	area = height * width * 2
	for i in range(height):
		for j in range(width):
			for d in ((-1, 0), (+1, 0), (0, -1), (0, +1)):
				ii = i + d[0]
				jj = j + d[1]
				area += max(0, A[i][j] - (A[ii][jj] if 0 <= ii < height and 0 <= jj < width else 0))
	return area
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	a = 0
	h = len(A)
	w = len(A[0])
	for i in range(h):
		a += A[i][0]
		a += A[i][w - 1]
		for j in range(w - 1):
			x = A[i][j + 1] - A[i][j]
			y = A[i][w - 2 - j] - A[i][w - 1 - j]
			if x > 0:
				a += x
			if y > 0:
				a += y
	for i in range(w):
		a += A[0][i]
		a += A[h - 1][i]
		for j in range(h - 1):
			x = A[j + 1][i] - A[j][i]
			y = A[h - 2 - j][i] - A[h - 1 - j][i]
			if x > 0:
				a += x
			if y > 0:
				a += y
	b = [item for sublist in A for item in sublist]
	return a + (h * w - b.count(0)) * 2
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def to_arr(i, j):
	return i * W + j

def neibs(i, j):
	if i > 0:
		yield A[to_arr(i - 1, j)]
	if i < H - 1:
		yield A[to_arr(i + 1, j)]
	if j > 0:
		yield A[to_arr(i, j - 1)]
	if j < W - 1:
		yield A[to_arr(i, j + 1)]

def surfaceArea():
	res = 0
	for i in range(H):
		for j in range(W):
			val = A[to_arr(i, j)]
			res += 2 + val * 4 - sum((min(n, val) for n in neibs(i, j)))
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.extend(A_t)
result = surfaceArea()
print(result)


import sys

def total_non_zero(area):
	res = 0
	(nn, mm) = (len(area), len(area[0]))
	for i in range(nn):
		for j in range(mm):
			if area[i][j] > 0:
				res += 1
	return res

def surfaceArea(cubes, nn, mm):
	res = 0
	cubes_add = [[0] * (mm + 2)]
	for i in range(nn):
		cubes_i = cubes[i]
		cubes_i.append(0)
		cubes_i.insert(0, 0)
		cubes_add.append(cubes_i)
	cubes_add.append([0] * (mm + 2))
	bottom = total_non_zero(cubes)
	res = bottom * 2
	for i in range(1, nn + 1):
		for j in range(mm + 1):
			curr = cubes_add[i][j]
			for neighbor in [cubes_add[i - 1][j], cubes_add[i][j - 1], cubes_add[i + 1][j], cubes_add[i][j + 1]]:
				if curr > neighbor:
					res += curr - neighbor
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


(n, m) = map(int, input().split())
l = []
s = 0
for i in range(n):
	l.append(list(map(int, input().split())))
	for j in range(1, m):
		s = s + abs(l[i][j] - l[i][j - 1])
	s = s + l[i][0] + l[i][m - 1]
for i in range(1, n):
	for j in range(m):
		s = s + abs(l[i][j] - l[i - 1][j])
print(s + sum(l[0]) + sum(l[n - 1]) + 2 * n * m)


import sys

def calc1(A, s):
	luas = 0
	for i in range(s):
		a = [row[i] for row in A]
		b0 = 0
		for b in a:
			if b != b0:
				luas = luas + abs(b - b0)
			luas = luas + 1
			b0 = b
		luas = luas + b
	return luas

def calc2(A, s):
	luas = 0
	for i in range(s):
		b0 = 0
		for b in A[i]:
			if b != b0:
				luas = luas + abs(b - b0)
			b0 = b
		luas = luas + b
	return luas

def surfaceArea(A, H, W):
	s = len(A)
	return calc1(A, W) + calc2(A, H) + W * H
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def surfaceArea(a):
	n = len(a)
	m = len(a[0])
	sumi = 0
	for i in range(n):
		for j in range(m):
			if a[i][j] != 0:
				sumi += 2
	for i in range(n):
		for j in range(m):
			if j > 0:
				sumi += abs(a[i][j] - a[i][j - 1])
			else:
				sumi += a[i][j]
			if i > 0:
				sumi += abs(a[i][j] - a[i - 1][j])
			else:
				sumi += a[i][j]
	for i in range(n):
		sumi += a[i][m - 1]
	for j in range(m):
		sumi += a[n - 1][j]
	return sumi
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(a):
	res = sum(a[0]) + sum(a[-1])
	ii = 0
	for i in a:
		res += len(i) * 2
		res += i[0] + i[-1]
		for x in range(len(i)):
			if ii != len(a) - 1:
				res += abs(a[ii][x] - a[ii + 1][x])
			if x == len(i) - 1:
				break
			res += abs(i[x] - i[x + 1])
		ii += 1
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A, H, W):
	sum = 0
	for i in range(H):
		for j in range(W):
			sum += 2
			if i == 0 and j == 0:
				sum += A[i][j] * 2
			elif i == 0 and j == W - 1:
				sum += A[i][j] + abs(A[i][j] - A[i][j - 1])
			elif i == H - 1 and j == 0:
				sum += A[i][j] + abs(A[i][j] - A[i - 1][j])
			elif i == H - 1 and j == W - 1:
				sum += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])
			elif i == 0:
				sum += abs(A[i][j] - A[i][j - 1]) + A[i][j]
			elif j == 0:
				sum += A[i][j] + abs(A[i][j] - A[i - 1][j])
			elif i == H - 1 or j == W - 1:
				sum += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])
			else:
				sum += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])
	for i in range(H):
		sum = sum + A[i][W - 1]
	for j in range(W):
		sum = sum + A[H - 1][j]
	return sum
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A, H, W)
print(result)


import sys

def rowSurface(row):
	surface = row[0]
	for i in range(1, len(row)):
		surface += row[i] - row[i - 1] if row[i] - row[i - 1] > 0 else 0
	return surface

def surfaceArea(A):
	numRows = len(A)
	numColumns = len(A[0])
	transA = [list(t) for t in zip(*A)]
	front = 0
	back = 0
	for row in A:
		front += rowSurface(row)
		row.reverse()
		back += rowSurface(row)
	left = 0
	right = 0
	for row in transA:
		left += rowSurface(row)
		row.reverse()
		right += rowSurface(row)
	return numRows * numColumns * 2 + front + back + left + right
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def neighbours(A, i, j):
	yield (A[i - 1][j] if i > 0 else 0)
	yield (A[i][j - 1] if j > 0 else 0)
	yield (A[i + 1][j] if i < H - 1 else 0)
	yield (A[i][j + 1] if j < W - 1 else 0)

def area(A, i, j):
	surface = 0
	for n in neighbours(A, i, j):
		surface += max(0, A[i][j] - n)
	return surface

def surfaceArea(A):
	res = 0
	for i in range(H):
		for j in range(W):
			res += area(A, i, j) + 2
	return res
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


(m, n) = [int(x) for x in input().strip().split(' ')]
matrix = []
for _ in range(m):
	matrix.append([int(x) for x in input().strip().split(' ')])
permutations = [(-1, 0), (0, -1), (1, 0), (0, 1)]
surfaces = 0
for i in range(m):
	for j in range(n):
		curr = matrix[i][j]
		if curr > 0:
			surfaces += curr * 4 + 2
		else:
			surfaces += 0
			continue
		for k in permutations:
			a = i + k[0]
			b = j + k[1]
			if not (a >= 0 and a < m and (b >= 0) and (b < n)):
				continue
			val = matrix[i + k[0]][j + k[1]]
			if curr > val:
				surfaces -= val
			else:
				surfaces -= curr
print(surfaces)


def sub(i, j):
	c1 = min(b[i - 1][j], b[i][j])
	c2 = min(b[i + 1][j], b[i][j])
	c3 = min(b[i][j - 1], b[i][j])
	c4 = min(b[i][j + 1], b[i][j])
	return c1 + c2 + c3 + c4
(h, w) = map(int, input().split(' '))
arr = []
for i in range(h):
	arr.append(list(map(int, input().split(' '))))
a = [[0 for i in range(w + 2)] for i in range(h + 2)]
b = [[0 for i in range(w + 2)] for i in range(h + 2)]
for i in range(h):
	for j in range(w):
		b[i + 1][j + 1] = arr[i][j]
for i in range(1, h + 1):
	for j in range(1, w + 1):
		a[i][j] = b[i][j] * 6 - (b[i][j] - 1) * 2 - sub(i, j)
sum = 0
for i in range(1, h + 1):
	for j in range(1, w + 1):
		sum += a[i][j]
print(sum)


import sys

def surfaceArea(A):
	sum1 = 0
	h = len(A)
	w = len(A[0])
	sum1 = sum(A[0]) + sum(A[h - 1])
	sum1 += 2 * len(A) * len(A[0])
	arr2 = list(map(list, zip(*A)))
	for i in range(w):
		sum1 += sum([abs(arr2[i][j - 1] - arr2[i][j]) for j in range(1, h)])
	for i in range(h):
		sum1 += A[i][0] + A[i][-1]
		sum1 += sum([abs(A[i][j - 1] - A[i][j]) for j in range(1, w)])
	return sum1
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(a):
	s = 2 * len(a) * len(a[0])
	for i in a[0]:
		s += i
	for i in a[len(a) - 1]:
		s += i
	for i in range(len(a)):
		for j in range(len(a[0])):
			if j == 0:
				s += a[i][j]
			if j == len(a[0]) - 1:
				s += a[i][j]
			if j != len(a[0]) - 1:
				s += abs(a[i][j] - a[i][j + 1])
	for i in range(len(a) - 1):
		for j in range(len(a[0])):
			s += abs(a[i][j] - a[i + 1][j])
	return s
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
u = 2 * H * W
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
for i in range(H):
	s = A[i][0] + A[i][W - 1]
	if W > 1:
		for j in range(1, W):
			s += abs(A[i][j] - A[i][j - 1])
	u += s
for i in range(W):
	s = A[0][i] + A[H - 1][i]
	if H > 1:
		for j in range(1, H):
			s += abs(A[j][i] - A[j - 1][i])
	u += s
print(u)


result = 0
table = []
(h, w) = [int(i) for i in input().split()]
for _ in range(h):
	table.append([int(i) for i in input().split()])
for i in range(100):
	for row in table:
		flag = False
		for cell in row:
			if not flag:
				if cell > i:
					result += 1
					flag = True
			elif cell <= i:
				flag = False
	for width in range(w):
		flag = False
		for row in range(h):
			if not flag:
				if table[row][width] > i:
					result += 1
					flag = True
			elif table[row][width] <= i:
				flag = False
result *= 2
print(result + w * h * 2)


(h, w) = list(map(int, input().split()))
arr = [0 for i in range(h)]
for i in range(h):
	arr[i] = list(map(int, input().split()))
surface_area = 0
for i in range(h):
	for j in range(w):
		surface_area += 4 * arr[i][j] + 2
		if j != w - 1:
			surface_area -= min(arr[i][j], arr[i][j + 1])
		if i != h - 1:
			surface_area -= min(arr[i][j], arr[i + 1][j])
		if i != 0:
			surface_area -= min(arr[i][j], arr[i - 1][j])
		if j != 0:
			surface_area -= min(arr[i][j], arr[i][j - 1])
print(surface_area)


import sys

def surfaceArea(A):
	area = 0
	rows = len(A)
	cols = len(A[0])
	for i in range(rows):
		for j in range(cols):
			mat_val = A[i][j]
			for k in range(1, mat_val + 1):
				if j - 1 < 0:
					area += 1
				elif A[i][j - 1] < k:
					area += 1
				if j + 1 >= cols:
					area += 1
				elif A[i][j + 1] < k:
					area += 1
				if i + 1 >= rows:
					area += 1
				elif A[i + 1][j] < k:
					area += 1
				if i - 1 < 0:
					area += 1
				elif A[i - 1][j] < k:
					area += 1
				if k == 1:
					area += 1
				if k == mat_val:
					area += 1
	return area
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	(h, w) = (len(A), len(A[0]))
	surf = 2 * h * w
	for line in A:
		for j in range(w):
			if j == 0:
				surf += line[j]
			if j == len(line) - 1:
				surf += line[j]
			else:
				surf += abs(line[j + 1] - line[j])
	for j in range(w):
		for i in range(h):
			if i == 0:
				surf += A[i][j]
			if i == h - 1:
				surf += A[i][j]
			else:
				surf += abs(A[i + 1][j] - A[i][j])
	return surf
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	assert len(A_t) == W
	A.append(A_t)
assert len(A) == H
result = surfaceArea(A)
print(result)


import sys

def surfaceArea(A):
	result = 2 * len(A) * len(A[0])
	for i in range(len(A)):
		for j in range(len(A[0])):
			if i == 0:
				result = result + A[i][j]
			elif A[i][j] > A[i - 1][j]:
				result = result + A[i][j] - A[i - 1][j]
			if i == len(A) - 1:
				result = result + A[i][j]
			elif A[i][j] > A[i + 1][j]:
				result = result + A[i][j] - A[i + 1][j]
			if j == 0:
				result = result + A[i][j]
			elif A[i][j] > A[i][j - 1]:
				result = result + A[i][j] - A[i][j - 1]
			if j == len(A[0]) - 1:
				result = result + A[i][j]
			elif A[i][j] > A[i][j + 1]:
				result = result + A[i][j] - A[i][j + 1]
	return result
(H, W) = input().strip().split(' ')
(H, W) = [int(H), int(W)]
A = []
for A_i in range(H):
	A_t = [int(A_temp) for A_temp in input().strip().split(' ')]
	A.append(A_t)
result = surfaceArea(A)
print(result)