题意:给你一个n*m的矩阵,你从(1,0)位置射出一个平行于x轴的光,遇到#号的时候,你可以选择转弯然后你要要到达(n,m+1)这个位置,问你最少用多少个#

思路:最短路一波



#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
vector<int>e[maxn];
int n,m;
int vis[maxn];
int d[maxn];
void bfs()
{
	queue<int>q;
	vis[n]=1;
	d[n]=0;
	q.push(n);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		for (int i = 0;i<e[u].size();i++)
		{
			int v = e[u][i];
			if (!vis[v])
			{
				vis[v]=1;
				q.push(v);
				d[v]=d[u]+1;
			}
		}
	}
}
int main()
{
    scanf("%d%d",&n,&m);
	for (int i = 1;i<=n;i++)
		for (int j = 1;j<=m;j++)
		{
			char c;
			scanf(" %c",&c);
			if (c=='#')
			{
				e[i].push_back(j+n);
				e[j+n].push_back(i);
			}
		}
	bfs();
	if (d[1])
		printf("%d\n",d[1]);
	else
		printf("-1\n");
}




Description



"The Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.

The Chamber of Secrets is an n × m



 The left light ray passes through a regular column, and the right ray — through the magic column.


The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position.



 This figure illustrates the first sample test.


Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.



Input



The first line of the input contains two integer numbers n and m (2 ≤ n, m ≤ 1000). Each of the next n lines contains m characters. Each character is either "." or "#" and represents one cell of the Chamber grid. It's "." if the corresponding cell is empty and "#" if it's a regular column.



Output



Print the minimum number of columns to make magic or -1 if it's impossible to do.



Sample Input



Input



3 3 .#. ... .#.



Output



2



Input



4 3 ##. ... .#. .#.



Output



2



Hint



The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.