import io import sys _INPUT = """\ 6 5 4 2 5 7 11 1 6 9 10 3 4 8 12 5 4 2 5 -1 11 -1 6 9 10 3 -1 8 12 5 4 2 5 -1 11 -1 -1 -1 10 3 -1 8 12 """ def solve(test): inf=10**20 H,W=map(int,input().split()) A=[list(map(int,input().split())) for _ in range(H-2)] dp=[inf]*((H-2)*W) def idx(i,j): return i*W+j for i in range(H-2): if A[i][0]!=-1: dp[idx(i,0)]=A[i][0] for j in range(W-1): for i in range(H-2): if A[i][j+1]!=-1: for k,l in [(i-1,j),(i,j),(i+1,j)]: if 0<=k