H, W = map(int, input().split()) A = [[0 for i in range(W)]] + [list(map(int, input().split())) for i in range(H-2)] + [[0 for i in range(W)]] d = [-1, 0, 1] ans = 10**10 for i in range(1, H-1): now = i cunt = 0 now_2 = i ok = True if A[now][0] != -1: cunt += A[now][0] else: continue for j in range(1, W): wall_now = 10**10 for k in d: if now + k <= 0 or now + k >= H-1: continue if A[now+k][j] == -1: continue if wall_now > A[now+k][j]: wall_now = A[now+k][j] now_2 = i+k if wall_now == 10**10: ok = False break now = now_2 cunt += wall_now if ok: ans = min(ans, cunt) if ans == 10**10: print(-1) else: print(ans)