#pragma GCC optimize("Ofast")
//#ifndef ONLINE_JUDGE 
//#define _GLIBCXX_DEBUG
//#endif
#ifdef ONLINE_JUDGE 
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <math.h>
#include <complex>
 
 
using namespace std;
#ifdef ONLINE_JUDGE 
using namespace atcoder;
#endif
#define rep(i,n) for (int i = 0;i < (int)(n);i++)
using ll = long long;
#ifdef ONLINE_JUDGE 
//using mint = modint998244353;
//using mint = modint;
using mint = modint1000000007;
#endif
const ll MOD=1000000007;
//const ll MOD=998244353;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);
int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
int dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0};
 
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
 
 
 
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  //cout << fixed << setprecision(15);
 
  ll R,C; cin>>R>>C;
  vector<vector<ll>> A(R,vector<ll>(C-1)),B(R-1,vector<ll>(C)); 
  rep(i,R) rep(j,C-1) cin>>A[i][j];
  rep(i,R-1) rep(j,C) cin>>B[i][j];
  
  vector<vector<pair<ll,ll>>> graph(2*R*C,vector<pair<ll,ll>>(0));
  auto f=[&](int i,int j,int k) {return i*C+j+k*R*C;};
  
  rep(r,R) rep(c,C-1) graph[f(r,c,0)].push_back({f(r,c+1,0),A[r][c]});
  rep(r,R) for(int c=1;c<C;c++) graph[f(r,c,0)].push_back({f(r,c-1,0),A[r][c-1]});
  rep(r,R-1) rep(c,C) graph[f(r,c,0)].push_back({f(r+1,c,0),B[r][c]});
  for(int r=1;r<R;r++) rep(c,C) {
    graph[f(r,c,0)].push_back({f(r,c,1),1});
    graph[f(r,c,1)].push_back({f(r-1,c,1),1});
    graph[f(r-1,c,1)].push_back({f(r-1,c,0),0});
  }
  
  vector<ll> dist(2*R*C,INF);
  priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> pq;
  pq.push({0,0});
  while(!pq.empty()) {
    auto [c,v]=pq.top(); pq.pop();
    if(!chmin(dist[v],c)) continue;
    if(v==f(R-1,C-1,0)) break;
    
    for(auto &[nv,nc]:graph[v]) {
      pq.push({c+nc,nv});
    }
  }
  
  cout<<dist[f(R-1,C-1,0)]<<'\n';
  
  
  return 0;
}
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define    endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define vvi            vector<vi>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
#define tr(c,i)        for(auto i = (c).begin(); i != (c).end(); i++)
#define present(c,x)    ((c).find(x) != (c).end()) 
#define cpresent(c,x)    (find(all(c),x) != (c).end()) 
using namespace std;
ll mod=1e9+7;
#define N  100005

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int TESTS=1;
    //cin>>TESTS;
    //ll cs=1;
    while(TESTS--)
    {
        char s,t; cin >> s >> t;
        if(s=='Y') cout << (char)toupper(t) << "\n";
        else cout << t << "\n";
    }
    return 0;
}