BFS starts at some node and then goes over all it's neighbours.
If we have an adjacency matrix it means that it will start at some row and will go over all columns of this row.
When going over the row it will put more nodes inside BFS's queue and because we're going over the columns of the row the first node in the queue will be the first node in the first column.
So in total we can say the we start at some node N, then go over all other nodes n_1,..., n_n
then go to n_1 and go over all other nodes in it's row and add them to the queue. Then we continue to some other row of a node we added to the queue earlier.
M nodes in total and let's say that we start with the node at grid[0][0]
grid[0][0], ..., grid[0][M-1]
//Added to the queue grid[0][1], grid[0][2]
grid[1,0], ..., grid[1][M-1]
//Added to the queue grid[1][3], grid[1][4]
grid[2,0], ..., grid[2][M-1]
and so on...