In this problem, a $n \times m$ rectangular matrix $a$ is called increasing if, for each row of $i$, when go from left to right, the values strictly increase (that is, $a_{i,1}<a_{i,2}<\dots<a_{i,m}$) and for each column $j$, when go from top to bottom, the values strictly increase (that is, $a_{1,j}<a_{2,j}<\dots<a_{n,j}$).

In a given matrix of non-negative integers, it is necessary to replace each value of $0$ with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.

It is guaranteed that in a given value matrix all values of $0$ are contained only in internal cells (that is, not in the first or last row and not in the first or last column).


-----Input-----

The first line contains integers $n$ and $m$ ($3 \le n, m \le 500$) — the number of rows and columns in the given matrix $a$.

The following lines contain $m$ each of non-negative integers — the values in the corresponding row of the given matrix: $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($0 \le a_{i,j} \le 8000$).

It is guaranteed that for all $a_{i,j}=0$, $1 < i < n$ and $1 < j < m$ are true.


-----Output-----

If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.


-----Examples-----
Input
4 5
1 3 5 6 7
3 0 7 0 9
5 0 0 0 10
8 9 10 11 12

Output
144

Input
3 3
1 2 3
2 0 4
4 5 6

Output
30

Input
3 3
1 2 3
3 0 4
4 5 6

Output
-1

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

Output
-1



-----Note-----

In the first example, the resulting matrix is as follows: 

1 3 5 6 7

3 6 7 8 9

5 7 8 9 10

8 9 10 11 12



In the second example, the value $3$ must be put in the middle cell.

In the third example, the desired resultant matrix does not exist.

Difficulty: interview

Starter code:

Solutions:
n, m = map(int, input().split())
matr = [list(map(int, input().split())) for _ in range(n)]

for i, st in enumerate(matr):
    for j, elem in enumerate(st):
        if matr[~i][~j] == 0:
            matr[~i][~j] = min(matr[~i + 1][~j], matr[~i][~j + 1]) - 1

# print(*matr, sep='\n')

for i, st in enumerate(matr):
    for j, elem in enumerate(st):
        if i != 0 and j != 0:
            if matr[i][j] <= max(matr[i - 1][j], matr[i][j - 1]):
                print('-1')
                return
        elif i == 0 and j != 0:
            if matr[i][j] <= matr[i][j - 1]:
                print('-1')
                return
        elif i != 0 and j == 0:
            if matr[i][j] <= matr[i - 1][j]:
                print('-1')
                return
print(sum(sum(l) for l in matr))

n, m = list(map(int, input().split()))
k = 0
a = [0] * n
for i in range(n):
    a[i] = list(map(int, input().split()))

b = [0] * n
c = [0] * n
for i in range(n):
    b[i] = [0] * m
    c[i] = [0] * m
    for j in range(m):
        b[i][j] = [0, 0]
        c[i][j] = [0, 0]

for i in range(n):
    for j in range(m - 1):
        if a[i][j] >= a[i][j + 1] and a[i][j + 1] != 0:
            k = 1
for j in range(m):
    for i in range(n - 1):
        if a[i][j] >= a[i + 1][j] and a[i + 1][j] != 0:
            k = 1

for i in range( n - 1, 0, -1):
    for j in range(m - 1, 0, -1):
        if a[i][j] == 0:
            a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1)

for i in range(n):
    for j in range(m - 1):
        if a[i][j] >= a[i][j + 1] and a[i][j + 1] != 0:
            k = 1
for j in range(m):
    for i in range(n - 1):
        if a[i][j] >= a[i + 1][j] and a[i + 1][j] != 0:
            k = 1
            
ans = 0
for i in range(n):
    ans += sum(a[i])
if k == 1:
    print(-1)
else:
    print(ans)
            
            


import sys
n,m=map(int,input().split())
a=[[*map(int,input().split())] for _ in range(n)]
try:
 for i in range(n-1,-1,-1):
  for j in range(m-1,-1,-1):
   if a[i][j]==0:a[i][j]=min(a[i+1][j],a[i][j+1])-1
   if i+1<n:assert a[i][j]<a[i+1][j]
   if j+1<m:assert a[i][j]<a[i][j+1]
 print(sum(map(sum,a)))
except AssertionError:
 print(-1)

