#include <iostream>
#include <string>
#include <queue>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;

    int arr[n][m];
    int arr2[n][m] = {0};

    string sss;
    for (int i = 0; i < n; i++) {
        cin >> sss;
        for (int j = 0; j < m; j++) {
        arr[i][j] = sss[j] - '0';
        }
    }
    

    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};

    queue<pair<int,int>> q;
    q.push({0, 0});
    arr2[0][0] = 1; 

    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
      //  cout<<x<<y<<"start";
      //  cout<<"\n";
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            cout << arr[nx][ny];
            cout<<"\n";
            cout<<arr2[nx][ny];
            cout <<"number";
            cout<<"\n";
            if (nx < 0 || ny < 0 || nx >= n || ny >= m){
                continue;
            }
            
            if (arr[nx][ny] == 1 && arr2[nx][ny] == 0) {
                arr2[nx][ny] = arr2[x][y] + 1;
                q.push({nx, ny});
               // cout<<nx<<ny;
             //   cout<<"\n";
            }
        }
    }

//    cout << arr2[n-1][m-1];
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: