Help Needed with AtCoder ABC 176 D

I have used 0/1 BFS in this problem, but somehow getting WA on 3 test cases.

Here’s my submission:

As an abstraction, I have converted every point (i,j) on the maze to a unique number (w*i+j) as I find that more intuitive to me.

What am I doing wrong?