#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int n,m;
vector<int> f[2000051];
vector<int> val[2000051];
int dis[2000051];
bool b[2000051];
struct squ
{
int dis,p;
squ(){}
squ(int a,int b){dis=a;p=b;}
};
int INF=2100000000;
bool operator <(squ a,squ b){return a.dis>b.dis;}
priority_queue<squ> que;
void init()
{
scanf("%d%d",&n,&m);
if(n==1||m==1)
{
if(n>m)swap(n,m);
int ans=INF;
for(int i=1;i<m;i++)
{
int c;
scanf("%d",&c);
if(ans>c)ans=c;
}
if(ans==INF)
cout<<0<<endl;
else
cout<<ans<<endl;
}
else
{
int ins=(n-1)*(m-1);
int fin=2*ins+1;
//上点i*m+j 下点 i*m+j+ins;
//起点 0 终点 2*ins+1
for(int i=0;i<n;i++)
for(int j=1;j<m;j++)
{
int c;
scanf("%d",&c);
if(i==0)
{
int x=i*(m-1)+j;
f[0].push_back(x);
f[x].push_back(0);
val[0].push_back(c);
val[x].push_back(c);
}
if(i>0&&i<n-1)
{
int x=(i-1)*(m-1)+j+ins,y=i*(m-1)+j;
f[y].push_back(x);
f[x].push_back(y);
val[x].push_back(c);
val[y].push_back(c);
}
if(i==n-1)
{
int x=(i-1)*(m-1)+j+ins;
f[x].push_back(fin);
f[fin].push_back(x);
val[fin].push_back(c);
val[x].push_back(c);
}
}
for(int i=0;i<n-1;i++)
for(int j=1;j<=m;j++)
{
int c;
scanf("%d",&c);
if(j==1)
{
int x=i*(m-1)+j+ins;
f[x].push_back(fin);
f[fin].push_back(x);
val[fin].push_back(c);
val[x].push_back(c);
}
if(j>1&&j<m)
{
int x=i*(m-1)+j-1,y=i*(m-1)+j+ins;
f[y].push_back(x);
f[x].push_back(y);
val[y].push_back(c);
val[x].push_back(c);
}
if(j==m)
{
int x=i*(m-1)+j-1;
f[x].push_back(0);
f[0].push_back(x);
val[0].push_back(c);
val[x].push_back(c);
}
}
for(int i=0;i<n-1;i++)
for(int j=1;j<m;j++)
{
int c;
scanf("%d",&c);
int x=i*(m-1)+j,y=i*(m-1)+j+ins;
f[y].push_back(x);
f[x].push_back(y);
val[y].push_back(c);
val[x].push_back(c);
}
que.push(squ(0,0));
for(int i=1;i<=fin;i++)
dis[i]=INF;
dis[0]=0;
for(;!que.empty();)
{
squ x=que.top();
que.pop();
int d=x.dis;
if(x.p==fin)break;
if(b[x.p])continue;
b[x.p]=1;
for(int i=0;i<f[x.p].size();i++)
{
int next=f[x.p][i];
if(!b[next]&&d+val[x.p][i]<dis[next])
{dis[next]=d+val[x.p][i];
que.push(squ(dis[next],next));}
}
}
cout<<dis[fin]<<endl;
}
}
int main()
{
init();
return 0;
}