입력을 받을때 공백이 없기때문에 scanf %1d를 이용하였다.
또한 0,0 으로 시작하였기때문에 return 은 N-1,M-1로 해야한다.
#include <string>
#include <vector>
#include<iostream>
#include<queue>
using namespace std;
int N, M;
int graph[101][101];
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,1,-1 };
int bfs(int y, int x) {
queue<pair<int, int>> q;
q.push({ y,x });
while (!q.empty()) {
int y = q.front().first;
int x = 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 >= M || ny < 0 || ny >= N) continue;
if (graph[ny][nx] == 0) continue;
if (graph[ny][nx] == 1) {
graph[ny][nx] = graph[y][x] + 1;
q.push({ ny,nx });
}
}
}
return graph[N - 1][M - 1];
}
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
scanf("%1d", &graph[i][j]);
}
}
cout << bfs(0, 0);
return 0;
}