#include <iostream>
#include <string>
using namespace std;

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

int arr[1001][1001];

void change(int i, int j, int n, int m){
    arr[i][j] = 2;
    for (int k = 0; k < 4; k++) {
        int ny = i + dy[k];
        int nx = j + dx[k];
        if(ny >= 0 && ny < n && nx >= 0 && nx < m){
            if(arr[ny][nx] == 0){
                change(ny, nx, n, m);
            }
        }
    }
}

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

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

    for (int i = 0; i < m; i++) {
        if (arr[0][i] == 0) {
            change(0, i, n, m);
        }
    }
    for (int i = 0; i < m; i++) {
        if (arr[n-1][i] == 2) {
            cout << "YES";
            return 0;
        }
    }

    cout << "NO";
    return 0;
}

Embed on website

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