
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2010;
const ll mod=1e9+7;
char g[N][N];
int n,m;
ll base[N*N];
int w[N][N];
int main()
{
    IO;
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>g[i]+1;
        
        int cnt=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++) cnt+=(int)g[i][j]=='.';
            
        base[0]=1;
        for(int i=1;i<=cnt;i++) base[i]=base[i-1]*2%mod;
        ll res=0;
        for(int i=1;i<=n;i++)
        {
            int j=1;
            while(j<=m)
            {
                while(g[i][j]=='#') j++;
                
                int now=1,k=1;
                while(j+k<=m&&g[i][j+k]=='.') now++,k++;
                for(int p=j;p<j+k;p++) w[i][p]+=now;
                j=j+k;
            }
        }
        for(int i=1;i<=m;i++)
        {
            int j=1;
            while(j<=n)
            {
                while(g[j][i]=='#') j++;
                int now=1,k=1;
                while(j+k<=n&&g[j+k][i]=='.') now++,k++;
                for(int p=j;p<j+k;p++) w[p][i]+=now;
                j=j+k;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(g[i][j]=='#') continue;
                int now=w[i][j]-1;
                res=(res+(base[now]-1)*base[cnt-now]%mod)%mod;
                
            }
        }
        
        cout<<(ll)(res+mod)%mod<<'\n';
        
    }
    return 0;
}
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
  for (auto e : v) {
    o << e << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M; cin >> N >> M;
  vv(vi) es(N, vvi(26));
  F0R(i, M) {
    int a, b; char c; cin >> a >> b >> c;
    a--; b--;
    es[a][c-'a'].push_back(b);
    es[b][c - 'a'].push_back(a);
  }
  // 
  int inf = INT32_MAX / 2;
  vvi dp(N, vi(N, inf));
  dp[0][N - 1] = 0;
  queue<pii> qu;
  qu.emplace(0, N - 1);
  int step = 0;
  while (sz(qu)) {
    queue<pii> nq;
    while (sz(qu)) {
      auto [u, v] = qu.front(); qu.pop();
      if (dp[u][v] < step) continue;
      //cout << (u + 1) << '-' << (v + 1) << newl;
      F0R(c, 26) {
        for (auto to : es[u][c]) {
          for (auto to2 : es[v][c]) {
            if (dp[to][to2] <= step + 1) continue;
            dp[to][to2] = step + 1;
            nq.emplace(to, to2);
          }
        }
      }
    }
    qu = move(nq);
    step++;
  }
  cout << (dp[N - 1][0] >= inf ? -1 : dp[N- 1][0]) << newl;
}
#endif
