solutions: ["import sys\n\ndef surfaceArea(A):\n\tans = 0\n\tfor i in range(len(A)):\n\t\tfor j in range(len(A[0])):\n\t\t\tfor (dx, dy) in [(-1, 0), (0, 1), (1, 0), (0, -1)]:\n\t\t\t\tnx = j + dx\n\t\t\t\tny = i + dy\n\t\t\t\tif 0 <= nx < len(A[0]) and 0 <= ny < len(A):\n\t\t\t\t\tif nx >= j and ny >= i:\n\t\t\t\t\t\tans += abs(A[i][j] - A[ny][nx])\n\t\t\t\telse:\n\t\t\t\t\tans += A[i][j]\n\treturn ans\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result + H * W + H * W)\n", "import sys\n\ndef surfaceArea(A):\n\tarea = 0\n\tfor y in range(H):\n\t\tfor x in range(W):\n\t\t\th = A[y][x]\n\t\t\tarea += 2 + 4 * h\n\t\t\tif x > 0:\n\t\t\t\thl = A[y][x - 1]\n\t\t\t\tarea -= min(hl, h)\n\t\t\tif x < W - 1:\n\t\t\t\thr = A[y][x + 1]\n\t\t\t\tarea -= min(hr, h)\n\t\t\tif y > 0:\n\t\t\t\thu = A[y - 1][x]\n\t\t\t\tarea -= min(hu, h)\n\t\t\tif y < H - 1:\n\t\t\t\thd = A[y + 1][x]\n\t\t\t\tarea -= min(hd, h)\n\treturn area\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tsol = 2 * W * H\n\tfor i in range(H):\n\t\tfor j in range(W + 1):\n\t\t\ta = A[i][j - 1] if j > 0 else 0\n\t\t\tb = A[i][j] if j < W else 0\n\t\t\tsol += abs(a - b)\n\tfor i in range(H + 1):\n\t\tfor j in range(W):\n\t\t\ta = A[i - 1][j] if i > 0 else 0\n\t\t\tb = A[i][j] if i < H else 0\n\t\t\tsol += abs(a - b)\n\treturn sol\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = [[] for j in range(H)]\nfor y in range(H):\n\tA[y] = [int(A_temp) for A_temp in input().strip().split(' ')]\narea = 0\nfor y in range(H):\n\tfor x in range(W):\n\t\tif A[y][x]:\n\t\t\tarea += 2\n\t\t\tif y > 0:\n\t\t\t\tif A[y][x] > A[y - 1][x]:\n\t\t\t\t\tarea += A[y][x] - A[y - 1][x]\n\t\t\telse:\n\t\t\t\tarea += A[y][x]\n\t\t\tif y < H - 1:\n\t\t\t\tif A[y][x] > A[y + 1][x]:\n\t\t\t\t\tarea += A[y][x] - A[y + 1][x]\n\t\t\telse:\n\t\t\t\tarea += A[y][x]\n\t\t\tif x > 0:\n\t\t\t\tif A[y][x] > A[y][x - 1]:\n\t\t\t\t\tarea += A[y][x] - A[y][x - 1]\n\t\t\telse:\n\t\t\t\tarea += A[y][x]\n\t\t\tif x < W - 1:\n\t\t\t\tif A[y][x] > A[y][x + 1]:\n\t\t\t\t\tarea += A[y][x] - A[y][x + 1]\n\t\t\telse:\n\t\t\t\tarea += A[y][x]\nprint(area)\n", "import sys\n\ndef surfaceArea(A):\n\tn = len(A)\n\tm = len(A[0])\n\tA = [[0] * m] + A + [[0] * m]\n\tfor i in range(len(A)):\n\t\tA[i] = [0] + A[i] + [0]\n\tsol = 0\n\tfor i in range(1, n + 1):\n\t\tfor j in range(1, m + 1):\n\t\t\tif A[i][j] > A[i - 1][j]:\n\t\t\t\tsol += A[i][j] - A[i - 1][j]\n\t\t\tif A[i][j] > A[i + 1][j]:\n\t\t\t\tsol += A[i][j] - A[i + 1][j]\n\t\t\tif A[i][j] > A[i][j - 1]:\n\t\t\t\tsol += A[i][j] - A[i][j - 1]\n\t\t\tif A[i][j] > A[i][j + 1]:\n\t\t\t\tsol += A[i][j] - A[i][j + 1]\n\tsol += 2 * (n * m)\n\treturn sol\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\ttotal = len(A) * len(A[0]) * 2\n\tfor row in A:\n\t\tcur = 0\n\t\tfor val in row:\n\t\t\ttotal += abs(cur - val)\n\t\t\tcur = val\n\t\ttotal += cur\n\ttrans_A = [[row[i] for row in A] for i in range(len(A[0]))]\n\tfor row in trans_A:\n\t\tcur = 0\n\t\tfor val in row:\n\t\t\ttotal += abs(cur - val)\n\t\t\tcur = val\n\t\ttotal += cur\n\treturn total\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor _ in range(H):\n\trow = [int(val) for val in input().strip().split(' ')]\n\tA.append(row)\nresult = surfaceArea(A)\nprint(result)\n", "(h, w) = map(int, input().split())\na = []\nfor i in range(h):\n\ta.append(tuple(map(int, input().split())))\nans = 0\nfor i in range(h):\n\tfor j in range(w):\n\t\tif i == 0:\n\t\t\tans += a[i][j]\n\t\tif i == h - 1:\n\t\t\tans += a[i][j]\n\t\tif j == 0:\n\t\t\tans += a[i][j]\n\t\tif j == w - 1:\n\t\t\tans += a[i][j]\n\t\tif a[i][j]:\n\t\t\tans += 2\n\t\tif i < h - 1:\n\t\t\tans += abs(a[i][j] - a[i + 1][j])\n\t\tif j < w - 1:\n\t\t\tans += abs(a[i][j] - a[i][j + 1])\nprint(ans)\n", "(h, w) = [int(x) for x in input().split()]\na = []\nfor _ in range(h):\n\ta.append(list(map(int, input().split())))\nans = 2 * h * w\nfor i in range(h):\n\tfor j in range(w):\n\t\tif i > 0:\n\t\t\tans += max(a[i][j] - a[i - 1][j], 0)\n\t\telse:\n\t\t\tans += a[0][j]\n\t\tif j > 0:\n\t\t\tans += max(a[i][j] - a[i][j - 1], 0)\n\t\telse:\n\t\t\tans += a[i][0]\n\t\tif i < h - 1:\n\t\t\tans += max(a[i][j] - a[i + 1][j], 0)\n\t\telse:\n\t\t\tans += a[h - 1][j]\n\t\tif j < w - 1:\n\t\t\tans += max(a[i][j] - a[i][j + 1], 0)\n\t\telse:\n\t\t\tans += a[i][w - 1]\nprint(ans)\n", "import sys\n\ndef surfaceArea(A):\n\trows = len(A)\n\tif rows == 0:\n\t\treturn 0\n\tcols = len(A[0])\n\ts = 2 * rows * cols\n\tfor i in range(0, rows):\n\t\tfor j in range(0, cols):\n\t\t\tup = 0 if i == 0 else A[i - 1][j]\n\t\t\tdown = 0 if i == rows - 1 else A[i + 1][j]\n\t\t\tleft = 0 if j == 0 else A[i][j - 1]\n\t\t\tright = 0 if j == cols - 1 else A[i][j + 1]\n\t\t\ts += max(A[i][j] - up, 0)\n\t\t\ts += max(A[i][j] - down, 0)\n\t\t\ts += max(A[i][j] - left, 0)\n\t\t\ts += max(A[i][j] - right, 0)\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef get(A, row, col):\n\tif len(A) == 0:\n\t\treturn 0\n\tif row >= len(A) or col >= len(A[0]):\n\t\treturn 0\n\tif row < 0 or col < 0:\n\t\treturn 0\n\treturn A[row][col]\n\ndef surfaceArea(A, H, W):\n\ts = 0\n\tfor r in range(H):\n\t\tfor c in range(W):\n\t\t\tv = A[r][c]\n\t\t\tif v > 0:\n\t\t\t\ts += 2\n\t\t\ts += max(0, v - get(A, r - 1, c))\n\t\t\ts += max(0, v - get(A, r + 1, c))\n\t\t\ts += max(0, v - get(A, r, c - 1))\n\t\t\ts += max(0, v - get(A, r, c + 1))\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tres = H * W\n\tfor (i, row) in enumerate(A):\n\t\tfor (j, el) in enumerate(row):\n\t\t\tres += 1\n\t\t\tif i == 0:\n\t\t\t\tres += el\n\t\t\telse:\n\t\t\t\tres += abs(el - A[i - 1][j])\n\t\t\tif i == H - 1:\n\t\t\t\tres += el\n\t\t\tif j == 0:\n\t\t\t\tres += el\n\t\t\telse:\n\t\t\t\tres += abs(el - A[i][j - 1])\n\t\t\tif j == W - 1:\n\t\t\t\tres += el\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tn = len(A)\n\tm = len(A[0])\n\th_dict = {}\n\tfor x in range(n):\n\t\tfor y in range(m):\n\t\t\th_dict[x, y] = A[x][y]\n\tdelta = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n\ttotal = 2 * n * m\n\tfor x in range(n):\n\t\tfor y in range(m):\n\t\t\tfor (dx, dy) in delta:\n\t\t\t\tneighbor = (x + dx, y + dy)\n\t\t\t\tif h_dict.get(neighbor) is None:\n\t\t\t\t\ttotal += h_dict[x, y]\n\t\t\t\t\tcontinue\n\t\t\t\tdh = h_dict[x, y] - h_dict[neighbor]\n\t\t\t\tif dh > 0:\n\t\t\t\t\ttotal += dh\n\treturn total\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tres = 0\n\tres += len(A) * len(A[0]) * 2\n\th = len(A)\n\tw = len(A[0])\n\tfor i in range(h):\n\t\tres += A[i][0] + A[i][-1]\n\tfor j in range(w):\n\t\tres += A[0][j] + A[-1][j]\n\tfor i in range(h):\n\t\tfor j in range(1, w):\n\t\t\tres += abs(A[i][j] - A[i][j - 1])\n\tfor i in range(1, h):\n\t\tfor j in range(w):\n\t\t\tres += abs(A[i][j] - A[i - 1][j])\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "(H, W) = map(int, input().split())\nboard = []\nfor h0 in range(H):\n\tboard.append(list(map(int, input().split())))\nssurf = 2 * H * W\nfor i in range(H):\n\tfor j in range(W):\n\t\tc = 0\n\t\tc += max(board[i][j] - (board[i - 1][j] if i > 0 else 0), 0)\n\t\tc += max(board[i][j] - (board[i + 1][j] if i < H - 1 else 0), 0)\n\t\tc += max(board[i][j] - (board[i][j - 1] if j > 0 else 0), 0)\n\t\tc += max(board[i][j] - (board[i][j + 1] if j < W - 1 else 0), 0)\n\t\tssurf += c\nprint(ssurf)\n", "import sys\n\ndef surfaceArea(A):\n\tsurface = 0\n\th = len(A)\n\tw = len(A[0])\n\tfor i in range(h):\n\t\tfor j in range(w):\n\t\t\tsurface += 2\n\t\t\tif i == 0:\n\t\t\t\tsurface += A[i][j]\n\t\t\tif j == 0:\n\t\t\t\tsurface += A[i][j]\n\t\t\tif i == h - 1:\n\t\t\t\tsurface += A[i][j]\n\t\t\telse:\n\t\t\t\tsurface += abs(A[i][j] - A[i + 1][j])\n\t\t\tif j == w - 1:\n\t\t\t\tsurface += A[i][j]\n\t\t\telse:\n\t\t\t\tsurface += abs(A[i][j] - A[i][j + 1])\n\treturn surface\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tH = len(A)\n\tW = len(A[0])\n\tans = 2 * H * W\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tif i == 0:\n\t\t\t\tans += A[i][j]\n\t\t\tif j == 0:\n\t\t\t\tans += A[i][j]\n\t\t\tif j == W - 1:\n\t\t\t\tans += A[i][j]\n\t\t\telse:\n\t\t\t\tans += abs(A[i][j] - A[i][j + 1])\n\t\t\tif i == H - 1:\n\t\t\t\tans += A[i][j]\n\t\t\telse:\n\t\t\t\tans += abs(A[i][j] - A[i + 1][j])\n\treturn ans\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\nDD = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n\ndef surfaceArea(A, H, W):\n\tres = 0\n\tfor r in range(H):\n\t\tfor c in range(W):\n\t\t\tres += 2\n\t\t\tfor (dr, dc) in DD:\n\t\t\t\t(nr, nc) = (r + dr, c + dc)\n\t\t\t\tif nr < 0 or nc < 0 or nr >= H or (nc >= W):\n\t\t\t\t\tres += A[r][c]\n\t\t\t\telif A[nr][nc] < A[r][c]:\n\t\t\t\t\tres += A[r][c] - A[nr][nc]\n\treturn res\n(H, W) = map(int, input().strip().split(' '))\nA = []\nfor _ in range(H):\n\ta = tuple(map(int, input().strip().split(' ')))\n\tA.append(a)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "(h, w) = list(map(int, input().split()))\na = [list(map(int, input().split())) for i in range(h)]\nprice = 0\nfor i in range(h):\n\tfor j in range(w):\n\t\tfor k in range(a[i][j]):\n\t\t\tprice += 4\n\t\t\tif i > 0 and a[i - 1][j] - 1 >= k:\n\t\t\t\tprice -= 2\n\t\t\tif j > 0 and a[i][j - 1] - 1 >= k:\n\t\t\t\tprice -= 2\nprice += 2 * h * w\nprint(price)\n", "import sys\n\ndef count_row(X):\n\tt = len(X)\n\trv = X[0] + X[t - 1]\n\tfor i in range(t - 1):\n\t\trv += abs(X[i] - X[i + 1])\n\treturn rv\n\ndef surfaceArea(A):\n\tnum_rows = len(A)\n\tnum_cols = len(A[0])\n\tcount = 2 * num_rows * num_cols\n\tfor i in range(num_rows):\n\t\tcount += count_row(A[i])\n\tfor j in range(num_cols):\n\t\tX = [A[i][j] for i in range(num_rows)]\n\t\tcount += count_row(X)\n\treturn count\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef f(A, i, j, H, W):\n\tif (i < 0) | (j < 0):\n\t\treturn 0\n\tif (i > H - 1) | (j > W - 1):\n\t\treturn 0\n\treturn A[i][j]\n\ndef surfaceArea(A, W, H):\n\ts = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\ts += 2\n\t\t\tfor a in [-1, 1]:\n\t\t\t\ts += max(A[i][j] - f(A, i + a, j, H, W), 0)\n\t\t\t\ts += max(A[i][j] - f(A, i, j + a, H, W), 0)\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, W, H)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tans = W * H * 2\n\tfor i in range(H):\n\t\tans += A[i][0] + A[i][W - 1]\n\tfor i in range(W):\n\t\tans += A[0][i] + A[H - 1][i]\n\tfor i in range(H):\n\t\tfor j in range(W - 1):\n\t\t\tans += abs(A[i][j] - A[i][j + 1])\n\tfor j in range(W):\n\t\tfor i in range(H - 1):\n\t\t\tans += abs(A[i][j] - A[i + 1][j])\n\treturn ans\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "def getIntList():\n\treturn list(map(int, input().split(' ')))\n\ndef paint(row):\n\ttot = 0\n\tprev = 0\n\tfor x in row:\n\t\tif x > prev:\n\t\t\ttot += x - prev\n\t\tprev = x\n\treturn tot\n(h, w) = getIntList()\ntoy = [getIntList() for x in range(h)]\ns1 = sum([x > 0 for i in toy for x in i])\ns2 = sum([paint(row) for row in toy])\ns3 = sum([paint(row) for row in zip(*toy)])\nprint(2 * (s1 + s2 + s3))\n", "import sys\n\ndef surfaceArea(A):\n\tprice = 0\n\trows_count = len(A)\n\tfor i in range(rows_count):\n\t\tcolumns_count = len(A[i])\n\t\tfor j in range(columns_count):\n\t\t\tcurrent_value = A[i][j]\n\t\t\tif i - 1 < 0:\n\t\t\t\tprice += current_value\n\t\t\tif j - 1 < 0:\n\t\t\t\tprice += current_value\n\t\t\tif j + 1 == columns_count:\n\t\t\t\tprice += current_value\n\t\t\telse:\n\t\t\t\tprice += abs(A[i][j + 1] - current_value)\n\t\t\tif i + 1 == rows_count:\n\t\t\t\tprice += current_value\n\t\t\telse:\n\t\t\t\tprice += abs(A[i + 1][j] - current_value)\n\t\t\tprice += 2\n\treturn price\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(a, r, c):\n\tco = 0\n\tfor i in range(r):\n\t\tfor j in range(c):\n\t\t\tif j - 1 < 0:\n\t\t\t\tco = co + a[i][j]\n\t\t\telif a[i][j - 1] < a[i][j]:\n\t\t\t\tco = co + a[i][j] - a[i][j - 1]\n\t\t\tif j + 1 > c - 1:\n\t\t\t\tco = co + a[i][j]\n\t\t\telif a[i][j + 1] < a[i][j]:\n\t\t\t\tco = co + a[i][j] - a[i][j + 1]\n\t\t\tif i - 1 < 0:\n\t\t\t\tco = co + a[i][j]\n\t\t\telif a[i - 1][j] < a[i][j]:\n\t\t\t\tco = co + a[i][j] - a[i - 1][j]\n\t\t\tif i + 1 > r - 1:\n\t\t\t\tco = co + a[i][j]\n\t\t\telif a[i + 1][j] < a[i][j]:\n\t\t\t\tco = co + a[i][j] - a[i + 1][j]\n\tco = co + 2 * r * c\n\treturn co\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tsum = 2 * H * W\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tvalue = A[i][j]\n\t\t\tif i - 1 < 0:\n\t\t\t\tsum += value\n\t\t\telse:\n\t\t\t\tsum += max(0, value - A[i - 1][j])\n\t\t\tif j + 1 > W - 1:\n\t\t\t\tsum += value\n\t\t\telse:\n\t\t\t\tsum += max(0, value - A[i][j + 1])\n\t\t\tif i + 1 > H - 1:\n\t\t\t\tsum += value\n\t\t\telse:\n\t\t\t\tsum += max(0, value - A[i + 1][j])\n\t\t\tif j - 1 < 0:\n\t\t\t\tsum += value\n\t\t\telse:\n\t\t\t\tsum += max(0, value - A[i][j - 1])\n\treturn sum\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, L, W):\n\tSA = 0\n\tfor i in range(0, L):\n\t\tfor j in range(0, W):\n\t\t\tif i - 1 >= 0 and j - 1 >= 0:\n\t\t\t\tSA += 2 + abs(A[i][j] - A[i - 1][j]) + abs(A[i][j] - A[i][j - 1])\n\t\t\telif i - 1 >= 0 and j - 1 == -1:\n\t\t\t\tSA += 2 + abs(A[i][j] - A[i - 1][j]) + abs(A[i][j])\n\t\t\telif j - 1 >= 0 and i - 1 == -1:\n\t\t\t\tSA += 2 + abs(A[i][j]) + abs(A[i][j] - A[i][j - 1])\n\t\t\telif i == 0 and j == 0:\n\t\t\t\tSA += 2 + abs(A[i][j]) + abs(A[i][j])\n\tfor i in range(0, W):\n\t\tSA += A[L - 1][i]\n\tfor i in range(0, L):\n\t\tSA += A[i][W - 1]\n\treturn SA\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(a, h, w):\n\tsu = sum(a[-1]) + sum(a[0]) + 2 * h * w\n\tfor j in range(h):\n\t\tsu = su + a[j][0] + a[j][-1]\n\tfor j in range(h):\n\t\tfor j1 in range(w - 1):\n\t\t\tsu = su + abs(a[j][j1] - a[j][j1 + 1])\n\tfor j in range(h - 1):\n\t\tfor j1 in range(w):\n\t\t\tsu = su + abs(a[j][j1] - a[j + 1][j1])\n\treturn su\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\ttotalArea = 0\n\tiMax = len(A) - 1\n\tjMax = len(A[0]) - 1\n\tfor i in range(len(A)):\n\t\tfor j in range(len(A[i])):\n\t\t\ttotalArea += 2\n\t\t\tif i - 1 >= 0:\n\t\t\t\tif A[i][j] > A[i - 1][j]:\n\t\t\t\t\ttotalArea += A[i][j] - A[i - 1][j]\n\t\t\telse:\n\t\t\t\ttotalArea += A[i][j]\n\t\t\tif i + 1 <= iMax:\n\t\t\t\tif A[i][j] > A[i + 1][j]:\n\t\t\t\t\ttotalArea += A[i][j] - A[i + 1][j]\n\t\t\telse:\n\t\t\t\ttotalArea += A[i][j]\n\t\t\tif j - 1 >= 0:\n\t\t\t\tif A[i][j] > A[i][j - 1]:\n\t\t\t\t\ttotalArea += A[i][j] - A[i][j - 1]\n\t\t\telse:\n\t\t\t\ttotalArea += A[i][j]\n\t\t\tif j + 1 <= jMax:\n\t\t\t\tif A[i][j] > A[i][j + 1]:\n\t\t\t\t\ttotalArea += A[i][j] - A[i][j + 1]\n\t\t\telse:\n\t\t\t\ttotalArea += A[i][j]\n\treturn totalArea\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef removen(A, i, j, H, W):\n\there = A[i][j]\n\tnorth = 0\n\tsouth = 0\n\teast = 0\n\twest = 0\n\tn = 0\n\tif here != 0:\n\t\tif i > 0:\n\t\t\tnorth = A[i - 1][j]\n\t\tif j > 0:\n\t\t\twest = A[i][j - 1]\n\t\tif i < H - 1:\n\t\t\tsouth = A[i + 1][j]\n\t\tif j < W - 1:\n\t\t\teast = A[i][j + 1]\n\t\tn = min(north, here) + min(south, here) + min(east, here) + min(west, here)\n\treturn n\n\ndef remove_neighbors(A, H, W):\n\ttot = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\ttot += removen(A, i, j, H, W)\n\treturn tot\n\ndef surfaceArea(A, H, W):\n\ttotal_without_neighbors = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tif A[i][j] == 1:\n\t\t\t\ttotal_without_neighbors += 6\n\t\t\telif A[i][j] == 2:\n\t\t\t\ttotal_without_neighbors += 10\n\t\t\telif A[i][j] > 2:\n\t\t\t\ttotal_without_neighbors += 10 + 4 * (A[i][j] - 2)\n\tneigh = remove_neighbors(A, H, W)\n\ttotal_without_neighbors -= neigh\n\treturn total_without_neighbors\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\ttop = bottom = len(A) * len(A[0])\n\tleft = sum(A[0])\n\tright = sum(A[-1])\n\tfront = 0\n\tback = 0\n\tmiddle = 0\n\tfor elem in range(len(A)):\n\t\tfront += A[elem][0]\n\t\tback += A[elem][-1]\n\t\tfor i in range(len(A[elem])):\n\t\t\tif i != len(A[elem]) - 1:\n\t\t\t\tmiddle += abs(A[elem][i + 1] - A[elem][i])\n\t\t\tif elem != len(A) - 1:\n\t\t\t\tmiddle += abs(A[elem + 1][i] - A[elem][i])\n\treturn top + bottom + left + right + front + back + middle\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tH = len(A) - 2\n\tW = len(A[0]) - 2\n\ts = H * W * 2\n\tfor i in range(1, H + 1):\n\t\tfor j in range(1, W + 1):\n\t\t\tfor k in range(1, A[i][j] + 1):\n\t\t\t\tif A[i - 1][j] < k:\n\t\t\t\t\ts += 1\n\t\t\t\tif A[i + 1][j] < k:\n\t\t\t\t\ts += 1\n\t\t\t\tif A[i][j - 1] < k:\n\t\t\t\t\ts += 1\n\t\t\t\tif A[i][j + 1] < k:\n\t\t\t\t\ts += 1\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = [[0] * (W + 2)]\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA_t.insert(0, 0)\n\tA_t.append(0)\n\tA.append(A_t)\nA.append([0] * (W + 2))\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, h, w):\n\tsu = 2 * h * w\n\tsu += sum(A[0]) + sum(A[-1])\n\tfor j in range(h):\n\t\tsu += A[j][0] + A[j][-1]\n\tfor i in range(h):\n\t\tfor j in range(w - 1):\n\t\t\tsu += abs(A[i][j] - A[i][j + 1])\n\tfor i in range(h - 1):\n\t\tfor j in range(w):\n\t\t\tsu += abs(A[i][j] - A[i + 1][j])\n\treturn su\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tanswer = len(A) * len(A[0]) * 2\n\tbefore = None\n\tfor i in range(len(A)):\n\t\tfor j in range(len(A[i])):\n\t\t\tif j == 0:\n\t\t\t\tanswer += A[i][j]\n\t\t\telif before > A[i][j]:\n\t\t\t\tpass\n\t\t\telse:\n\t\t\t\tanswer += A[i][j] - before\n\t\t\tbefore = A[i][j]\n\tfor i in range(len(A)):\n\t\tfor j in range(len(A[i]) - 1, -1, -1):\n\t\t\tif j == len(A[i]) - 1:\n\t\t\t\tanswer += A[i][j]\n\t\t\telif before > A[i][j]:\n\t\t\t\tpass\n\t\t\telse:\n\t\t\t\tanswer += A[i][j] - before\n\t\t\tbefore = A[i][j]\n\tfor j in range(len(A[0])):\n\t\tfor i in range(len(A)):\n\t\t\tif i == 0:\n\t\t\t\tanswer += A[i][j]\n\t\t\telif before > A[i][j]:\n\t\t\t\tpass\n\t\t\telse:\n\t\t\t\tanswer += A[i][j] - before\n\t\t\tbefore = A[i][j]\n\tfor j in range(len(A[0])):\n\t\tfor i in range(len(A) - 1, -1, -1):\n\t\t\tif i == len(A) - 1:\n\t\t\t\tanswer += A[i][j]\n\t\t\telif before > A[i][j]:\n\t\t\t\tpass\n\t\t\telse:\n\t\t\t\tanswer += A[i][j] - before\n\t\t\tbefore = A[i][j]\n\treturn answer\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "def s(x, y):\n\tif x - y > 0:\n\t\treturn x - y\n\telse:\n\t\treturn 0\n(h, w) = map(int, input().split())\ncube = []\ncube.append([0] * (w + 2))\nfor i in range(h):\n\tcube.append(list(map(int, input().split())))\ncube.append([0] * (w + 2))\nfor i in range(1, h + 1):\n\tcube[i].insert(0, 0)\n\tcube[i].append(0)\narea = 0\narea += 2 * h * w\nh = h + 2\nw = w + 2\nfor i in range(1, h - 1):\n\tfor j in range(1, w - 1):\n\t\tarea += s(cube[i][j], cube[i][j + 1])\n\t\tarea += s(cube[i][j], cube[i][j - 1])\n\t\tarea += s(cube[i][j], cube[i + 1][j])\n\t\tarea += s(cube[i][j], cube[i - 1][j])\nprint(area)\n", "import sys\n\ndef surfaceArea(A):\n\tbottom = top = H * W\n\tleft = right = front = back = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tif j == 0:\n\t\t\t\tleft += A[i][j]\n\t\t\telse:\n\t\t\t\tcalculate = A[i][j] - A[i][j - 1]\n\t\t\t\tif calculate > 0:\n\t\t\t\t\tleft += calculate\n\t\t\tif j == W - 1:\n\t\t\t\tright += A[i][j]\n\t\t\telse:\n\t\t\t\tcalculate = A[i][j] - A[i][j + 1]\n\t\t\t\tif calculate > 0:\n\t\t\t\t\tright += calculate\n\t\t\tif i == 0:\n\t\t\t\tfront += A[i][j]\n\t\t\telse:\n\t\t\t\tcalculate = A[i][j] - A[i - 1][j]\n\t\t\t\tif calculate > 0:\n\t\t\t\t\tfront += calculate\n\t\t\tif i == H - 1:\n\t\t\t\tback += A[i][j]\n\t\t\telse:\n\t\t\t\tcalculate = A[i][j] - A[i + 1][j]\n\t\t\t\tif calculate > 0:\n\t\t\t\t\tback += calculate\n\treturn top + bottom + left + right + front + back\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tbottom_top = H * W * 2\n\ttop_side = sum(A[0])\n\tbottom_side = sum(A[-1])\n\tleft_side = 0\n\tright_side = 0\n\thori_diff = 0\n\tvert_diff = 0\n\tfor i in range(len(A)):\n\t\tleft_side += A[i][0]\n\t\tright_side += A[i][-1]\n\tfor i in A:\n\t\thori_diff += sum([abs(j - i) for (i, j) in zip(i[:-1], i[1:])])\n\tswapped = zip(*A[::-1])\n\tfor i in swapped:\n\t\tvert_diff += sum([abs(j - i) for (i, j) in zip(i[:-1], i[1:])])\n\treturn left_side + right_side + hori_diff + vert_diff + bottom_top + top_side + bottom_side\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\ts = 0\n\tfor i in range(H):\n\t\ts += A[i][0]\n\tfor i in range(W):\n\t\ts += A[0][i]\n\tfor i in range(H):\n\t\ts += A[i][W - 1]\n\tfor i in range(W):\n\t\ts += A[H - 1][i]\n\tfor i in range(1, H):\n\t\tfor j in range(W):\n\t\t\ts += abs(A[i][j] - A[i - 1][j])\n\tfor i in range(H):\n\t\tfor j in range(1, W):\n\t\t\ts += abs(A[i][j] - A[i][j - 1])\n\ts += 2 * H * W\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tans = 0\n\tnans = 0\n\tfor i in range(1, len(A)):\n\t\tfor j in range(0, len(A[i]) - 1):\n\t\t\tans = ans + abs(A[i][j] - A[i][j + 1])\n\tfor j in range(1, len(A[0])):\n\t\tfor i in range(0, len(A) - 1):\n\t\t\tnans = nans + abs(A[i][j] - A[i + 1][j])\n\tk = (len(A) - 2) * (len(A[0]) - 2) * 2\n\treturn ans + nans + k\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nA_ = [0 for i in range(W + 2)]\nA.append(A_)\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA_t.append(0)\n\tA_t.insert(0, 0)\n\tA.append(A_t)\nA.append(A_)\nresult = surfaceArea(A)\nprint(result)\n", "def surfaceArea(A):\n\tc = 2 * len(A) * len(A[0])\n\tfor i in range(len(A)):\n\t\tc += A[i][0]\n\t\tfor j in range(1, len(A[i])):\n\t\t\tc += abs(A[i][j] - A[i][j - 1])\n\t\tc += A[i][-1]\n\tfor i in range(len(A[0])):\n\t\tc += A[0][i]\n\t\tfor j in range(1, len(A)):\n\t\t\tc += abs(A[j][i] - A[j - 1][i])\n\t\tc += A[-1][i]\n\treturn c\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\traw_area = 0\n\tflat_a = [item for sublist in A for item in sublist]\n\tfor a in flat_a:\n\t\traw_area += a * 4 + 2\n\tfor i in range(H - 1):\n\t\tfor j in range(W):\n\t\t\tshared = min(A[i][j], A[i + 1][j])\n\t\t\traw_area -= shared * 2\n\tfor i in range(H):\n\t\tfor j in range(W - 1):\n\t\t\tshared = min(A[i][j], A[i][j + 1])\n\t\t\traw_area -= shared * 2\n\treturn raw_area\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(a, r, c):\n\tans = sum(a[0]) + sum(a[r - 1]) + 2 * r * c\n\tfor i in range(r):\n\t\tfor j in range(c):\n\t\t\tif j == 0 or j == c - 1:\n\t\t\t\tans += a[i][j]\n\t\t\t\tif c - 1 == 0:\n\t\t\t\t\tans += a[i][j]\n\t\t\tif 0 < j:\n\t\t\t\tans += abs(a[i][j] - a[i][j - 1])\n\tfor i in range(c):\n\t\tfor j in range(1, r):\n\t\t\tans += abs(a[j][i] - a[j - 1][i])\n\treturn ans\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, W, H):\n\tans = 0\n\ttop_bottom = 0\n\tfor i in range(1, H + 1):\n\t\tfor j in range(1, W + 1):\n\t\t\tans += max(0, A[i][j] - A[i - 1][j])\n\t\t\tans += max(0, A[i][j] - A[i + 1][j])\n\t\t\tans += max(0, A[i][j] - A[i][j - 1])\n\t\t\tans += max(0, A[i][j] - A[i][j + 1])\n\t\t\tif A[i][j] != 0:\n\t\t\t\ttop_bottom += 2\n\treturn ans + top_bottom\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nA.append([0] * (W + 2))\nfor A_i in range(H):\n\tA_t = [0] + [int(A_temp) for A_temp in input().strip().split(' ')] + [0]\n\tA.append(A_t)\nA.append([0] * (W + 2))\nresult = surfaceArea(A, W, H)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\t(s1, s2, s3, s4) = (0, 0, 0, 0)\n\tfor i in range(H):\n\t\ts1 = s1 + A[i][0]\n\t\ts2 = s2 + A[i][W - 1]\n\t\tfor j in range(W):\n\t\t\tif j < W - 1:\n\t\t\t\ts3 = s3 + abs(A[i][j] - A[i][j + 1])\n\t\t\tif i < H - 1:\n\t\t\t\ts4 = s4 + abs(A[i][j] - A[i + 1][j])\n\treturn sum(A[0]) + sum(A[H - 1]) + 2 * H * W + s1 + s2 + s3 + s4\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(a, h, w):\n\tprice = 2 * h * w\n\tfor y in range(h):\n\t\tfor x in range(w):\n\t\t\tif x == 0:\n\t\t\t\tprice += a[y][x]\n\t\t\tif x == w - 1:\n\t\t\t\tprice += a[y][x]\n\t\t\tif y == 0:\n\t\t\t\tprice += a[y][x]\n\t\t\tif y == h - 1:\n\t\t\t\tprice += a[y][x]\n\t\t\tif x > 0 and a[y][x] > a[y][x - 1]:\n\t\t\t\tprice += a[y][x] - a[y][x - 1]\n\t\t\tif x + 1 < w and a[y][x] > a[y][x + 1]:\n\t\t\t\tprice += a[y][x] - a[y][x + 1]\n\t\t\tif y > 0 and a[y][x] > a[y - 1][x]:\n\t\t\t\tprice += a[y][x] - a[y - 1][x]\n\t\t\tif y + 1 < h and a[y][x] > a[y + 1][x]:\n\t\t\t\tprice += a[y][x] - a[y + 1][x]\n\treturn price\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surf(H, W, A, i, j):\n\tcur = A[i][j]\n\tif cur == 0:\n\t\treturn 0\n\ttop = A[i - 1][j] if i > 0 else 0\n\tbottom = A[i + 1][j] if i + 1 < H else 0\n\tleft = A[i][j - 1] if j > 0 else 0\n\tright = A[i][j + 1] if j + 1 < W else 0\n\treturn 2 + max(0, cur - left) + max(0, cur - right) + max(0, cur - top) + max(0, cur - bottom)\n\ndef surfaceArea(H, W, A):\n\tres = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tres += surf(H, W, A, i, j)\n\treturn res\n(H, W) = map(int, input().split())\nA = [list(map(int, input().split())) for _ in range(H)]\nresult = surfaceArea(H, W, A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tsolution = 2 * H * W\n\tif H == 1 and W == 1:\n\t\treturn solution + 4 * A[0][0]\n\tif H == 1:\n\t\tfor j in range(W):\n\t\t\tif j == 0:\n\t\t\t\tsolution += 3 * A[0][j] + max(0, A[0][j] - A[0][j + 1])\n\t\t\telif j == W - 1:\n\t\t\t\tsolution += 3 * A[0][j] + max(0, A[0][j] - A[0][j - 1])\n\t\t\telse:\n\t\t\t\tsolution += 2 * A[0][j] + max(0, A[0][j] - A[0][j - 1]) + max(0, A[0][j] - A[0][j + 1])\n\t\treturn solution\n\tif W == 1:\n\t\tfor i in range(H):\n\t\t\tif i == 0:\n\t\t\t\tsolution += 3 * A[i][0] + max(0, A[i][0] - A[i + 1][0])\n\t\t\telif i == H - 1:\n\t\t\t\tsolution += 3 * A[i][0] + max(0, A[i][0] - A[i - 1][0])\n\t\t\telse:\n\t\t\t\tsolution += 2 * A[i][0] + max(0, A[i][0] - A[i - 1][0]) + max(0, A[i][0] - A[i + 1][0])\n\t\treturn solution\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tif i == 0 and j == 0:\n\t\t\t\tsolution += 2 * A[i][j] + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j + 1])\n\t\t\telif i == 0 and j == W - 1:\n\t\t\t\tsolution += 2 * A[i][j] + max(0, A[i][j] - A[i + 1][j]) + max(0, A[i][j] - A[i][j - 1])\n\t\t\telif i == H - 1 and j == 0:\n\t\t\t\tsolution += 2 * A[i][j] + max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i][j + 1])\n\t\t\telif i == H - 1 and j == W - 1:\n\t\t\t\tsolution += 2 * A[i][j] + max(0, A[i][j] - A[i - 1][j]) + max(0, A[i][j] - A[i][j - 1])\n\t\t\telif i == 0:\n\t\t\t\tsolution += 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])\n\t\t\telif i == H - 1:\n\t\t\t\tsolution += 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])\n\t\t\telif j == 0:\n\t\t\t\tsolution += 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])\n\t\t\telif j == W - 1:\n\t\t\t\tsolution += 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])\n\t\t\telse:\n\t\t\t\tsolution += 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])\n\treturn solution\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor _ in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\nfrom collections import defaultdict\n\ndef surfaceArea(M):\n\ti = 1\n\tj = 1\n\tsu = 0\n\twhile M[i, j]:\n\t\twhile M[i, j]:\n\t\t\tsu += M[i, j] * 4 + 2\n\t\t\tsu -= min(M[i, j], M[i - 1, j])\n\t\t\tsu -= min(M[i, j], M[i + 1, j])\n\t\t\tsu -= min(M[i, j], M[i, j - 1])\n\t\t\tsu -= min(M[i, j], M[i, j + 1])\n\t\t\tj += 1\n\t\ti += 1\n\t\tj = 1\n\treturn su\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nM = defaultdict(int)\ni = 1\nfor _ in range(H):\n\tr = map(int, input().strip().split(' '))\n\tj = 1\n\tfor c in r:\n\t\tM[i, j] = c\n\t\tj += 1\n\ti += 1\nresult = surfaceArea(M)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tm = len(A)\n\tn = len(A[0])\n\tbase = m * n\n\tsides = extras = 0\n\tfor c in range(n):\n\t\tsides += A[0][c] + A[m - 1][c]\n\tfor r in range(m):\n\t\tsides += A[r][0] + A[r][n - 1]\n\tfor r in range(m):\n\t\tfor c in range(1, n):\n\t\t\textras += abs(A[r][c - 1] - A[r][c])\n\tfor r in range(1, m):\n\t\tfor c in range(n):\n\t\t\textras += abs(A[r - 1][c] - A[r][c])\n\treturn base * 2 + sides + extras\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "(r, c) = map(int, input().split())\na = []\nfor i in range(r):\n\ta.append(list(map(int, input().split())))\nif r == 1 and c == 1:\n\tprint(2 + 4 * a[0][0])\nelif r == 1:\n\tpa = 2 * c + 3 * a[0][0] + 3 * a[0][c - 1]\n\tfor i in range(1, c - 1):\n\t\tpa += 2 * a[0][i]\n\tfor i in range(c - 1):\n\t\tpa += abs(a[0][i + 1] - a[0][i])\n\tprint(pa)\nelif c == 1:\n\tpa = 2 * r + 3 * a[0][0] + 3 * a[r - 1][0]\n\tfor i in range(1, r - 1):\n\t\tpa += 2 * a[i][0]\n\tfor i in range(r - 1):\n\t\tpa += abs(a[i + 1][0] - a[i][0])\n\tprint(pa)\nelse:\n\tans = 2 * r * c + 2 * a[0][0] + 2 * a[r - 1][c - 1] + 2 * a[0][c - 1] + 2 * a[r - 1][0]\n\tfor i in range(1, r - 1):\n\t\tans += a[i][0] + a[i][c - 1]\n\tfor i in range(1, c - 1):\n\t\tans += a[0][i] + a[r - 1][i]\n\tfor j in range(r):\n\t\tfor i in range(c - 1):\n\t\t\tans += abs(a[j][i + 1] - a[j][i])\n\tfor j in range(c):\n\t\tfor i in range(r - 1):\n\t\t\tans += abs(a[i + 1][j] - a[i][j])\n\tprint(ans)\n", "(H, W) = list(map(int, input().strip().split()))\nA = [[None for j in range(W)] for i in range(H)]\nArea = [[None for j in range(W)] for i in range(H)]\nfor i in range(H):\n\tA[i] = list(map(int, input().strip().split()))\n\tArea[i] = [2 * (1 + 2 * h) for h in A[i]]\nTotal_area = 0\nfor i in range(H):\n\tfor j in range(W):\n\t\tif i < H - 1:\n\t\t\tif A[i + 1][j] > A[i][j]:\n\t\t\t\tArea[i][j] -= A[i][j]\n\t\t\telse:\n\t\t\t\tArea[i][j] -= A[i + 1][j]\n\t\tif i > 0:\n\t\t\tif A[i - 1][j] > A[i][j]:\n\t\t\t\tArea[i][j] -= A[i][j]\n\t\t\telse:\n\t\t\t\tArea[i][j] -= A[i - 1][j]\n\t\tif j < W - 1:\n\t\t\tif A[i][j + 1] > A[i][j]:\n\t\t\t\tArea[i][j] -= A[i][j]\n\t\t\telse:\n\t\t\t\tArea[i][j] -= A[i][j + 1]\n\t\tif j > 0:\n\t\t\tif A[i][j - 1] > A[i][j]:\n\t\t\t\tArea[i][j] -= A[i][j]\n\t\t\telse:\n\t\t\t\tArea[i][j] -= A[i][j - 1]\n\tTotal_area += sum(Area[i])\nprint(Total_area)\n", "import sys\n\ndef surfaceArea(A):\n\t(X, Y, s) = (len(A) - 1, len(A[0]) - 1, 0)\n\tfor (x, r) in enumerate(A):\n\t\tfor (y, c) in enumerate(r):\n\t\t\ts += 2\n\t\t\tif x == 0:\n\t\t\t\ts += c\n\t\t\tif x == X:\n\t\t\t\ts += c\n\t\t\tif y == 0:\n\t\t\t\ts += c\n\t\t\tif y == Y:\n\t\t\t\ts += c\n\t\t\tif x != 0 and A[x - 1][y] < c:\n\t\t\t\ts += c - A[x - 1][y]\n\t\t\tif x != X and A[x + 1][y] < c:\n\t\t\t\ts += c - A[x + 1][y]\n\t\t\tif y != 0 and A[x][y - 1] < c:\n\t\t\t\ts += c - A[x][y - 1]\n\t\t\tif y != Y and A[x][y + 1] < c:\n\t\t\t\ts += c - A[x][y + 1]\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef get_diff(A, h, w, i, j):\n\tdx = [1, 0, 0, -1]\n\tdy = [0, 1, -1, 0]\n\tdiff_sum = 0\n\tfor di in range(len(dx)):\n\t\tpi = i + dx[di]\n\t\tpj = j + dy[di]\n\t\tif pi < 0 or pi >= h or pj < 0 or (pj >= w):\n\t\t\tcontinue\n\t\tdiff = max(0, A[i][j] - A[pi][pj])\n\t\tdiff_sum += diff\n\treturn diff_sum\n\ndef surfaceArea(A, h, w):\n\ttop_bottom = h * w * 2\n\tsides = 0\n\tfor i in range(h):\n\t\tsides += A[i][0] + A[i][w - 1]\n\tfor j in range(w):\n\t\tsides += A[0][j] + A[h - 1][j]\n\tdiff_sum = 0\n\tfor i in range(h):\n\t\tfor j in range(w):\n\t\t\tdiff = get_diff(A, h, w, i, j)\n\t\t\tdiff_sum += diff\n\treturn top_bottom + sides + diff_sum\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\nimport math\n\ndef surfaceArea(A):\n\th = len(A)\n\tw = len(A[0])\n\ttotalprice = 0\n\tfor i in range(h):\n\t\tprice = 0\n\t\tfor j in range(w):\n\t\t\tprice += 2\n\t\t\tif i == 0:\n\t\t\t\tprice += A[i][j]\n\t\t\tif j == 0:\n\t\t\t\tprice += A[i][j]\n\t\t\tif j == w - 1:\n\t\t\t\tprice += A[i][j]\n\t\t\tif i == h - 1:\n\t\t\t\tprice += A[i][j]\n\t\t\tif j < w - 1:\n\t\t\t\tprice += abs(A[i][j] - A[i][j + 1])\n\t\t\tif i < h - 1:\n\t\t\t\tprice += abs(A[i][j] - A[i + 1][j])\n\t\ttotalprice += price\n\treturn totalprice\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "(h, w) = map(int, input().split())\n(l, s) = ([], 0)\nfor i in range(h):\n\tx = list(map(int, input().split()))\n\ts += sum(x)\n\tl.append(x)\nprice = s * 6 - (s - h * w) * 2\nm = 0\nfor i in range(h):\n\tfor j in range(w - 1):\n\t\tm += min(l[i][j], l[i][j + 1])\nfor i in range(h - 1):\n\tfor j in range(w):\n\t\tm += min(l[i][j], l[i + 1][j])\nprice -= m * 2\nprint(price)\n", "import sys\n\ndef side(A, curr, i, j):\n\tif i < 0 or j < 0 or i >= len(A) or (j >= len(A[i])):\n\t\treturn curr\n\telif curr <= A[i][j]:\n\t\treturn 0\n\telse:\n\t\treturn curr - A[i][j]\n\ndef surfaceArea(A):\n\tsurface = len(A) * len(A[0]) * 2\n\tfor i in range(len(A)):\n\t\tfor j in range(len(A[i])):\n\t\t\tsurface += side(A, A[i][j], i - 1, j)\n\t\t\tsurface += side(A, A[i][j], i + 1, j)\n\t\t\tsurface += side(A, A[i][j], i, j + 1)\n\t\t\tsurface += side(A, A[i][j], i, j - 1)\n\treturn surface\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, n, m):\n\ttotal = 2 * n * m\n\tcurret = 0\n\tface = 0\n\tfor i in range(m):\n\t\tcurret = 0\n\t\tfor j in range(n):\n\t\t\tif A[j][i] > curret:\n\t\t\t\tface += A[j][i] - curret\n\t\t\tcurret = A[j][i]\n\ttotal += face\n\tface = 0\n\tfor i in range(m):\n\t\tcurret = 0\n\t\tfor j in range(n):\n\t\t\tif A[n - j - 1][m - i - 1] > curret:\n\t\t\t\tface += A[n - j - 1][m - i - 1] - curret\n\t\t\tcurret = A[n - j - 1][m - i - 1]\n\ttotal += face\n\tface = 0\n\tfor i in range(n):\n\t\tcurret = 0\n\t\tfor j in range(m):\n\t\t\tif A[i][j] > curret:\n\t\t\t\tface += A[i][j] - curret\n\t\t\tcurret = A[i][j]\n\ttotal += face\n\tface = 0\n\tfor i in range(n):\n\t\tcurret = 0\n\t\tfor j in range(m):\n\t\t\tif A[n - i - 1][m - j - 1] > curret:\n\t\t\t\tface += A[n - i - 1][m - j - 1] - curret\n\t\t\tcurret = A[n - i - 1][m - j - 1]\n\ttotal += face\n\tface = 0\n\treturn total\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\t(H, W) = (len(A), len(A[0]))\n\tarea = 2 * H * W\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\th = A[i][j]\n\t\t\teast = h if j == 0 else max(0, A[i][j] - A[i][j - 1])\n\t\t\twest = h if j == W - 1 else max(0, A[i][j] - A[i][j + 1])\n\t\t\tnorth = h if i == 0 else max(0, A[i][j] - A[i - 1][j])\n\t\t\tsouth = h if i == H - 1 else max(0, A[i][j] - A[i + 1][j])\n\t\t\tarea += east + west + north + south\n\treturn area\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tres = 2 * H * W\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tif j == 0:\n\t\t\t\tres += A[i][j]\n\t\t\tif j == W - 1:\n\t\t\t\tres += A[i][j]\n\t\t\tif i == 0:\n\t\t\t\tres += A[i][j]\n\t\t\tif i == H - 1:\n\t\t\t\tres += A[i][j]\n\t\t\tif j > 0:\n\t\t\t\tres += abs(A[i][j - 1] - A[i][j])\n\t\t\tif i > 0:\n\t\t\t\tres += abs(A[i - 1][j] - A[i][j])\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n(n, m) = [int(i) for i in input().strip().split()]\narr = [[int(i) for i in input().strip().split()] for i in range(n)]\nans = 0\nfor i in range(n):\n\tfor j in range(m):\n\t\ttemp = 6\n\t\tk = arr[i][j]\n\t\tif k > 1:\n\t\t\ttemp = k * 6 - (k - 2) * 2 - 2\n\t\tif i - 1 >= 0:\n\t\t\tif arr[i - 1][j] > k:\n\t\t\t\ttemp -= k\n\t\t\telse:\n\t\t\t\ttemp -= arr[i - 1][j]\n\t\tif i + 1 < n:\n\t\t\tif arr[i + 1][j] > k:\n\t\t\t\ttemp -= k\n\t\t\telse:\n\t\t\t\ttemp -= arr[i + 1][j]\n\t\tif j - 1 >= 0:\n\t\t\tif arr[i][j - 1] > k:\n\t\t\t\ttemp -= k\n\t\t\telse:\n\t\t\t\ttemp -= arr[i][j - 1]\n\t\tif j + 1 < m:\n\t\t\tif arr[i][j + 1] > k:\n\t\t\t\ttemp -= k\n\t\t\telse:\n\t\t\t\ttemp -= arr[i][j + 1]\n\t\tans += temp\nprint(ans)\n", "(m, n) = list(map(int, input().split()))\na = []\nfor i in range(m):\n\ta.append(list(map(int, input().split())))\nans = 2 * m * n\nfor i in range(m):\n\tans += a[i][0] + a[i][n - 1]\nfor i in range(n):\n\tans += a[0][i] + a[m - 1][i]\ntempans = ans\nans = 0\nfor i in range(m):\n\tfor j in range(n):\n\t\tif i > 0:\n\t\t\tans += abs(a[i][j] - a[i - 1][j])\n\t\tif i < m - 1:\n\t\t\tans += abs(a[i][j] - a[i + 1][j])\n\t\tif j > 0:\n\t\t\tans += abs(a[i][j] - a[i][j - 1])\n\t\tif j < n - 1:\n\t\t\tans += abs(a[i][j] - a[i][j + 1])\nprint(tempans + ans // 2)\n", "(m, n) = [int(x) for x in input().strip().split(' ')]\nmatrix = []\nfor _ in range(m):\n\tmatrix.append([int(x) for x in input().strip().split(' ')])\npermutations = [(-1, 0), (0, -1), (1, 0), (0, 1)]\nsurfaces = 0\nfor i in range(m):\n\tfor j in range(n):\n\t\tcurr = matrix[i][j]\n\t\tif curr > 0:\n\t\t\tsurfaces += curr * 4 + 2\n\t\telse:\n\t\t\tsurfaces += 0\n\t\t\tcontinue\n\t\tfor k in permutations:\n\t\t\ta = i + k[0]\n\t\t\tb = j + k[1]\n\t\t\tif not (a >= 0 and a < m and (b >= 0) and (b < n)):\n\t\t\t\tcontinue\n\t\t\tval = matrix[i + k[0]][j + k[1]]\n\t\t\tif curr > val:\n\t\t\t\tsurfaces -= val\n\t\t\telse:\n\t\t\t\tsurfaces -= curr\nprint(surfaces)\n", "import sys\n\ndef surfaceArea(A):\n\tAA = []\n\tadjacency = [(i, j) for i in (-1, 0, 1) for j in (-1, 0, 1) if not i == j == 0]\n\ttotal = 0\n\tfor i in range(0, len(A)):\n\t\tAA.append([])\n\t\tfor j in range(0, len(A[i])):\n\t\t\taij = A[i][j]\n\t\t\taijss = aij * 4 + 2\n\t\t\tfor adj in adjacency:\n\t\t\t\t(adjx, adjy) = adj\n\t\t\t\tif abs(adjx) == abs(adjy) or i + adjx < 0 or j + adjy < 0 or (i + adjx >= len(A)) or (j + adjy >= len(A[i])):\n\t\t\t\t\tcontinue\n\t\t\t\tadjaij = A[i + adjx][j + adjy]\n\t\t\t\taijss -= min(aij, adjaij)\n\t\t\tAA[i].append(aijss)\n\t\ttotal += sum(AA[i])\n\treturn total\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "(h, w) = [int(x) for x in input().split()]\ngrid = []\nfor _ in range(h):\n\tgrid.append([int(x) for x in input().split()] + [0])\ngrid.append([0] * (w + 1))\narea = w * h * 2\nfor i in range(h + 1):\n\tfor j in range(w + 1):\n\t\tarea += abs(grid[i][j] - grid[i - 1][j])\n\t\tarea += abs(grid[i][j] - grid[i][j - 1])\nprint(area)\n", "import sys\n\ndef getExposedArea(arr):\n\tvalue = 0\n\tfor i in range(len(arr) - 1):\n\t\tvalue = value + abs(arr[i] - arr[i + 1])\n\treturn value\n\ndef surfaceArea(H, W, A):\n\tareaTopBottom = 2 * H * W\n\ttempSum = 0\n\tfor row in A:\n\t\ttempSum = tempSum + row[0] + row[-1]\n\tareaSides = sum(A[0]) + sum(A[-1]) + tempSum\n\thorizontalSum = 0\n\tverticalSum = 0\n\tfor row in A:\n\t\thorizontalSum = horizontalSum + getExposedArea(row)\n\tmyList = []\n\tfor j in range(len(A[0])):\n\t\ttempList = []\n\t\tfor row in A:\n\t\t\ttempList.append(row[j])\n\t\tmyList.append(tempList)\n\tfor vRow in myList:\n\t\tverticalSum = verticalSum + getExposedArea(vRow)\n\tsumHV = horizontalSum + verticalSum\n\treturn sumHV + areaSides + areaTopBottom\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(H, W, A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, W, H):\n\tsides = 0\n\tfor row in range(H):\n\t\tfor col in range(W):\n\t\t\tif row == 0:\n\t\t\t\tsides += A[row][col]\n\t\t\tif col == 0:\n\t\t\t\tsides += A[row][col]\n\t\t\tif row == H - 1:\n\t\t\t\tsides += A[row][col]\n\t\t\tif col == W - 1:\n\t\t\t\tsides += A[row][col]\n\t\t\tif row > 0:\n\t\t\t\tsides += abs(A[row][col] - A[row - 1][col])\n\t\t\tif col > 0:\n\t\t\t\tsides += abs(A[row][col] - A[row][col - 1])\n\t\t\tif A[row][col] != 0:\n\t\t\t\tsides += 2\n\treturn sides\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nprint(surfaceArea(A, W, H))\n", "import sys\n\ndef surfaceArea(H, W, A):\n\ts = 0\n\tfor i in range(H):\n\t\tr = 0\n\t\trr = []\n\t\tfor j in range(W):\n\t\t\tr += A[i][j] if i == 0 else abs(A[i - 1][j] - A[i][j])\n\t\t\tr += A[i][j] if i == H - 1 else 0\n\t\t\tr += A[i][j] if j == 0 else abs(A[i][j] - A[i][j - 1])\n\t\t\tr += A[i][j] if j == W - 1 else 0\n\t\t\tr += 2 if A[i][j] > 0 else 0\n\t\ts += r\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(H, W, A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tprice = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tval = 4 * A[i][j] + 2\n\t\t\tif i < H - 1:\n\t\t\t\tval -= min(A[i][j], A[i + 1][j])\n\t\t\tif i > 0:\n\t\t\t\tval -= min(A[i][j], A[i - 1][j])\n\t\t\tif j < W - 1:\n\t\t\t\tval -= min(A[i][j], A[i][j + 1])\n\t\t\tif j > 0:\n\t\t\t\tval -= min(A[i][j], A[i][j - 1])\n\t\t\tprice += val\n\treturn price\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor _ in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "(h, w) = map(int, input().split())\ndata = []\ndata.append([0 for i in range(w + 2)])\nfor i in range(h):\n\tlst = list(map(int, input().split()))\n\tlst.insert(0, 0)\n\tlst.append(0)\n\tdata.append(lst)\nans = h * w * 2\ndata.append([0 for i in range(w + 2)])\nfor i in range(1, h + 1):\n\tfor j in range(1, w + 1):\n\t\tif data[i][j] - data[i][j - 1] > 0:\n\t\t\tans += data[i][j] - data[i][j - 1]\n\t\tif data[i][j] - data[i - 1][j] > 0:\n\t\t\tans += data[i][j] - data[i - 1][j]\n\t\tif data[i][j] - data[i][j + 1] > 0:\n\t\t\tans += data[i][j] - data[i][j + 1]\n\t\tif data[i][j] - data[i + 1][j] > 0:\n\t\t\tans += data[i][j] - data[i + 1][j]\nprint(ans)\n", "import sys\n\ndef surfaceArea(A):\n\tm = len(A)\n\tn = len(A[0])\n\tans = 0\n\tfor i in range(m):\n\t\tfor j in range(n):\n\t\t\tif j > 0 and A[i][j] > A[i][j - 1]:\n\t\t\t\tans += A[i][j] - A[i][j - 1]\n\t\t\tif j == 0:\n\t\t\t\tans += A[i][j]\n\t\t\tif j == n - 1:\n\t\t\t\tans += A[i][j]\n\t\t\tif j < n - 1 and A[i][j] > A[i][j + 1]:\n\t\t\t\tans += A[i][j] - A[i][j + 1]\n\t\t\tif i == 0:\n\t\t\t\tans += A[i][j]\n\t\t\tif i > 0 and A[i][j] > A[i - 1][j]:\n\t\t\t\tans += A[i][j] - A[i - 1][j]\n\t\t\tif i == m - 1:\n\t\t\t\tans += A[i][j]\n\t\t\tif i < m - 1 and A[i][j] > A[i + 1][j]:\n\t\t\t\tans += A[i][j] - A[i + 1][j]\n\t\t\tans += 2\n\treturn ans\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef check_side(A, i, j, i2, j2):\n\tif A[i][j] > A[i2][j2]:\n\t\treturn A[i][j] - A[i2][j2]\n\treturn 0\n\ndef surfaceArea(H, W, A):\n\tresult = 2 * H * W\n\tfor i in range(1, H + 1):\n\t\tfor j in range(1, W + 1):\n\t\t\tresult += check_side(A, i, j, i + 1, j)\n\t\t\tresult += check_side(A, i, j, i - 1, j)\n\t\t\tresult += check_side(A, i, j, i, j + 1)\n\t\t\tresult += check_side(A, i, j, i, j - 1)\n\treturn result\n(H, W) = [int(i) for i in input().strip().split(' ')]\nA = [[0 for _ in range(W + 2)]]\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append([0] + A_t + [0])\nA.append([0 for _ in range(W + 2)])\nresult = surfaceArea(H, W, A)\nprint(result)\n", "import sys\n\ndef initialize(A):\n\tl = []\n\tfor i in range(len(A)):\n\t\tA[i].append(0)\n\t\tl = [0]\n\t\tA[i] = l + A[i]\n\tl = []\n\tfor i in range(len(A[0])):\n\t\tl.append(0)\n\ttemp = []\n\ttemp.append(l)\n\tfor row in A:\n\t\ttemp.append(row)\n\tA = temp\n\tA.append(l)\n\treturn A\n\ndef checkAround(up, down, left, right, center, level):\n\ts = 0\n\tif center > level:\n\t\tcenter = level\n\tif center > up:\n\t\ts += 1\n\tif center > left:\n\t\ts += 1\n\tif center > down:\n\t\ts += 1\n\tif center > right:\n\t\ts += 1\n\treturn s\n\ndef surfaceArea(A):\n\th = len(A)\n\tw = len(A[0])\n\ttop = h * w\n\tbase = h * w\n\thighest_block = 1\n\tsurface = 0\n\tfor row in A:\n\t\ttemp = max(row)\n\t\tif temp > highest_block:\n\t\t\thighest_block = temp\n\tA = initialize(A)\n\tfor level in range(highest_block):\n\t\tfor i in range(1, h + 1):\n\t\t\tfor j in range(1, w + 1):\n\t\t\t\tif A[i][j] > level:\n\t\t\t\t\tcurrent = checkAround(A[i - 1][j], A[i + 1][j], A[i][j - 1], A[i][j + 1], A[i][j], level + 1)\n\t\t\t\t\tsurface += current\n\tsurface = surface + top + base\n\treturn surface\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\ttotal = 0\n\tfor i in range(len(A)):\n\t\tfor j in range(len(A[i])):\n\t\t\tif i == 0:\n\t\t\t\ttotal += A[i][j]\n\t\t\telse:\n\t\t\t\ttotal += max(0, A[i][j] - A[i - 1][j])\n\t\t\tif i + 1 == len(A):\n\t\t\t\ttotal += A[i][j]\n\t\t\telse:\n\t\t\t\ttotal += max(0, A[i][j] - A[i + 1][j])\n\t\t\tif j == 0:\n\t\t\t\ttotal += A[i][j]\n\t\t\telse:\n\t\t\t\ttotal += max(0, A[i][j] - A[i][j - 1])\n\t\t\tif j + 1 == len(A[i]):\n\t\t\t\ttotal += A[i][j]\n\t\t\telse:\n\t\t\t\ttotal += max(0, A[i][j] - A[i][j + 1])\n\treturn total\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A) + 2 * H * W\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tsTop = H * W\n\tsBottom = H * W\n\tsHLeft = sum(A[0])\n\tsHRight = sum(A[H - 1])\n\tsWInside = sum([A[i][0] for i in range(H)])\n\tsWOutside = sum([A[i][W - 1] for i in range(H)])\n\tsumShips = 0\n\tsumShips += sum([abs(A[i][j] - A[i][j + 1]) for i in range(H) for j in range(W - 1)])\n\tsumShips += sum([abs(A[i][j] - A[i + 1][j]) for i in range(H - 1) for j in range(W)])\n\tprice = sTop + sBottom + sHLeft + sHRight + sWInside + sWOutside + sumShips\n\treturn price\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef exposedArea(array):\n\tval = 0\n\tfor i in range(len(array) - 1):\n\t\tval = val + abs(array[i] - array[i + 1])\n\treturn val\n\ndef surfaceArea(H, W, A):\n\tareaofTopBottom = 2 * H * W\n\tsidesum = 0\n\tfor row in A:\n\t\tsidesum = sidesum + row[0] + row[-1]\n\t\tareasides = sum(A[0]) + sum(A[-1]) + sidesum\n\thorsum = 0\n\tversum = 0\n\tfor row in A:\n\t\thorsum = horsum + exposedArea(row)\n\tmylist = []\n\tfor j in range(len(A[0])):\n\t\tnormallist = []\n\t\tfor row in A:\n\t\t\tnormallist.append(row[j])\n\t\tmylist.append(normallist)\n\tfor verrow in mylist:\n\t\tversum = versum + exposedArea(verrow)\n\tsumHV = horsum + versum\n\treturn sumHV + areasides + areaofTopBottom\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(H, W, A)\nprint(result)\n", "import sys\nNEIGHBS = [(0, 1), (0, -1), (1, 0), (-1, 0)]\n\ndef getHeight(A, x, y, H, W):\n\tif x < 0 or y < 0 or x >= W or (y >= H):\n\t\treturn 0\n\telse:\n\t\treturn A[y][x]\n\ndef surfaceArea(A, H, W):\n\tres = 0\n\tfor x in range(W):\n\t\tfor y in range(H):\n\t\t\tmH = A[y][x]\n\t\t\tif mH != 0:\n\t\t\t\tres += 2\n\t\t\t\tfor n in NEIGHBS:\n\t\t\t\t\tnH = getHeight(A, x + n[0], y + n[1], H, W)\n\t\t\t\t\tres += max(mH - nH, 0)\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef findArea(B):\n\tprev = B[0]\n\ts = prev\n\tfor i in range(1, len(B)):\n\t\tif B[i] > prev:\n\t\t\ts += B[i] - prev\n\t\tprev = B[i]\n\treturn s\n\ndef surfaceArea(A, H, W):\n\ts = 0\n\tfor i in range(H):\n\t\ts += findArea(A[i])\n\tfor i in range(W):\n\t\ts += findArea([row[i] for row in A])\n\treturn s * 2 + H * W * 2\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef main():\n\tbuffer = map(int, sys.stdin.read().split())\n\tH = next(buffer)\n\tW = next(buffer)\n\tboard = [[0] * (W + 2) for _ in range(H + 2)]\n\tfor i in range(1, H + 1):\n\t\tfor j in range(1, W + 1):\n\t\t\tboard[i][j] = next(buffer)\n\ttotal = 2 * H * W\n\tfor i in range(1, H + 1):\n\t\tfor j in range(1, W + 1):\n\t\t\ttotal += max(0, board[i][j] - board[i + 1][j])\n\t\t\ttotal += max(0, board[i][j] - board[i - 1][j])\n\t\t\ttotal += max(0, board[i][j] - board[i][j + 1])\n\t\t\ttotal += max(0, board[i][j] - board[i][j - 1])\n\tprint(total)\nmain()\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tans = 2 * H * W\n\tfor i in range(1, H + 2):\n\t\tfor j in range(1, W + 2):\n\t\t\tans += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])\n\treturn ans\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = [[0 for _ in range(W + 2)]]\nfor A_i in range(H):\n\tA_t = [0] + [int(A_temp) for A_temp in input().strip().split(' ')] + [0]\n\tA.append(A_t)\nA.append([0 for _ in range(W + 2)])\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\theight = len(A)\n\twidth = len(A[0])\n\tarea = height * width * 2\n\tfor i in range(height):\n\t\tfor j in range(width):\n\t\t\tfor d in ((-1, 0), (+1, 0), (0, -1), (0, +1)):\n\t\t\t\tii = i + d[0]\n\t\t\t\tjj = j + d[1]\n\t\t\t\tarea += max(0, A[i][j] - (A[ii][jj] if 0 <= ii < height and 0 <= jj < width else 0))\n\treturn area\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\ta = 0\n\th = len(A)\n\tw = len(A[0])\n\tfor i in range(h):\n\t\ta += A[i][0]\n\t\ta += A[i][w - 1]\n\t\tfor j in range(w - 1):\n\t\t\tx = A[i][j + 1] - A[i][j]\n\t\t\ty = A[i][w - 2 - j] - A[i][w - 1 - j]\n\t\t\tif x > 0:\n\t\t\t\ta += x\n\t\t\tif y > 0:\n\t\t\t\ta += y\n\tfor i in range(w):\n\t\ta += A[0][i]\n\t\ta += A[h - 1][i]\n\t\tfor j in range(h - 1):\n\t\t\tx = A[j + 1][i] - A[j][i]\n\t\t\ty = A[h - 2 - j][i] - A[h - 1 - j][i]\n\t\t\tif x > 0:\n\t\t\t\ta += x\n\t\t\tif y > 0:\n\t\t\t\ta += y\n\tb = [item for sublist in A for item in sublist]\n\treturn a + (h * w - b.count(0)) * 2\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef to_arr(i, j):\n\treturn i * W + j\n\ndef neibs(i, j):\n\tif i > 0:\n\t\tyield A[to_arr(i - 1, j)]\n\tif i < H - 1:\n\t\tyield A[to_arr(i + 1, j)]\n\tif j > 0:\n\t\tyield A[to_arr(i, j - 1)]\n\tif j < W - 1:\n\t\tyield A[to_arr(i, j + 1)]\n\ndef surfaceArea():\n\tres = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tval = A[to_arr(i, j)]\n\t\t\tres += 2 + val * 4 - sum((min(n, val) for n in neibs(i, j)))\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.extend(A_t)\nresult = surfaceArea()\nprint(result)\n", "import sys\n\ndef total_non_zero(area):\n\tres = 0\n\t(nn, mm) = (len(area), len(area[0]))\n\tfor i in range(nn):\n\t\tfor j in range(mm):\n\t\t\tif area[i][j] > 0:\n\t\t\t\tres += 1\n\treturn res\n\ndef surfaceArea(cubes, nn, mm):\n\tres = 0\n\tcubes_add = [[0] * (mm + 2)]\n\tfor i in range(nn):\n\t\tcubes_i = cubes[i]\n\t\tcubes_i.append(0)\n\t\tcubes_i.insert(0, 0)\n\t\tcubes_add.append(cubes_i)\n\tcubes_add.append([0] * (mm + 2))\n\tbottom = total_non_zero(cubes)\n\tres = bottom * 2\n\tfor i in range(1, nn + 1):\n\t\tfor j in range(mm + 1):\n\t\t\tcurr = cubes_add[i][j]\n\t\t\tfor neighbor in [cubes_add[i - 1][j], cubes_add[i][j - 1], cubes_add[i + 1][j], cubes_add[i][j + 1]]:\n\t\t\t\tif curr > neighbor:\n\t\t\t\t\tres += curr - neighbor\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "(n, m) = map(int, input().split())\nl = []\ns = 0\nfor i in range(n):\n\tl.append(list(map(int, input().split())))\n\tfor j in range(1, m):\n\t\ts = s + abs(l[i][j] - l[i][j - 1])\n\ts = s + l[i][0] + l[i][m - 1]\nfor i in range(1, n):\n\tfor j in range(m):\n\t\ts = s + abs(l[i][j] - l[i - 1][j])\nprint(s + sum(l[0]) + sum(l[n - 1]) + 2 * n * m)\n", "import sys\n\ndef calc1(A, s):\n\tluas = 0\n\tfor i in range(s):\n\t\ta = [row[i] for row in A]\n\t\tb0 = 0\n\t\tfor b in a:\n\t\t\tif b != b0:\n\t\t\t\tluas = luas + abs(b - b0)\n\t\t\tluas = luas + 1\n\t\t\tb0 = b\n\t\tluas = luas + b\n\treturn luas\n\ndef calc2(A, s):\n\tluas = 0\n\tfor i in range(s):\n\t\tb0 = 0\n\t\tfor b in A[i]:\n\t\t\tif b != b0:\n\t\t\t\tluas = luas + abs(b - b0)\n\t\t\tb0 = b\n\t\tluas = luas + b\n\treturn luas\n\ndef surfaceArea(A, H, W):\n\ts = len(A)\n\treturn calc1(A, W) + calc2(A, H) + W * H\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef surfaceArea(a):\n\tn = len(a)\n\tm = len(a[0])\n\tsumi = 0\n\tfor i in range(n):\n\t\tfor j in range(m):\n\t\t\tif a[i][j] != 0:\n\t\t\t\tsumi += 2\n\tfor i in range(n):\n\t\tfor j in range(m):\n\t\t\tif j > 0:\n\t\t\t\tsumi += abs(a[i][j] - a[i][j - 1])\n\t\t\telse:\n\t\t\t\tsumi += a[i][j]\n\t\t\tif i > 0:\n\t\t\t\tsumi += abs(a[i][j] - a[i - 1][j])\n\t\t\telse:\n\t\t\t\tsumi += a[i][j]\n\tfor i in range(n):\n\t\tsumi += a[i][m - 1]\n\tfor j in range(m):\n\t\tsumi += a[n - 1][j]\n\treturn sumi\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(a):\n\tres = sum(a[0]) + sum(a[-1])\n\tii = 0\n\tfor i in a:\n\t\tres += len(i) * 2\n\t\tres += i[0] + i[-1]\n\t\tfor x in range(len(i)):\n\t\t\tif ii != len(a) - 1:\n\t\t\t\tres += abs(a[ii][x] - a[ii + 1][x])\n\t\t\tif x == len(i) - 1:\n\t\t\t\tbreak\n\t\t\tres += abs(i[x] - i[x + 1])\n\t\tii += 1\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A, H, W):\n\tsum = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tsum += 2\n\t\t\tif i == 0 and j == 0:\n\t\t\t\tsum += A[i][j] * 2\n\t\t\telif i == 0 and j == W - 1:\n\t\t\t\tsum += A[i][j] + abs(A[i][j] - A[i][j - 1])\n\t\t\telif i == H - 1 and j == 0:\n\t\t\t\tsum += A[i][j] + abs(A[i][j] - A[i - 1][j])\n\t\t\telif i == H - 1 and j == W - 1:\n\t\t\t\tsum += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])\n\t\t\telif i == 0:\n\t\t\t\tsum += abs(A[i][j] - A[i][j - 1]) + A[i][j]\n\t\t\telif j == 0:\n\t\t\t\tsum += A[i][j] + abs(A[i][j] - A[i - 1][j])\n\t\t\telif i == H - 1 or j == W - 1:\n\t\t\t\tsum += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])\n\t\t\telse:\n\t\t\t\tsum += abs(A[i][j] - A[i][j - 1]) + abs(A[i][j] - A[i - 1][j])\n\tfor i in range(H):\n\t\tsum = sum + A[i][W - 1]\n\tfor j in range(W):\n\t\tsum = sum + A[H - 1][j]\n\treturn sum\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A, H, W)\nprint(result)\n", "import sys\n\ndef rowSurface(row):\n\tsurface = row[0]\n\tfor i in range(1, len(row)):\n\t\tsurface += row[i] - row[i - 1] if row[i] - row[i - 1] > 0 else 0\n\treturn surface\n\ndef surfaceArea(A):\n\tnumRows = len(A)\n\tnumColumns = len(A[0])\n\ttransA = [list(t) for t in zip(*A)]\n\tfront = 0\n\tback = 0\n\tfor row in A:\n\t\tfront += rowSurface(row)\n\t\trow.reverse()\n\t\tback += rowSurface(row)\n\tleft = 0\n\tright = 0\n\tfor row in transA:\n\t\tleft += rowSurface(row)\n\t\trow.reverse()\n\t\tright += rowSurface(row)\n\treturn numRows * numColumns * 2 + front + back + left + right\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef neighbours(A, i, j):\n\tyield (A[i - 1][j] if i > 0 else 0)\n\tyield (A[i][j - 1] if j > 0 else 0)\n\tyield (A[i + 1][j] if i < H - 1 else 0)\n\tyield (A[i][j + 1] if j < W - 1 else 0)\n\ndef area(A, i, j):\n\tsurface = 0\n\tfor n in neighbours(A, i, j):\n\t\tsurface += max(0, A[i][j] - n)\n\treturn surface\n\ndef surfaceArea(A):\n\tres = 0\n\tfor i in range(H):\n\t\tfor j in range(W):\n\t\t\tres += area(A, i, j) + 2\n\treturn res\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "(m, n) = [int(x) for x in input().strip().split(' ')]\nmatrix = []\nfor _ in range(m):\n\tmatrix.append([int(x) for x in input().strip().split(' ')])\npermutations = [(-1, 0), (0, -1), (1, 0), (0, 1)]\nsurfaces = 0\nfor i in range(m):\n\tfor j in range(n):\n\t\tcurr = matrix[i][j]\n\t\tif curr > 0:\n\t\t\tsurfaces += curr * 4 + 2\n\t\telse:\n\t\t\tsurfaces += 0\n\t\t\tcontinue\n\t\tfor k in permutations:\n\t\t\ta = i + k[0]\n\t\t\tb = j + k[1]\n\t\t\tif not (a >= 0 and a < m and (b >= 0) and (b < n)):\n\t\t\t\tcontinue\n\t\t\tval = matrix[i + k[0]][j + k[1]]\n\t\t\tif curr > val:\n\t\t\t\tsurfaces -= val\n\t\t\telse:\n\t\t\t\tsurfaces -= curr\nprint(surfaces)\n", "def sub(i, j):\n\tc1 = min(b[i - 1][j], b[i][j])\n\tc2 = min(b[i + 1][j], b[i][j])\n\tc3 = min(b[i][j - 1], b[i][j])\n\tc4 = min(b[i][j + 1], b[i][j])\n\treturn c1 + c2 + c3 + c4\n(h, w) = map(int, input().split(' '))\narr = []\nfor i in range(h):\n\tarr.append(list(map(int, input().split(' '))))\na = [[0 for i in range(w + 2)] for i in range(h + 2)]\nb = [[0 for i in range(w + 2)] for i in range(h + 2)]\nfor i in range(h):\n\tfor j in range(w):\n\t\tb[i + 1][j + 1] = arr[i][j]\nfor i in range(1, h + 1):\n\tfor j in range(1, w + 1):\n\t\ta[i][j] = b[i][j] * 6 - (b[i][j] - 1) * 2 - sub(i, j)\nsum = 0\nfor i in range(1, h + 1):\n\tfor j in range(1, w + 1):\n\t\tsum += a[i][j]\nprint(sum)\n", "import sys\n\ndef surfaceArea(A):\n\tsum1 = 0\n\th = len(A)\n\tw = len(A[0])\n\tsum1 = sum(A[0]) + sum(A[h - 1])\n\tsum1 += 2 * len(A) * len(A[0])\n\tarr2 = list(map(list, zip(*A)))\n\tfor i in range(w):\n\t\tsum1 += sum([abs(arr2[i][j - 1] - arr2[i][j]) for j in range(1, h)])\n\tfor i in range(h):\n\t\tsum1 += A[i][0] + A[i][-1]\n\t\tsum1 += sum([abs(A[i][j - 1] - A[i][j]) for j in range(1, w)])\n\treturn sum1\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(a):\n\ts = 2 * len(a) * len(a[0])\n\tfor i in a[0]:\n\t\ts += i\n\tfor i in a[len(a) - 1]:\n\t\ts += i\n\tfor i in range(len(a)):\n\t\tfor j in range(len(a[0])):\n\t\t\tif j == 0:\n\t\t\t\ts += a[i][j]\n\t\t\tif j == len(a[0]) - 1:\n\t\t\t\ts += a[i][j]\n\t\t\tif j != len(a[0]) - 1:\n\t\t\t\ts += abs(a[i][j] - a[i][j + 1])\n\tfor i in range(len(a) - 1):\n\t\tfor j in range(len(a[0])):\n\t\t\ts += abs(a[i][j] - a[i + 1][j])\n\treturn s\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nu = 2 * H * W\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nfor i in range(H):\n\ts = A[i][0] + A[i][W - 1]\n\tif W > 1:\n\t\tfor j in range(1, W):\n\t\t\ts += abs(A[i][j] - A[i][j - 1])\n\tu += s\nfor i in range(W):\n\ts = A[0][i] + A[H - 1][i]\n\tif H > 1:\n\t\tfor j in range(1, H):\n\t\t\ts += abs(A[j][i] - A[j - 1][i])\n\tu += s\nprint(u)\n", "result = 0\ntable = []\n(h, w) = [int(i) for i in input().split()]\nfor _ in range(h):\n\ttable.append([int(i) for i in input().split()])\nfor i in range(100):\n\tfor row in table:\n\t\tflag = False\n\t\tfor cell in row:\n\t\t\tif not flag:\n\t\t\t\tif cell > i:\n\t\t\t\t\tresult += 1\n\t\t\t\t\tflag = True\n\t\t\telif cell <= i:\n\t\t\t\tflag = False\n\tfor width in range(w):\n\t\tflag = False\n\t\tfor row in range(h):\n\t\t\tif not flag:\n\t\t\t\tif table[row][width] > i:\n\t\t\t\t\tresult += 1\n\t\t\t\t\tflag = True\n\t\t\telif table[row][width] <= i:\n\t\t\t\tflag = False\nresult *= 2\nprint(result + w * h * 2)\n", "(h, w) = list(map(int, input().split()))\narr = [0 for i in range(h)]\nfor i in range(h):\n\tarr[i] = list(map(int, input().split()))\nsurface_area = 0\nfor i in range(h):\n\tfor j in range(w):\n\t\tsurface_area += 4 * arr[i][j] + 2\n\t\tif j != w - 1:\n\t\t\tsurface_area -= min(arr[i][j], arr[i][j + 1])\n\t\tif i != h - 1:\n\t\t\tsurface_area -= min(arr[i][j], arr[i + 1][j])\n\t\tif i != 0:\n\t\t\tsurface_area -= min(arr[i][j], arr[i - 1][j])\n\t\tif j != 0:\n\t\t\tsurface_area -= min(arr[i][j], arr[i][j - 1])\nprint(surface_area)\n", "import sys\n\ndef surfaceArea(A):\n\tarea = 0\n\trows = len(A)\n\tcols = len(A[0])\n\tfor i in range(rows):\n\t\tfor j in range(cols):\n\t\t\tmat_val = A[i][j]\n\t\t\tfor k in range(1, mat_val + 1):\n\t\t\t\tif j - 1 < 0:\n\t\t\t\t\tarea += 1\n\t\t\t\telif A[i][j - 1] < k:\n\t\t\t\t\tarea += 1\n\t\t\t\tif j + 1 >= cols:\n\t\t\t\t\tarea += 1\n\t\t\t\telif A[i][j + 1] < k:\n\t\t\t\t\tarea += 1\n\t\t\t\tif i + 1 >= rows:\n\t\t\t\t\tarea += 1\n\t\t\t\telif A[i + 1][j] < k:\n\t\t\t\t\tarea += 1\n\t\t\t\tif i - 1 < 0:\n\t\t\t\t\tarea += 1\n\t\t\t\telif A[i - 1][j] < k:\n\t\t\t\t\tarea += 1\n\t\t\t\tif k == 1:\n\t\t\t\t\tarea += 1\n\t\t\t\tif k == mat_val:\n\t\t\t\t\tarea += 1\n\treturn area\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\t(h, w) = (len(A), len(A[0]))\n\tsurf = 2 * h * w\n\tfor line in A:\n\t\tfor j in range(w):\n\t\t\tif j == 0:\n\t\t\t\tsurf += line[j]\n\t\t\tif j == len(line) - 1:\n\t\t\t\tsurf += line[j]\n\t\t\telse:\n\t\t\t\tsurf += abs(line[j + 1] - line[j])\n\tfor j in range(w):\n\t\tfor i in range(h):\n\t\t\tif i == 0:\n\t\t\t\tsurf += A[i][j]\n\t\t\tif i == h - 1:\n\t\t\t\tsurf += A[i][j]\n\t\t\telse:\n\t\t\t\tsurf += abs(A[i + 1][j] - A[i][j])\n\treturn surf\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tassert len(A_t) == W\n\tA.append(A_t)\nassert len(A) == H\nresult = surfaceArea(A)\nprint(result)\n", "import sys\n\ndef surfaceArea(A):\n\tresult = 2 * len(A) * len(A[0])\n\tfor i in range(len(A)):\n\t\tfor j in range(len(A[0])):\n\t\t\tif i == 0:\n\t\t\t\tresult = result + A[i][j]\n\t\t\telif A[i][j] > A[i - 1][j]:\n\t\t\t\tresult = result + A[i][j] - A[i - 1][j]\n\t\t\tif i == len(A) - 1:\n\t\t\t\tresult = result + A[i][j]\n\t\t\telif A[i][j] > A[i + 1][j]:\n\t\t\t\tresult = result + A[i][j] - A[i + 1][j]\n\t\t\tif j == 0:\n\t\t\t\tresult = result + A[i][j]\n\t\t\telif A[i][j] > A[i][j - 1]:\n\t\t\t\tresult = result + A[i][j] - A[i][j - 1]\n\t\t\tif j == len(A[0]) - 1:\n\t\t\t\tresult = result + A[i][j]\n\t\t\telif A[i][j] > A[i][j + 1]:\n\t\t\t\tresult = result + A[i][j] - A[i][j + 1]\n\treturn result\n(H, W) = input().strip().split(' ')\n(H, W) = [int(H), int(W)]\nA = []\nfor A_i in range(H):\n\tA_t = [int(A_temp) for A_temp in input().strip().split(' ')]\n\tA.append(A_t)\nresult = surfaceArea(A)\nprint(result)\n"]

difficulty: MEDIUM

source: hacker rank

question:
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)$.
![image](2.png)
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

![image](2.png)
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.

image_tags:
3D Geometry
3D Geometry

data_split: test