Language: C++

import java.util.Scanner;
import java.util.Queue;
import java.util.ArrayDeque;


class Maze {
  private int width;
  private int height;
  private int px;
  private int py;
  private int length;
  private Cell[][] cells;
  private boolean[][] is_passed_through;

  static Cell[][] getInitialCells(int width, int height) {
    Cell[][] _cells = new Cell[height][width];
    for(int y = 0; y < height; y++) {
      for(int x = 0; x < width; x++) {
        _cells[y][x] = new Cell();
      }
    }
    return _cells;
  }

  static boolean[][] getInitialIsPassedThrough(int width, int height) {
    boolean[][] _is_passed_through = new boolean[height][width];
    for(int y = 0; y < height; y++) {
      for(int x = 0; x < width; x++) {
        _is_passed_through[y][x] = false;
      }
    }
    _is_passed_through[0][0] = true;
    return _is_passed_through;
  }

  Maze(int _width, int _height) {
    this(_width, _height, 0, 0, 1, getInitialCells(_width, _height), getInitialIsPassedThrough(_width, _height));
  }

  private Maze(int _width, int _height, int _px, int _py, int _length, Cell[][] _cells, boolean[][] _is_passed_through) {
    width  = _width;
    height = _height;
    px     = _px;
    py     = _py;
    length = _length;
    cells  = _cells;
    is_passed_through = _is_passed_through;
  }

  private Maze dupuricate() {
    // boolean[][] _is_passed_through = new boolean[height][width];
    // for(int y = 0; y < height; y++) {
    //   for(int x = 0; x < width; x++) {
    //     _is_passed_through[y][x] = isPassedThrough(x, y);
    //   }
    // }
    return new Maze(width, height, px, py, length, cells, is_passed_through);
  }

  public void setWallAround() {
    for(int y = 0; y < height; y++) {
      for(int x = 0; x < width; x++) {
        if(x == 0)          { cell(x, y).setLeft();   }
        if(x == width - 1)  { cell(x, y).setRight();  }
        if(y == 0)          { cell(x, y).setTop();    }
        if(y == height - 1) { cell(x, y).setBottom(); }
      }
    }
  }

  public void setWall(int p, int q, int isWall) {
    if(isWall == 0) {
      return;
    }

    int y = p / 2;
    int x = q;

    // vertical wall
    if(p % 2 == 0) {
      cell(x,     y).setRight();
      cell(x + 1, y).setLeft();
    }

    // horizontal wall
    else {
      cell(x, y    ).setBottom();
      cell(x, y + 1).setTop();
    }
  }

  public int getLength() {
    return length;
  }

  private Cell cell(int x, int y) {
    return cells[y][x];
  }

  private boolean isPassedThrough(int x, int y) {
    return is_passed_through[y][x];
  }

  private void passThrough() {
    is_passed_through[py][px] = true;
  }

  public Maze moveTop() {
    Maze dupuricated = dupuricate();
    dupuricated.py--;
    dupuricated.length++;
    dupuricated.passThrough();
    return dupuricated;
  }
  public Maze moveRight() {
    Maze dupuricated = dupuricate();
    dupuricated.px++;
    dupuricated.length++;
    dupuricated.passThrough();
    return dupuricated;
  }
  public Maze moveBottom() {
    Maze dupuricated = dupuricate();
    dupuricated.py++;
    dupuricated.length++;
    dupuricated.passThrough();
    return dupuricated;
  }
  public Maze moveLeft() {
    Maze dupuricated = dupuricate();
    dupuricated.px--;
    dupuricated.length++;
    dupuricated.passThrough();
    return dupuricated;
  }

  public boolean ableToGoTop() {
    return cell(px, py).ableToGoTop() && !isPassedThrough(px, py - 1);
  }
  public boolean ableToGoRight() {
    return cell(px, py).ableToGoRight() && !isPassedThrough(px + 1, py);
  }
  public boolean ableToGoBottom() {
    return cell(px, py).ableToGoBottom() && !isPassedThrough(px, py + 1);
  }
  public boolean ableToGoLeft() {
    return cell(px, py).ableToGoLeft() && !isPassedThrough(px - 1, py);
  }

  public boolean isFinished() {
    return px == width - 1 && py == height - 1;
  }