import sys
n,m = map(int,input().split())
M = []
for i in range(n):
    M += [list(map(int,input().split()))]
S = M[-1][-1]

for j in range(n-2 , -1 , -1):   
    if M[j][-1] >= M[j+1][-1] :
        print(-1)
        return
    if M[j][-1] == 0:
        M[j][-1] = M[j-1][-1] - 1  
    S += M[j][-1]
    
for j in range(m-2 , -1 , -1):
    if M[-1][j] >= M[-1][j+1] :
        print(-1)
        return
    if M[-1][j] == 0:
        M[-1][j] = M[-1][j-1] - 1   
    S += M[-1][j]
        
for k in range(m-2 , -1 , -1):
    for p in range(n-2 , -1 , -1):
        if M[p][k] >= min(M[p+1][k] , M[p][k+1]):
            print(-1)
            return
        if M[p][k] == 0:
            M[p][k] = min(M[p+1][k] , M[p][k+1]) - 1 
        S += M[p][k]
print(S)

n, m = list(map(int, input().split(' ')))

matrix = []
for i in range(n):
    row = [int(x) for x in input().split(' ')]
    matrix.append(row)


def determine(matrix, n, m):
    sum_max = matrix[n-1][m-1]

    for j in range(m-2, -1, -1):
        if matrix[n-1][j] < matrix[n-1][j+1]:
            if matrix[n-1][j] == 0:
                matrix[n-1][j] = matrix[n-1][j+1]-1
            sum_max += matrix[n-1][j]
        else:
            return -1

    for i in range(n-2, -1, -1):
        if matrix[i][m-1] < matrix[i+1][m-1]:
            if matrix[i][m-1] == 0:
                matrix[i][m-1] = matrix[i+1][m-1]-1
            sum_max += matrix[i][m-1]
        else:
            return -1

    for i in range(n-2, -1, -1):
        for j in range(m-2, -1, -1):
            if matrix[i][j] < min(matrix[i+1][j], matrix[i][j+1]):
                if matrix[i][j] == 0:
                    matrix[i][j] = min(matrix[i+1][j], matrix[i][j+1])-1
                sum_max += matrix[i][j]
            else:
                return -1
    return sum_max


sum_max = determine(matrix, n, m)
print(sum_max)
        


r,c = map(int,input().split())
s = [list(map(int,input().split())) for _ in range(r)]
#print(s)
re = 0
for i in range(r-1,-1,-1):
    for j in range(c-1,-1,-1):
        if 0<i<r-1 and 0<j<c-1:
            if s[i][j]==0:
                s[i][j] = min(s[i+1][j],s[i][j+1])-1
            if (s[i-1][j]>0 and s[i-1][j]>=s[i][j]) or (s[i][j-1]>0 and s[i][j-1]>=s[i][j]):
                print(-1)
                return
        if i<r-1 and j<c-1:
            if s[i][j]>=s[i+1][j] or s[i][j]>=s[i][j+1]:
                print(-1)
                return
        if i<r-1 and j==c-1:
            if s[i][j]>=s[i+1][j]:
                print(-1)
                return
        if i==r-1 and j<c-1:
            if s[i][j]>=s[i][j+1]:
                print(-1)
                return
        re += s[i][j]
print(re)

import sys
def ma():
    n,m = map(int,input().split())
    a = []
    for i in range(n):
        a.append([*map(int,sys.stdin.readline().split())])
    if a[n-1][m-1] <= a[n-2][m-1] or a[n-1][m-1] <= a[n-1][m-2]:
        return print(-1)
    for i in range(n-2,0,-1):
        for j in range(m-2,0,-1):
            if a[i][j] <= a[i+1][j] or a[i][j] <= a[i][j+1]:
                mi = min(a[i+1][j],a[i][j+1])-1
                if a[i][j] == 0:
                    
                    a[i][j] = min(a[i+1][j],a[i][j+1])-1
                if mi <= a[i-1][j] or mi <= a[i][j-1]:
                        return print(-1)
            else:
                return print(-1)
    su = 0
    for i in range(n):
        su += sum(a[i])
    print(su)
    
ma()


