'i do not understand bfs algorithm

I am trying to solve the acmicpc.net code challenge Tomato (Korean):

Cheolsu's Tomato Farm has a large warehouse for storing tomatoes. Tomatoes are stored in a warehouse by putting them one by one in a box in a grid as shown in the picture below.

enter image description here

Some of the tomatoes stored in the warehouse are ripe, but there may be some that are not yet ripe. After one day of storage, the unripe tomatoes adjacent to the ripe tomatoes are affected by the ripe tomatoes and ripen. Adjacent to one tomato means the tomato in four directions: left, right, front, and back. There is no effect on diagonal tomatoes, and it is assumed that tomatoes do not ripen on their own. Cheolsu wants to know how the minimum number of days needed for the tomatoes stored in the warehouse to have ripened.

Given the size of the grid for storing tomatoes and the information of ripe and unripe tomatoes, write a program to find the minimum number of days needed to all the tomatoes to have become ripe. Take note that some boxes of the grid might not contain a tomato.

This is a code that I recombined while googling..

#include<iostream>
#include<queue>
#include<algorithm>


using namespace std;

int board[1002][1002];
int dist[1002][1002];
int n, m;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int main(void) {

    cin >> m >> n;
    queue<pair<int, int> > Q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> board[i][j];
            if (board[i][j] == 1) //익은 토마토
                Q.push(make_pair(i,j));//큐에 익은토마토 넣기
            if (board[i][j] == 0)//안익은 토마토
                dist[i][j] = -1;
        }
    }
    while (!Q.empty()) {
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        for (int dir = 0; dir < 4; dir++) {
            int nx = x + dx[dir];
            int ny = y + dy[dir];

            if (nx >= 0 && ny >= 0 && nx < n && ny < m && dist[nx][ny] < 0) {
                dist[nx][ny] = dist[x][y] + 1;
               
                Q.push(make_pair(nx, ny));
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (dist[i][j] == -1) {
                cout << -1;
                return 0;
            }
            ans = max(ans, dist[i][j]);
        }
    }
    cout << ans;
} '''

i need a code review that why do i have to set dist[i][j] to -1 if board[i][j]==0 ? dist is for counting days for ripe tomatoes

why initialize dist to -1 for counting days?

and I don't get it the part of

for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (dist[i][j] == -1) {
                cout << -1;
                return 0;
            }
            ans = max(ans, dist[i][j]);
        }
    }

i dont' get it :-(



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source