Language: C++

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


class Maze {
  private static int n = 0;

  private int id;
  private int from_id;
  public Maze from;
  private int width;
  private int height;
  // TODO
  public int px;
  public int py;
  private int length;
  private Cell[][] cells;

  Maze(int _width, int _height) {
    id = ++n;
    from_id = 0;
    from   = null;
    width  = _width;
    height = _height;
    px     = 0;
    py     = 0;
    length = 1;

    cells = new Cell[height][width];
    for(int y = 0; y < height; y++) {
      for(int x = 0; x < width; x++) {
        cells[y][x] = new Cell();
      }
    }
    cell(0, 0).passThrough();
  }

  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 Maze dupuricate() {
    Maze dupuricated = new Maze(width, height);
    dupuricated.from_id = id;
    dupuricated.from = this;
    for(int y = 0; y < height; y++) {
      for(int x = 0; x < width; x++) {
        dupuricated.cells[y][x] = cells[y][x].dupuricate();
      }
    }
    return dupuricated;
  }

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

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

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

  public String debugString() {
    String debug = "";
    debug += "id = " + id + ", ";
    debug += "from_id = " + from_id + ", ";
    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;
  private boolean is_passed_through;

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

  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 void passThrough() { is_passed_through = true; }
  public boolean isPassedThrough() { return is_passed_through; }

  public Cell dupuricate() {
    Cell dupuricated = new Cell();
    if(!ableToGoTop())     dupuricated .setTop();
    if(!ableToGoRight())   dupuricated .setRight();
    if(!ableToGoBottom())  dupuricated .setBottom();
    if(!ableToGoLeft())    dupuricated .setLeft();
    if( isPassedThrough()) dupuricated .passThrough();
    return dupuricated;
  }

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

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
      printDebug("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;
          }
          printDebug(i + ", " + j);
          maze.setWall(i, j, sc.nextInt());
        }
      }
      printDebug(maze.debugString());
      printDebug("");

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

        if(current.isFinished()) {
          tail = current;

          printDebug("reverse search.");
          while(tail != null) {
            // printDebug(tail.debugString());
            printDebug(tail.px + " ," + tail.py);
            tail = tail.from;
          }
          printDebug("");

          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);
    }
  }

  static void printDebug(String message) {
    boolean debug = false;
    if(debug) {
      System.out.println(message);
    }
  }
}

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 'static'
    7 |   private static int n = 0;
      |          ^~~~~~~
      |          :
a.cc:7:22: error: ISO C++ forbids in-class initialization of non-const static member 'Maze::n'
    7 |   private static int n = 0;
      |                      ^
a.cc:9:10: error: expected ':' before 'int'
    9 |   private int id;
      |          ^~~~
      |          :
a.cc:10:10: error: expected ':' before 'int'
   10 |   private int from_id;
      |          ^~~~
      |          :
a.cc:11:9: error: expected ':' before 'Maze'
   11 |   public Maze from;
      |         ^~~~~
      |         :
a.cc:11:15: error: field 'from' has incomplete type 'Maze'
   11 |   public Maze from;
      |               ^~~~