def comp(A):
    for i in range(n-2, -1, -1):
        for j in range(m-2, -1, -1):
            if (A[i+1][j+1] <= A[i][j+1] or A[i+1][j+1] <= A[i+1][j]):
                return -1
            if (A[i][j] == 0):
                A[i][j] = min (A[i+1][j]-1, A[i][j+1]-1)
                if (A[i][j] <= A[i-1][j] or A[i][j] <= A[i][j-1] or A[i][j] == 0):
                    return -1
    
    return sum([sum(A[i]) for i in range(n)])
 
n, m = map(int, input().rstrip().split())
 
A = []
for i in range(n):
    A.append(list(map(int, input().rstrip().split())))
res = comp (A)
 
print(res)

def comp(A):
    s = 0
    for i in range(n-2, -1, -1):
        for j in range(m-2, -1, -1):
            if (A[i+1][j+1] <= A[i][j+1] or A[i+1][j+1] <= A[i+1][j]):
                return -1
            if (A[i][j] == 0):
                A[i][j] = min (A[i+1][j]-1, A[i][j+1]-1)
                if (A[i][j] <= A[i-1][j] or A[i][j] <= A[i][j-1] or A[i][j] == 0):
                    return -1
            s+=A[i][j]
    
    return  s + sum(A[n-1]) + sum([A[i][m-1] for i in range(n-1)])
 
n, m = map(int, input().rstrip().split())
 
A = []
for i in range(n):
    A.append(list(map(int, input().rstrip().split())))
res = comp (A)
 
print(res)

n,m=list(map(int,input().split()))
arr=[]
for _ in range(n):
    arr.append(list(map(int,input().split())))
ans=0
for i in range(n-1,-1,-1):
    for j in range(m-1,-1,-1):
        if(arr[i][j]==0):
            if(i<n-1 and j<m-1):
                arr[i][j]=min(arr[i+1][j],arr[i][j+1])-1
            elif(j<m-1):
                arr[i][j]=arr[i][j+1]-1
            else:
                arr[i][j]=arr[i+1][j]-1
        ans+=arr[i][j]
flag=True
for i in range(n):
    for j in range(m):
        if(i<n-1 and j<m-1):
            if(arr[i][j]>=arr[i][j+1] or arr[i][j]>=arr[i+1][j] or arr[i][j]>=arr[i+1][j+1]):
                flag=False
                break
        if(i<n-1):
            if(arr[i][j]>=arr[i+1][j]):
                flag=False
                break
        if(j<m-1):
            if(arr[i][j]>=arr[i][j+1]):
                flag=False
                break
    if(flag):
        pass
    else:
        break
if(flag):
    print(ans)
else:
    print(-1)



def sum_matr(matr):
    for i in (0, n-1):
        for j in range(m-1):
            if matr[i][j] >= matr[i][j+1]:
                return -1
    for j in (0, m-1):
        for i in range(n-1):
            if matr[i][j] >= matr[i+1][j]:
                return -1
    for i in range(n-2,0,-1):
        for j in range(m-2,0,-1):
            if matr[i][j] == 0:
                c = min(matr[i][j+1] - 1, matr[i+1][j] - 1)
                if c <= matr[i][j-1] or c <= matr[i-1][j]:
                    return -1
                else:
                    matr[i][j] = c
            elif matr[i][j] >= matr[i+1][j] or matr[i][j] >= matr[i+1][j]:
                return -1
    s = 0
    for i in range(n):
        for j in range(m):
            s += matr[i][j]
    return s

n, m = [int(elem) for elem in input().split()]
matr = []
for i in range(n):
    matr.append([int(elem) for elem in input().split()])
    
print(sum_matr(matr))


def __starting_point():
    n, m = list(map(int, input().split()))
    matrix = []
    for i in range(n):
        matrix.append(list(map(int, input().split())))
    res = 0
    for i in range(n - 2, 0, -1):
        for j in range(m - 2, 0, -1):
            if matrix[i][j] == 0:
                new = min(matrix[i + 1][j], matrix[i][j + 1]) - 1
                if (matrix[i - 1][j] == 0 or new > matrix[i - 1][j]) and (matrix[i][j - 1] == 0 or new > matrix[i][j - 1]):
                    matrix[i][j] = new
                else:
                    res = -1
                    break
        if res == -1:
            break
    if res != -1:
        for i in range(n):
            for j in range(1, m):
                if matrix[i][j] <= matrix[i][j - 1]:
                    res = -1
        if res == -1:
            print(-1)
        else:
            for i in range(m):
                for j in range(1, n):
                    if matrix[j][i] <= matrix[j-1][i]:
                        res = -1
            if res == -1:
                print(-1)
            else:
                for i in range(n):
                    res += sum(matrix[i])
                print(res)
    else:
        print(-1)

