#include <vector>
#include <queue>
#include <iostream>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int visited[100][100];
int solution(vector<vector<int> > maps)
{
int answer = 100 * 100 + 1;
int n = maps.size(), m = maps[0].size();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
visited[i][j] = 0;
}
// pos, depth
queue<pair<pair<int, int>, int>> q;
q.push({{n - 1, m - 1}, 1});
while(!q.empty())
{
auto [pos, depth] = q.front();
q.pop();
int h = pos.first, w = pos.second;
if (h == 0 && w == 0)
{
answer = min(answer, depth);
continue;
}
if (depth >= answer)
continue;
if (visited[h][w] == 1)
continue;
visited[h][w] = 1;
for (int i = 0; i < 4; i++)
{
int nextY = h + dy[i];
int nextX = w + dx[i];
if (nextY < 0 || nextY > n - 1 || nextX < 0 || nextX > m - 1 || maps[nextY][nextX] == 0 || visited[nextY][nextX] == 1)
continue;
q.push({{nextY, nextX}, depth + 1});
}
}
return answer == 10001 ? -1 : answer;
}