#include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(int i=int(x);i<(int)(n);i++)
#define rep(i,n) for(ll i = 0;i < n;++i)
#define rrep(i,n) for(int i=int(n-1);i>=0;i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define coy cout<<"Yes"<<endl
#define con cout<<"No"<<endl
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9+7
#define LINF (ll)1e18+10  
using ll = long long;

using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
using PIP = pair<int, P>;
using Ps = pair<int, string>;

using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T>
bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template <typename T>
bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }

//const ll mod=998244353;
const ll mod = 1e9 + 7;
const ll MAX = 100;
template <typename T>
T abs(T a) { if (a < 0)return -a; else return a; }//2020/09/30 stdlib has abs(long) abs(long long) error
//////////////////////////////////////


int main() {
    int r, c;
    cin >> r >> c;


    vvi a(r, vi(c - 1)), b(r - 1, vi(c));

    rep(i, r)rep(j, c - 1)cin >> a[i][j];
    rep(i, r - 1)rep(j, c)cin >> b[i][j];


    vvi dist(r, vi(c, INF));
    --r, --c;

    priority_queue<PIP, vector<PIP>, greater<PIP>> q;

    dist[0][0] = 0;
    q.push(PIP(0, P(0, 0)));

    auto ok = [&](int x, int y) {
        return x >= 0 & x <= r && y >= 0 && y <= c;
    };
    while (q.size()) {
        PIP t = q.top(); q.pop();
        P now = t.second;
        int d = t.first;

        if (d > dist[now.fs][now.sc])continue;

        {
            int nx = now.fs, ny = now.sc + 1;
            if (ok(nx, ny)) {
                int nd = dist[now.fs][now.sc] + a[now.fs][now.sc];
                if (chmin(dist[nx][ny], nd))q.push(PIP(dist[nx][ny], P(nx, ny)));
            }
        }

        {
            int nx = now.fs, ny = now.sc - 1;
            if (ok(nx, ny)) {
                int nd = dist[now.fs][now.sc] + a[nx][ny];
                if (chmin(dist[nx][ny], nd))q.push(PIP(dist[nx][ny], P(nx, ny)));
            }
        }
        {
            int nx = now.fs + 1, ny = now.sc;
            if (ok(nx, ny)) {
                int nd = dist[now.fs][now.sc] + b[now.fs][now.sc];
                if (chmin(dist[nx][ny], nd))q.push(PIP(dist[nx][ny], P(nx, ny)));
            }
        }

        rep(i, now.fs) {
            int nx = now.fs - i - 1, ny = now.sc;
            if (ok(nx, ny)) {
                int nd = dist[now.fs][now.sc] + i + 2;
                if (chmin(dist[nx][ny], nd))q.push(PIP(dist[nx][ny], P(nx, ny)));
            }
        }
    }

    cout << dist[r][c] << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ALL(x) (x).begin(),(x).end()
const int IINF = 1e9;
const LL LINF = 1e18;
const LL MOD = 1e9+7;

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    REP(i, H) {
        cin >> S[i];
    }

    int edge = 0;

    REP(i, H - 1) {
        REP(j, W - 1) {
            int white = 0;
            FOR(k, i, i + 2) {
                FOR(l, j, j + 2) {
                    if(S[k][l] == '.') {
                        white++;
                    }
                }
            }
            if(white == 1 || white == 3) {
                edge++;
            }
        }
    }
    cout << edge << endl;
    return 0;
}