__starting_point()

def comp(A):
    s = 0
    for i in range(n-2, -1, -1):
        for j in range(m-2, -1, -1):
            if (A[i+1][j+1] <= A[i][j+1] or A[i+1][j+1] <= A[i+1][j]):
                return -1
            if (A[i][j] == 0):
                A[i][j] = min (A[i+1][j]-1, A[i][j+1]-1)
                if (A[i][j] <= A[i-1][j] or A[i][j] <= A[i][j-1] or A[i][j] == 0):
                    return -1
            s+=A[i][j]
    
    return  s + sum(A[n-1]) + sum([A[i][m-1] for i in range(n-1)])
 
n, m = map(int, input().rstrip().split())
 
A = []
for i in range(n):
    A.append(list(map(int, input().rstrip().split())))
res = comp (A)
 
print(res)

y = [int(x) for x in input().split(' ')]
n_m = []
for i in range(y[0]):
    p = [int(x) for x in input().split(' ')]
    n_m.append(p)
sum = 0
h = True
for i in range(y[0]-1, -1, -1):
    for j in range(y[1]-1, -1, -1):
        if n_m[i][j] == 0:
            if n_m[i+1][j] > n_m[i][j+1]:
                n_m[i][j] = n_m[i][j+1] - 1 
            else:
                n_m[i][j] = n_m[i+1][j] - 1 
        try:
            if j == 0:
                pass
            elif i == 0:
                pass
            elif n_m[i][j] <= n_m[i][j-1]:
                h = False
            elif n_m[i][j] <= n_m[i-1][j]:
                h = False
        except:
            pass
        if h == False:
            print(-1)
            return
        sum += n_m[i][j]
print(sum)


__author__ = 'Esfandiar'
n,m = list(map(int,input().split()))
a = list()
for i in range(n):
	a.append(list(map(int,input().split())))
su = 0
M = -1
for i in range(n-1,-1,-1):
    for j in range(m-1,-1,-1):
        if a[i][j] == 0:
            M = a[i][j+1] - 1
            M = min(M,a[i+1][j] - 1)
            a[i][j] = M
        su+=a[i][j]

for i in range(n):
	for j in range(m-1):
		if a[i][j] >= a[i][j+1]:
			print(-1)
			return

for i in range(m):
	for j in range(n-1):
		if a[j][i] >= a[j+1][i]:
			print(-1)
			return
print(su)



n, m = map(int, input().split())

matrix = [list(map(int, input().split())) for _ in range(n)]
res = 0

for i in range(n-2, 0, -1):
    for j in range(m-1, 0, -1):
        if matrix[i][j] == 0:
            mn = min(matrix[i + 1][j], matrix[i][j + 1])
            if mn != 1:
                matrix[i][j] = mn - 1
            else:
                res = -1
                break
    if res == -1:
        break
else:
    for i in range(n):
        if [matrix[i][j] for j in range(m-1) if matrix[i][j] >= matrix[i][j+1]]:
            res = -1
            break
    else:
        for j in range(m):
            if [matrix[i][j] for i in range(n-1) if matrix[i][j] >= matrix[i+1][j]]:
                res = -1
                break
        else:
            res = sum((sum(matrix[i]) for i in range(n)))

print(res)

n, m = list(map(int, input().split()))
a = []
for i in range(n):
    a.append(list(map(int, input().split())))


