//BAEKJOON 2206
#include <stdio.h>
#include <queue>
using namespace std;
struct node {
int x;
int y;
};
int n, m, flag=0;
int G[1010][1010], depth[2][1010][1010], dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
//depth 삼차원 ? 부수고/안 부수고
int main() {
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%1d", &G[i][j]);
queue<node> q;
q.push({1, 1});
depth[0][1][1]=depth[1][1][1]=1;
while(!q.empty()) {
node S=q.front();
q.pop();
int x=S.x, y=S.y;
for(int i=0; i<4; i++) {
int nx=x+dx[i], ny=y+dy[i];
if(1<=nx&&nx<=n && 1<=ny&&ny<=m) {
if(!G[nx][ny] && !depth[0][nx][ny]) {
q.push({nx, ny});
depth[0][nx][ny]=depth[0][x][y]+1;
}
else (!flag && G[nx][ny] && !depth[0][nx][ny]) {
q.push({nx, ny});
depth[1][nx][ny]=depth[0][x][y]+1;
}
}
}
}
if(!depth[0][n][m]) depth[0][n][m]=-1;
printf("%d", depth[0][n][m]);
}
To embed this project on your website, copy the following code and paste it into your website's HTML: