#include using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin>>N>>M; vector>A(N,vector(M)); for(int i=0;i>A[i][j]; } vector>>dist(N,vector>(M,{(long long)1e18,(long long)1e18})); priority_queue,vector>,greater>>pq; pq.push({0,0,0,0}); dist[0][0][0]=0; while(pq.size()){ long long d=pq.top()[0],f=pq.top()[1],r=pq.top()[2],e=pq.top()[3]; pq.pop(); if(dist[f][r][e]0&&dist[f][r-1][0]>d){ dist[f][r-1][0]=d; pq.push({d,f,r-1,0}); } if(r+1d){ dist[f][r+1][0]=d; pq.push({d,f,r+1,0}); } if(dist[f][r][1]>d+A[f][r]){ dist[f][r][1]=d+A[f][r]; pq.push({d+A[f][r],f,r,1}); } }else{ if(dist[f][r][0]>d){ dist[f][r][0]=d; pq.push({d,f,r,0}); } if(f+1d+A[f+1][r]){ dist[f+1][r][1]=d+A[f+1][r]; pq.push({d+A[f+1][r],f+1,r,1}); } } } long long ans=1e18; for(int i=0;i