def result(a, n, m):
    r = 0
    for i in range(-1, -m, -1):
        if a[-1][i] == 0:
            a[-1][i] = a[-1][i+1] - 1
        if a[-1][i] <= a[-1][i-1] or a[-1][i] <= a[-2][i]:
            return -1
        r = r + a[-1][i]
    if a[-1][-m] == 0:
        a[-1][-m] = a[-1][-m+1] - 1
    if a[-1][-m] <= a[-2][-m]:
        return -1
    r = r + a[-1][-m]
    for i in range(-2, -n, -1):
        for j in range(-1, -m, -1):
            if j == -1:
                if a[i][j] == 0:
                    a[i][j] == a[i+1][j] - 1
                if a[i][j] <= a[i][j-1] or a[i][j] <= a[i-1][j]:
                    return -1
                r = r + a[i][j]
                continue
            if a[i][j] == 0:
                a[i][j] = min(a[i+1][j] - 1, a[i][j+1] - 1)
            if a[i][j] <= a[i-1][j] or a[i][j] <= a[i][j-1]:
                return -1
            r = r + a[i][j]
        if a[i][-m] == 0:
            a[i][-m] = min(a[i+1][-m] - 1, a[i][-m+1] - 1)
        if a[i][-m] <= a[i-1][-m]:
            return -1
        r = r + a[i][-m]
    if a[0][-1] == 0:
        a[0][-1] = a[1][-1] - 1
    if a[0][-1] <= a[0][-2]:
        return -1
    r = r + a[0][-1]
    for i in range(-2, -m, -1):
        if a[0][i] == 0:
            a[0][i] = min(a[0][i+1] - 1, a[1][i] - 1)
        if a[0][i] <= a[0][i-1]:
            return -1
        r = r + a[0][i]
    if a[0][0] == 0:
        a[0][0] = min(a[0][1] - 1, a[1][0] - 1)
    r = r + a[0][0]
    return r


print(result(a, n, m))


n,m = list(map(int,input().split()))
a = [[int(i) for i in input().split()]for _ in range(n)]
ans = 0
for i in range(n-1,-1,-1):
    for j in range(m-1,-1,-1):
        if a[i][j] == 0:
            temp = min(a[i+1][j],a[i][j+1])-1
            if temp > max(a[i-1][j],a[i][j-1]):
                ans += temp
                a[i][j] = temp
            else:
                print(-1)
                return
        else:
            ans += a[i][j]
            if 0 < i < n-1:
                if a[i-1][j] < a[i][j] < a[i+1][j]:
                    continue
                else:
                    print(-1)
                    return
            if 0 < j < m-1:
                if a[i][j-1] < a[i][j] < a[i][j+1]:
                    continue
                else:
                    print(-1)
                    return
print(ans)


a=[int(a)for a in input().split()]
b=list(list())
su=0
ans=0
cp=0
for j in range (a[0]):
        c=[int(a) for a in input().split()][:a[1]]
        b.append(c)
for i in range(a[0]-1,-1,-1):
    for j in range(a[1]-1,-1,-1):
        if b[i][j]==0:
            b[i][j]=int(min(b[i][j+1],b[i+1][j]))-1
for z in b:
    for r in z:    
        su+=r
for i in range(a[0]):
    for j in range(a[1]):
        try:
            if b[i][j]>=b[i+1][j]:
                ans=-1
                break
        except:
            cp+=1
            #print(i,j)
        try:
            if b[i][j]>=b[i][j+1]:
                ans=-1
                break
        except:
            cp+=1
            #print(i,j)

if ans==-1:
    print(-1)
else:
    print(su)

n, m = list(map(int,input().split()))
matrix = [0] * n
ok = 0
for q in range(0,n):
    row = list(map(int,input().split()))
    matrix[q] = row

zero = 0
# def when_no_zero(n,m):
#     print("n",n,"m",m)
#     for z in range(0,n-1):
#         for x in range(0,m-1):
#             if(matrix[z][x]<matrix[z][x+1] and matrix[z][x]<matrix[z+1][x]):
#                 pass
#             else:
#                 print("-1")
#                 ok = 1
#                 break

def no_zeros(n,m):
    for g in range(0,n):
        for k in range(0,m-1):
            if(matrix[g][k]<matrix[g][k+1]):
                pass
            else:
                return -1
    for l in range(0,m-1):
        for m in range(0,n):
            if(matrix[l][m]<matrix[l+1][m]):
                pass
            else:
                return -1
    return find_sum(matrix)

def find_zero_value():
    for i in range(n-1,0,-1):
        for j in range(m-1,0,-1):
            if matrix[i][j] == 0:
                right = matrix[i][j+1]
                down = matrix[i+1][j]
                op_min = min([right,down])
                up = matrix[i-1][j]
                left = matrix[i][j-1]
                op_max = max([up,left])
                if(op_min-1>op_max):
                    matrix[i][j] = op_min-1
                else:
                    return -1
    return matrix
            
