#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 int main(){ int H,W; cin>>H>>W; vector a(H,vector(W,-1)); rep(i,H){ if(i==0||i==H-1)continue; rep(j,W){ cin>>a[i][j]; } } rep(i,H){ a[i].insert(a[i].begin(),0); a[i].push_back(0); } vector dis(H,vector(W+2,Inf64)); priority_queue>,vector>>,greater>>> Q; rep(i,H){ dis[i][0] = 0; Q.emplace(0,make_pair(i,0)); } while(Q.size()>0){ long long D = Q.top().first; int x = Q.top().second.first; int y = Q.top().second.second; Q.pop(); if(dis[x][y]!=D)continue; for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ int xx = x+i,yy = y+j; if(xx<0||xx>=H||yy<0||yy>=W+2)continue; //cout<nD){ dis[xx][yy] = nD; Q.emplace(nD,make_pair(xx,yy)); } } } } /*rep(i,H){ rep(j,W+2){ cout<