a.cc:6:7: note: definition of 'class Maze' is not complete until the closing brace
    6 | class Maze {
      |       ^~~~
a.cc:12:10: error: expected ':' before 'int'
   12 |   private int width;
      |          ^~~~
      |          :
a.cc:13:10: error: expected ':' before 'int'
   13 |   private int height;
      |          ^~~~
      |          :
a.cc:15:9: error: expected ':' before 'int'
   15 |   public int px;
      |         ^~~~
      |         :
a.cc:16:9: error: expected ':' before 'int'
   16 |   public int py;
      |         ^~~~
      |         :
a.cc:17:10: error: expected ':' before 'int'
   17 |   private int length;
      |          ^~~~
      |          :
a.cc:18:10: error: expected ':' before 'Cell'
   18 |   private Cell[][] cells;
      |          ^~~~~
      |          :
a.cc:18:11: error: 'Cell' does not name a type
   18 |   private Cell[][] cells;
      |           ^~~~
a.cc:39:9: error: expected ':' before 'void'
   39 |   public void setWallAround() {
      |         ^~~~~
      |         :
a.cc:50:9: error: expected ':' before 'void'
   50 |   public void setWall(int p, int q, int isWall) {
      |         ^~~~~
      |         :
a.cc:71:9: error: expected ':' before 'int'
   71 |   public int getLength() {
      |         ^~~~
      |         :
a.cc:75:10: error: expected ':' before 'Cell'
   75 |   private Cell cell(int x, int y) {
      |          ^~~~~
      |          :
a.cc:75:11: error: 'Cell' does not name a type
   75 |   private Cell cell(int x, int y) {
      |           ^~~~
a.cc:79:10: error: expected ':' before 'Maze'
   79 |   private Maze dupuricate() {
      |          ^~~~~
      |          :
a.cc:91:9: error: expected ':' before 'Maze'
   91 |   public Maze moveTop()    { Maze dupuricated = dupuricate(); dupuricated.px = px;     dupuricated.py = py - 1; dupuricated.length = length + 1; dupuricated.cell(dupuricated.px, dupuricated.py).passThrough(); return dupuricated; }
      |         ^~~~~
      |         :
a.cc:92:9: error: expected ':' before 'Maze'
   92 |   public Maze moveRight()  { Maze dupuricated = dupuricate(); dupuricated.px = px + 1; dupuricated.py = py;     dupuricated.length = length + 1; dupuricated.cell(dupuricated.px, dupuricated.py).passThrough(); return dupuricated; }
      |         ^~~~~
      |         :
a.cc:93:9: error: expected ':' before 'Maze'
   93 |   public Maze moveBottom() { Maze dupuricated = dupuricate(); dupuricated.px = px;     dupuricated.py = py + 1; dupuricated.length = length + 1; dupuricated.cell(dupuricated.px, dupuricated.py).passThrough(); return dupuricated; }
      |         ^~~~~
      |         :
a.cc:94:9: error: expected ':' before 'Maze'
   94 |   public Maze moveLeft()   { Maze dupuricated = dupuricate(); dupuricated.px = px - 1; dupuricated.py = py;     dupuricated.length = length + 1; dupuricated.cell(dupuricated.px, dupuricated.py).passThrough(); return dupuricated; }
      |         ^~~~~
      |         :
a.cc:96:9: error: expected ':' before 'boolean'
   96 |   public boolean ableToGoTop()    { return cell(px, py).ableToGoTop()    && !cell(px,     py - 1).isPassedThrough(); }
      |         ^~~~~~~~
      |         :
a.cc:96:10: error: 'boolean' does not name a type; did you mean 'bool'?
   96 |   public boolean ableToGoTop()    { return cell(px, py).ableToGoTop()    && !cell(px,     py - 1).isPassedThrough(); }
      |          ^~~~~~~
      |          bool
a.cc:97:9: error: expected ':' before 'boolean'
   97 |   public boolean ableToGoRight()  { return cell(px, py).ableToGoRight()  && !cell(px + 1, py    ).isPassedThrough(); }
      |         ^~~~~~~~
      |         :
a.cc:97:10: error: 'boolean' does not name a type; did you mean 'bool'?
   97 |   public boolean ableToGoRight()  { return cell(px, py).ableToGoRight()  && !cell(px + 1, py    ).isPassedThrough(); }
      |          ^~~~~~~
      |          bool
a.cc:98:9: error: expected ':' before 'boolean'
   98 |   public boolean ableToGoBottom() { return cell(px, py).ableToGoBottom() && !cell(px,     py + 1).isPassedThrough(); }
      |         ^~~~~~~~
      |         :
a.cc:98:10: error: 'boolean' does not name a type; did you mean 'bool'?
   98 |   public boolean ableToGoBottom() { return cell(px, py).ableToGoBottom() && !cell(px,     py + 1).isPassedThrough(); }
      |          ^~~~~~~
      |          bool
a.cc:99:9: error: expected ':' before 'boolean'
   99 |   public boolean ableToGoLeft()   { return cell(px, py).ableToGoLeft()   && !cell(px - 1, py    ).isPassedThrough(); }
      |         ^~~~~~~~
      |         :
a.cc:99:10: error: 'boolean' does not name a type; did you mean 'bool'?
   99 |   public boolean ableToGoLeft()   { return cell(px, py).ableToGoLeft()   && !cell(px - 1, py    ).isPassedThrough(); }
      |          ^~~~~~~
      |          bool
a.cc:101:9: error: expected ':' before 'boolean'
  101 |   public boolean isFinished() {
      |         ^~~~~~~~
      |         :
a.cc:101:10: error: 'boolean' does not name a type; did you mean 'bool'?
  101 |   public boolean isFinished() {
      |          ^~~~~~~
      |          bool
a.cc:105:9: error: expected ':' before 'String'
  105 |   public String debugString() {
      |         ^~~~~~~
      |         :
a.cc:105:10: error: 'String' does not name a type
  105 |   public String debugString() {
      |          ^~~~~~
a.cc:122:2: error: expected ';' after class definition
  122 | }
      |  ^
      |  ;
a.cc: In constructor 'Maze::Maze(int, int)':
a.cc:23:14: error: 'null' was not declared in this scope
   23 |     from   = null;
      |              ^~~~
a.cc:30:5: error: 'cells' was not declared in this scope
   30 |     cells = new Cell[height][width];
      |     ^~~~~
a.cc:30:17: error: 'Cell' does not name a type
   30 |     cells = new Cell[height][width];
      |                 ^~~~
a.cc:33:27: error: expected type-specifier before 'Cell'
   33 |         cells[y][x] = new Cell();
      |                           ^~~~
a.cc:36:5: error: 'cell' was not declared in this scope
   36 |     cell(0, 0).passThrough();
      |     ^~~~
a.cc: In member function 'void Maze::setWallAround()':
a.cc:42:31: error: 'cell' was not declared in this scope
   42 |         if(x == 0)          { cell(x, y).setLeft();   }
      |                               ^~~~
a.cc:43:31: error: 'cell' was not declared in this scope
   43 |         if(x == width - 1)  { cell(x, y).setRight();  }
      |                               ^~~~
a.cc:44:31: error: 'cell' was not declared in this scope
   44 |         if(y == 0)          { cell(x, y).setTop();    }
      |                               ^~~~
a.cc:45:31: error: 'cell' was not declared in this scope
   45 |         if(y == height - 1) { cell(x, y).setBottom(); }
      |                               ^~~~
a.cc: In member function 'void Maze::setWall(int, int, int)':
a.cc:60:7: error: 'cell' was not declared in this scope
   60 |       cell(x,     y).setRight();
      |       ^~~~
a.cc:66:7: error: 'cell' was not declared in this scope
   66 |       cell(x, y    ).setBottom();
      |       ^~~~
a.cc: In member function 'Maze Maze::dupuricate()':
a.cc:80:24: error: conversion from 'Maze*' to non-scalar type 'Maze' requested
   80 |     Maze dupuricated = new Maze(width, height);
      |                        ^~~~~~~~~~~~~~~~~~~~~~~
a.cc:85:21: error: 'class Maze' has no member named 'cells'
   85 |         dupuricated.cells[y][x] = cells[y][x].dupuricate();
      |                     ^~~~~
a.cc:85:35: error: 'cells' was not declared in this scope
   85 |         dupuricated.cells[y][x] = cells[y][x].dupuricate();
      |                                   ^~~~~
a.cc: In member function 'Maze Maze::moveTop()':
a.cc:91:158: error: 'class Maze' has no member named 'cell'
   91 |   public Maze moveTop()    { Maze dupuricated = dupuricate(); dupuricated.px = px;     dupuricated.py = py - 1; dupuricated.length = length + 1; dupuricated.cell(dupuricated.px, dupuricated.py).passThrough(); return dupuricated; }
      |                                                                                                                                                              ^~~~
a.cc: In member function 'Maze Maze::moveRight()':
a.cc:92:158: error: 'class Maze' has no member named 'cell'
   92 |   public Maze moveRight()  { Maze dupuricated = dupuricate(); dupuricated.px = px + 1; dupuricated.py = py;     dupuricated.length = length + 1; dupuricated.cell(dupuricated.px, dupuricated.py).passThrough(); return dupuricated; }
      |                                                                                                                                                              ^~~~
a.cc: In member function 'Maze Maze::moveBottom()':
a.cc:93:158: error: 'class Maze' has no member named 'cell'
   93 |   public Maze moveBottom() { Maze dupuricated = dupuricate(); dupuricated.px = px;     dupuricated.py = py + 1; dupuricated.length = length + 1; dupuricated.cell(dupuricated.px,