#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#define N 1100000
using namespace std;
char s[N],t[N];
int p[N],head,tail,n,f[N],ans;
int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	scanf("%s",t+1);
	bool bo=1;
	for(int i=1;i<=n;i++) if(s[i]!=t[i]) {bo=0;break;}
	if(bo) {printf("0\n");return 0;}
	int j=n;
	for(int i=n;i>=1;i--)
	{
		j=min(j,i);
		while(j && s[j]!=t[i]) j--;
		if(j==0) {printf("-1\n");return 0;}
		f[i]=j;
	}
	head=1;tail=0;
	for(int i=n;i>=1;i--)
	{
		while(head<=tail && p[head]-(tail-head)>i) head++;
		if(f[i]!=f[i+1]) p[++tail]=f[i];
		ans=max(ans,tail-head+1);
	}
	printf("%d\n",ans);
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int dh[4] = {1, 0, -1, 0};
int dw[4] = {0, 1, 0 , -1};

const int INF = 100000;
int main() {
  
  int wcount = 0;
  char grid[100][100];
  int H, W;
  cin >> H >> W;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == '.') wcount++;
    }
  }
  
  
  int dp[100][100];
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      dp[i][j] = INF;
    }
  }
  
  dp[1][1] = 0;
  
  
  queue<pair<int,int>> que;
  pair<int, int> p = make_pair(1,1);
  que.push(p);
  
  while(que.size()) {
    pair<int, int> p = que.front(); 
    que.pop();
    
    int h = p.first, w = p.second;
    
    for (int i = 0; i < 4; i++) {
      int nh = h + dh[i], nw = w + dw[i];
      
      if (grid[nh][nw] == '.' && dp[nh][nw] == INF && nh >=1 && nh <= H && nw >= 1 && nw <= W) {
        dp[nh][nw] = dp[h][w] + 1;        
        pair<int, int> np = make_pair(nh,nw);
        que.push(np);
      }
    }
  }
  
  if (dp[H][W] == INF) {
    cout << -1 << endl;
  } else {
    cout << wcount - dp[H][W] - 1 << endl;
  }
  
  
  
}