def find_sum(maz):
    su = 0
    for i in range(0,n):
        for j in range(0,m):
            su = su + matrix[i][j]
    return su


for i in range(0,n):
    for j in range(0,m):
        if matrix[i][j] == 0:
            zero = 1
            break

if(zero==0):
    print(no_zeros(n,m))
else:
    maz = find_zero_value()
    if(maz == -1):
        print("-1")
    else:
        summ = find_sum(maz)
        print(summ)


n, m = map(int, input().split())
matrix, result, ck = [], 0, False
for _ in range(n):
    matrix.append(list(map(int, input().split())))
for i in range(n-1, -1, -1):
    for j in range(m-1, -1, -1):
        if matrix[i][j] == 0:
            matrix[i][j] = min(matrix[i][j+1], matrix[i+1][j]) - 1
for i in range(n-1):
    for j in range(m-1):
        if matrix[i][j] >= matrix[i][j+1] or matrix[i][j] >= matrix[i+1][j]:
            ck = True
            break
        if ck:
            break
for row in matrix:
    result += sum(row)
if ck or matrix[-1][-1] <= matrix[-2][-1] or matrix[-1][-1] <= matrix[-1][-2]:
    print(-1)
else:
    print(result)

def rising(matr, n, m):
    s = matr[n - 1][m - 1]
    for j in range(m - 2, -1, -1):
        if matr[n - 1][j] < matr[n - 1][j + 1]:
            if matr[n - 1][j] == 0:
                matr[n - 1][j] = matr[n - 1][j + 1] - 1
            s += matr[n - 1][j]
        else:
            return -1
    for i in range(n - 2, -1, -1):
        if matr[i][m - 1] < matr[i + 1][m - 1]:
            if matr[i][m - 1] == 0:
                matr[i][m - 1] = matr[i + 1][m - 1] - 1
            s += matr[i][m - 1]
        else:
            return -1
    for i in range(n - 2, -1, -1):
        for j in range(m - 2, -1, -1):
            if matr[i][j] < min(matr[i + 1][j], matr[i][j + 1]):
                if matr[i][j] == 0:
                    matr[i][j] = min(matr[i + 1][j], matr[i][j + 1]) - 1
                s += matr[i][j]
            else:
                return -1
    return s


N, M = [int(x) for x in input().split()]
matrix = list()
for y in range(N):
    row = [int(y) for y in input().split()]
    matrix.append(row)
print(rising(matrix, N, M))


def solve(arr, n, k):
	s = 0
	for i in reversed(list(range(n))):
		for j in reversed(list(range(k))):
			if arr[i][j] == 0:
				m = min(arr[i+1][j], arr[i][j+1])
				if m == 1:
					return -1
				arr[i][j] = m - 1

			s += arr[i][j]
			if j > 0 and arr[i][j-1] >= arr[i][j]:
				return -1
			if i > 0 and arr[i-1][j] >= arr[i][j]:
				return -1

	return s

n, k = [int(x) for x in input().split()]

arr = []
for _ in range(n):
	arr.append( [int(x) for x in input().split()] )

res = solve(arr, n, k)
print(res)


def ii():
  return int(input())
def ss():
  return [x for x in input()]
def si():
  return [int(x) for x in input().split()]
def mi():
  return map(int, input().split())

a, b = mi()
s = []
ans = 0
key = True

for i in range(a):
	i = si()
	s.append(i)
	ans += sum(i)

for i in range(a - 1):
	if not (s[i][0] < s[i + 1][0] and s[i][-1] < s[i + 1][-1]):
		ans = -1
		key = False
		break
for i in range(b - 1):
	if not (s[0][i] < s[0][i + 1] and s[-1][i] < s[-1][i + 1]):
		ans = -1
		key = False
		break

for i in range(a - 2, 0, -1):
	if not key:
		break
	for x in range(b - 2, 0, -1):
		if s[i][x] < s[i + 1][x] and s[i][x] < s[i][x + 1] and (min(s[i + 1][x], s[i][x + 1]) - max(s[i - 1][x], s[i][x - 1])) > 1:
			if s[i][x] == 0:
				s[i][x] = min(s[i + 1][x], s[i][x + 1]) - 1
				ans += s[i][x]
		else:
			ans = -1
			key = False
			break
print(ans)