  public String debugString() {
    String debug = "";
    debug += "width = " + width + ", ";
    debug += "height = " + height + ", ";
    debug += "px = " + px + ", ";
    debug += "py = " + py + ", ";
    debug += "length = " + length;
    // debug += "\n";
    // for(int y = 0; y < height; y++) {
    //   for(int x = 0; x < width; x++) {
    //     debug += "cell(" + x + ", " + y + ") = " + cell(x, y).debugString() + "\n";
    //   }
    // }
    return debug;
  }
}

class Cell {
  private boolean top;
  private boolean right;
  private boolean bottom;
  private boolean left;

  Cell() {
    top    = true;
    right  = true;
    bottom = true;
    left   = true;
  }

  public void setTop()    { top    = false; }
  public void setRight()  { right  = false; }
  public void setBottom() { bottom = false; }
  public void setLeft()   { left   = false; }

  public boolean ableToGoTop()    { return top;    }
  public boolean ableToGoRight()  { return right;  }
  public boolean ableToGoBottom() { return bottom; }
  public boolean ableToGoLeft()   { return left;   }

  public String debugString() {
    return "top = " + (top ? "X" : " ") + ", right = " + (right ? "X" : " ") + ", bottom = " + (bottom ? "X" : " ") + ", left = " + (left ? "X" : " ") + ",";
  }
}

public class Main {
  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);

    while(true) {
      int width = sc.nextInt();
      int height = sc.nextInt();
      if(width == 0 && height == 0) {
        break;
      }

      // input wall data
      Maze maze = new Maze(width, height);
      maze.setWallAround();
      for(int i = 0; i < height * 2 - 1; i++) {
        for(int j = 0; j < width; j++) {
          if(i % 2 == 0 && j == width - 1) {
            continue;
          }
          maze.setWall(i, j, sc.nextInt());
        }
      }

      // do breadth first search
      int min = 0;
      Queue<Maze> queue = new ArrayDeque<Maze>();
      queue.add(maze);
      while(!queue.isEmpty()) {
        Maze current = queue.remove();

        if(current.isFinished()) {
          min = current.getLength();
          break;
        }

        if(current.ableToGoTop())    { queue.add(current.moveTop());    }
        if(current.ableToGoRight())  { queue.add(current.moveRight());  }
        if(current.ableToGoBottom()) { queue.add(current.moveBottom()); }
        if(current.ableToGoLeft())   { queue.add(current.moveLeft());   }
      }

      System.out.println(min);
    }
  }
}

Compiler output:
a.cc:1:1: error: 'import' does not name a type
    1 | import java.util.Scanner;
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:2:1: error: 'import' does not name a type
    2 | import java.util.Queue;
      | ^~~~~~
a.cc:2:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:3:1: error: 'import' does not name a type
    3 | import java.util.ArrayDeque;
      | ^~~~~~
a.cc:3:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:7:10: error: expected ':' before 'int'
    7 |   private int width;
      |          ^~~~
      |          :
a.cc:8:10: error: expected ':' before 'int'
    8 |   private int height;
      |          ^~~~
      |          :
a.cc:9:10: error: expected ':' before 'int'
    9 |   private int px;
      |          ^~~~
      |          :
a.cc:10:10: error: expected ':' before 'int'
   10 |   private int py;
      |          ^~~~
      |          :
a.cc:11:10: error: expected ':' before 'int'
   11 |   private int length;
      |          ^~~~
      |          :
a.cc:12:10: error: expected ':' before 'Cell'
   12 |   private Cell[][] cells;
      |          ^~~~~
      |          :
a.cc:12:11: error: 'Cell' does not name a type
   12 |   private Cell[][] cells;
      |           ^~~~
a.cc:13:10: error: expected ':' before 'boolean'
   13 |   private boolean[][] is_passed_through;
      |          ^~~~~~~~
      |          :
a.cc:13:11: error: 'boolean' does not name a type; did you mean 'bool'?
   13 |   private boolean[][] is_passed_through;
      |           ^~~~~~~
      |           bool
