int res = 1; bool flag = false; visited[0][0] = true;
while (true) { int que_size = (int) Q.size(); for (int i = 0; i < que_size; i++) { int x = Q.front() / 100; int y = Q.front() % 100; Q.pop();
if (x == N - 1 && y == M - 1) { flag = true; }
for (int d = 0; d < 4; d++) { int now_x = x + dx[d]; int now_y = y + dy[d]; if (now_x < 0 || now_x >= N || now_y < 0 || now_y >= M) { continue; } if (!map[now_x][now_y]) { continue; } if (visited[now_x][now_y]) { continue; } visited[now_x][now_y] = true; Q.push(now_x * 100 + now_y); } } if (flag) { break; }
res++; } return res; }
intmain(int argc, constchar *argv[]){ int N, M; scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { scanf("%1d", &map[i][j]); } } printf("%d\n", bfs(N, M));