#include using namespace std; int main() { int n, m; cin >> n >> m; vector a(n); for (int i = 0; i < n; i++) cin >>a[i]; vector> dist(n, vector(m, -1)); queue> q; q.push({0, 0}); dist[0][0] = 1; vector dx = {1, -1, 0, 0}; vector dy = {0, 0, 1, -1}; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] == '.' && dist[nx][ny] == -1) { q.push({nx, ny}); dist[nx][ny] = dist[x][y] + 1; } } } if (dist[n-1][m-1] == -1)cout << -1 << endl; else cout << dist[n-1][m-1] << endl; return 0; }