a.cc:15:10: error: 'Cell' does not name a type
   15 |   static Cell[][] getInitialCells(int width, int height) {
      |          ^~~~
a.cc:25:10: error: 'boolean' does not name a type; did you mean 'bool'?
   25 |   static boolean[][] getInitialIsPassedThrough(int width, int height) {
      |          ^~~~~~~
      |          bool
a.cc:40:10: error: expected ':' before 'Maze'
   40 |   private Maze(int _width, int _height, int _px, int _py, int _length, Cell[][] _cells, boolean[][] _is_passed_through) {
      |          ^~~~~
      |          :
a.cc:40:72: error: 'Cell' has not been declared
   40 |   private Maze(int _width, int _height, int _px, int _py, int _length, Cell[][] _cells, boolean[][] _is_passed_through) {
      |                                                                        ^~~~
a.cc:40:79: error: multidimensional array must have bounds for all dimensions except the first
   40 |   private Maze(int _width, int _height, int _px, int _py, int _length, Cell[][] _cells, boolean[][] _is_passed_through) {
      |                                                                               ^
a.cc:40:81: error: expected ',' or '...' before '_cells'
   40 |   private Maze(int _width, int _height, int _px, int _py, int _length, Cell[][] _cells, boolean[][] _is_passed_through) {
      |                                                                                 ^~~~~~
a.cc:50:10: error: expected ':' before 'Maze'
   50 |   private Maze dupuricate() {
      |          ^~~~~
      |          :
a.cc:60:9: error: expected ':' before 'void'
   60 |   public void setWallAround() {
      |         ^~~~~
      |         :
a.cc:71:9: error: expected ':' before 'void'
   71 |   public void setWall(int p, int q, int isWall) {
      |         ^~~~~
      |         :
a.cc:92:9: error: expected ':' before 'int'
   92 |   public int getLength() {
      |         ^~~~
      |         :
a.cc:96:10: error: expected ':' before 'Cell'
   96 |   private Cell cell(int x, int y) {
      |          ^~~~~
      |          :
a.cc:96:11: error: 'Cell' does not name a type
   96 |   private Cell cell(int x, int y) {
      |           ^~~~
a.cc:100:10: error: expected ':' before 'boolean'
  100 |   private boolean isPassedThrough(int x, int y) {
      |          ^~~~~~~~
      |          :
a.cc:100:11: error: 'boolean' does not name a type; did you mean 'bool'?
  100 |   private boolean isPassedThrough(int x, int y) {
      |           ^~~~~~~
      |           bool
a.cc:104:10: error: expected ':' before 'void'
  104 |   private void passThrough() {
      |          ^~~~~
      |          :
a.cc:108:9: error: expected ':' before 'Maze'
  108 |   public Maze moveTop() {
      |         ^~~~~
      |         :
a.cc:115:9: error: expected ':' before 'Maze'
  115 |   public Maze moveRight() {
      |         ^~~~~
      |         :
a.cc:122:9: error: expected ':' before 'Maze'
  122 |   public Maze moveBottom() {
      |         ^~~~~
      |         :
a.cc:129:9: error: expected ':' before 'Maze'
  129 |   public Maze moveLeft() {
      |         ^~~~~
      |         :
a.cc:137:9: error: expected ':' before 'boolean'
  137 |   public boolean ableToGoTop() {
      |         ^~~~~~~~
      |         :
a.cc:137:10: error: 'boolean' does not name a type; did you mean 'bool'?
  137 |   public boolean ableToGoTop() {
      |          ^~~~~~~
      |          bool
a.cc:140:9: error: expected ':' before 'boolean'
  140 |   public boolean ableToGoRight() {
      |         ^~~~~~~~
      |         :
a.cc:140:10: error: 'boolean' does not name a type; did you mean 'bool'?
  140 |   public boolean ableToGoRight() {
      |          ^~~~~~~
      |          bool
a.cc:143:9: error: expected ':' before 'boolean'
  143 |   public boolean ableToGoBottom() {
      |         ^~~~~~~~
      |         :
a.cc:143:10: error: 'boolean' does not name a type; did you mean 'bool'?
  143 |   public boolean ableToGoBottom() {
      |          ^~~~~~~
      |          bool
a.cc:146:9: error: expected ':' before 'boolean'
  146 |   public boolean ableToGoLeft() {
      |         ^~~~~~~~
      |         :
a.cc:146:10: error: 'boolean' does not name a type; did you mean 'bool'?
  146 |   public boolean ableToGoLeft() {
      |          ^~~~~~~
      |          bool
a.cc:150:9: error: expected ':' before 'boolean'
  150 |   public boolean isFinished() {
      |         ^~~~~~~~
      |         :
a.cc:150:10: error: 'boolean' does not name a type; did you mean 'bool'?
  150 |   public boolean isFinished() {
      |          ^~~~~~~
      |          bool
a.cc:154:9: error: expected ':' before 'String'
  154 |   public String debugString() {
      |         ^~~~~~~
      |         :
a.cc:154:10: error: 'String' does not name a type
  154 |   public String debugString() {
      |          ^~~~~~
a.cc:169:2: error: expected ';' after class definition
  169 | }
      |  ^
      |  ;
a.cc: In constructor 'Maze::Maze(int, int)':
a.cc:37:36: error: 'getInitialCells' was not declared in this scope
   37 |     this(_width, _height, 0, 0, 1, getInitialCells(_width, _height), getInitialIsPassedThrough(_width, _height));
      |                                    ^~~~~~~~~~~~~~~
a.cc:37:70: error: 'getInitialIsPassedThrough' was not declared in this scope
   37 |     this(_width, _height, 0, 0, 1, getInitialCells(_width, _height), getInitialIsPassedThrough(_width, _height));
      |                                                                      ^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:37:112: error: expression cannot be used as a function
   37 |     this(_width, _height, 0, 0, 1, getInitialCells(_width, _height), getInitialIsPassedThrough(_width, _height));
      |                                                                                                                ^
a.cc: In constructor 'Maze::Maze(...)':
a.cc:46:5: error: 'cells' was not declared in this scope
   46 |     cells  = _cells;
      |     ^~~~~
a.cc:46:14: error: '_cells' was not declared in this scope
   46 |     cells  = _cells;
      |              ^~~~~~
a.cc:47:5: error: 'is_passed_through' was not declared in this scope
   47 |     is_passed_through = _is_passed_through;
      |     ^~~~~~~~~~~~~~~~~
a.cc:47:25: error: '_is_passed_through' was not declared in this scope
   47 |     is_passed_through = _is_passed_through;
      |                         ^~~~~~~~~~~~~~~~~~
a.cc: In member function 'Maze Maze::dupuricate()':
a.cc:57:52: error: 'cells' was not declared in this scope
   57 |     return new Maze(width, height, px, py, length, cells, is_passed_through);
      |                                                    ^~~~~
a.cc:57:59: error: 'is_passed_through' was not declared in this scope
   57 |     return new Maze(width, height, px, py, length, cells, is_passed_through);
      |                                                           ^~~~~~~~~~~~~~~~~
a.cc: In member function 'void Maze::setWallAround()':
a.cc:63:31: error: 'cell' was not declared in this scope
   63 |         if(x == 0)          { cell(x, y).setLeft();   }
      |                               ^~~~
a.cc:64:31: error: 'cell' was not declared in this scope
   64 |         if(x == width - 1)  { cell(x, y).setRight();  }
      |                               ^~~~
a.cc:65:31: error: 'cell' was not declared in this scope
   65 |         if(y == 0)          { cell(x, y).setTop();    }
      |                               ^~~~
a.cc:66:31: error: 'cell' was not declared in this scope
   66 |         if(y == height - 1) { cell(x, y).setBottom(); }
      |                               ^~~~
a.cc: In member function 'void Maze::setWall(int, int, int)':
a.cc:81:7: error: 'cell' was not declared in this scope
   81 |       cell(x,     y).setRight();
      |       ^~~~
a.cc:87:7: error: 'cell' was not declared in this scope
   87 |       cell(x, y    ).setBottom();
      |       ^~~~
a.cc: In member function 'void Maze::passThrough()':
a.cc:105:5: error: 'is_passed_through' was not declared in this scope
  105 |     is_passed_through[py][px] = true;
      |     ^~~~~~~~~~~~~~~~~
a.cc: At global scope:
a.cc:172:10: error: expected ':' before 'boolean'
  172 |   private boolean top;
      |          ^~~~~~~~
      |          :
a.cc:172:11: error: 'boolean' does not name a type; did you mean 'bool'?
  172 |   private boolean top;
      |           ^~~~~~~
      |           bool
a.cc:173:10: error: expected ':' before 'boolean'
  173 |   private boolean right;
      |          ^~~~~~~~
      |          :
a.cc:173:11: error: 'boolean' does not name a type; did you mean 'bool'?
  173 |   private boolean r