#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;

#define sz(x) (int)(x).size()

template <typename T> bool ckmin (T& a, T b) { return (b < a) ? a = b, 1 : 0; }
template <typename T> bool ckmax (T& a, T b) { return (b > a) ? a = b, 1 : 0; }

#ifdef XVENOM
  #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: " << dbg_str(__VA_ARGS__)
#else
  #define dbg(...) ;
#endif
template <typename U, typename V> string to_string (pair<U, V>);
string to_string (const char* e) { return "\"" + string(e) + "\""; }
string to_string (string& e) { return "\"" + e + "\""; }
string to_string (char e) { return "\'" + string(1, e) + "\'"; }
string to_string (bool e) { return e ? "true" : "false"; }
template <typename T> string to_string (T e) {
  string s = "[ "; for (auto& x : e) s += to_string(x) + " "; return s + "]";
}
template <typename U, typename V> string to_string (pair<U, V> e) {
  return "(" + to_string(e.first) + ", " + to_string(e.second) + ")";
}
string dbg_str () { return " \n"; }
template <typename U, typename... V> string dbg_str (U u, V... v) {
  return " " + to_string(u) + dbg_str(v...);
}
/* *** */
const ll Mod = 1000000007;
/* --- */
using MT = decay<decltype(Mod)>::type;

MT inverse (MT a, MT n) {
  MT u = 0, v = 1;
  while (a != 0) {
    MT q = n / a;
    swap(u, v); v -= q * u;
    swap(n, a); a -= q * n;
  }
  assert(n == 1);
  return u;
}

class mint {
private: MT x;
public:
  void normalize() {
    if (x >= Mod || x < 0) x = (x % Mod) + Mod;
    if (x >= Mod) x -= Mod;
  }
  mint(): x(0) {}
  mint(MT z): x(z) { normalize(); }
  const MT& operator() () const { return x; }

  mint operator+ (const mint& z) { MT r = x + z(); if (r >= Mod) r -= Mod; return mint(r); }
  mint operator- (const mint& z) { MT r = x - z(); if (r < 0) r += Mod; return mint(r); }
  mint operator* (const mint& z) { MT r = x * z(); if (r >= Mod) r %= Mod; return mint(r); }
  mint operator/ (const mint& z) { return (*this) * mint(inverse(z(), Mod)); }

  mint operator+= (const mint& z) { (*this) = (*this) + z; return (*this); }
  mint operator-= (const mint& z) { (*this) = (*this) - z; return (*this); }
  mint operator*= (const mint& z) { (*this) = (*this) * z; return (*this); }
  mint operator/= (const mint& z) { (*this) = (*this) / z; return (*this); }

  friend std::istream& operator>> (std::istream&, mint&);
};

std::istream& operator>> (std::istream& stream, mint& p) {
  MT v; stream >> v; p.x = v; p.normalize(); return stream;
}
std::ostream& operator<< (std::ostream& stream, const mint& p) { return stream << p(); }
mint operator+ (const MT& p, const mint& q) { return mint(p) + q; }
mint operator- (const MT& p, const mint& q) { return mint(p) - q; }
mint operator* (const MT& p, const mint& q) { return mint(p) * q; }
mint operator/ (const MT& p, const mint& q) { return mint(p) / q; }
string to_string (const mint& p) { return to_string(p()); }

mint powMod (mint a, MT b) {
  mint res = 1, x = a;
  while (b > 0) {
    if (b & 1) res *= x;
    x *= x;
    b >>= 1;
  }
  return res;
}
/* --- */
const int N = 2000 + 66;
mint f[N][N], g[N][N], h[N][N], ans[N][N];
string grid[N];
int n, m;

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string t; cin >> t;
    grid[i] = " " + t;
  }
  
  f[1][1] = g[1][1] = h[1][1] = ans[1][1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i == 1 && j == 1) continue;
      if (grid[i][j] == '#') continue;
      ans[i][j] = f[i - 1][j] + g[i][j - 1] + h[i - 1][j - 1];
      f[i][j] = f[i - 1][j] + ans[i][j];
      g[i][j] = g[i][j - 1] + ans[i][j];
      h[i][j] = h[i - 1][j - 1] + ans[i][j];
    }
  }
  cout << ans[n][m] << '\n';
}


#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 100005;

int n, d;
int a[N], b[N];

int minu[N];

vector<int> v[N];

void solv()
{
    scanf("%d%d", &n, &d);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &b[i]);

    a[n + 1] = b[n + 1] = d + 1;

    for (int i = 1; i <= n; ++i)
        minu[i] = N;

    map<int, int> mp;
    for (int i = 0; i <= n + 1; ++i)
    {
        mp[a[i] - i] = i;
        if (mp.find(b[i] - i) != mp.end())
        {
            if (mp[b[i] - i] != i)
                v[mp[b[i] - i]].push_back(i);
            minu[i] = min(minu[i], i - mp[b[i] - i]);
        }
    }

    mp.clear();
    for (int i = n + 1; i >= 0; --i)
    {
        mp[a[i] - i] = i;
        if (mp.find(b[i] - i) != mp.end())
        {
            if (mp[b[i] - i] != i)
                v[mp[b[i] - i]].push_back(i);
            minu[i] = min(minu[i], mp[b[i] - i] - i);
        }
    }

    for (int i = 0; i <= n + 1; ++i)
    {
        if (minu[i] == N)
        {
            printf("-1\n");
            return;
        }
    }

    ll ans = 0;
    for (int i = 0; i <= n + 1; ++i)
    {
        if (v[i].empty())
            continue;
        sort(all(v[i]));
        if (v[i][0] < i)
            ans += (i - v[i][0]);
        if (v[i].back() > i)
            ans += (v[i].back() - i);
    }

    printf("%lld\n", ans);
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    //ios_base::sync_with_stdio(false), cin.tie(0);
    solv();
    return 0;
}
