Language: C++

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<vector>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;

#define MAX 30
#define INF 1000
int wn,hn;
int d[MAX][MAX];
bool w[MAX][MAX]; //w,h ?¨??£?
bool h[MAX][MAX]; //????£?
vector< pair<int, int> > nextlist; //x,y


void reset() {
	for (int i = 0; i < wn; i++) {
		for (int j = 0; j < hn; j++) {
			d[i][j] = INF;
		}
	}
}

bool sortRule(pair<int, int> left, pair<int, int> right) {
	return d[left.first][left.second] < d[right.first][right.second];
}


void maze(pair<int,int> pos) {
	nextlist.erase(nextlist.begin());
	//???
	if (pos.second > 0) {
		if (!w[pos.first][pos.second - 1] && d[pos.first][pos.second - 1] > d[pos.first][pos.second] + 1) {
			d[pos.first][pos.second - 1] = d[pos.first][pos.second] + 1;
			nextlist.push_back(pair<int, int>::pair(pos.first, pos.second - 1));
		}
	}

	//???
	if (pos.first > 0) {
		if (!h[pos.first - 1][pos.second] && d[pos.first - 1][pos.second] > d[pos.first][pos.second] + 1) {
			d[pos.first - 1][pos.second] = d[pos.first][pos.second] + 1;
			nextlist.push_back(pair<int, int>::pair(pos.first - 1, pos.second));
		}
	}

	//???
	if (pos.second < hn-1) {
		if (!w[pos.first][pos.second] && d[pos.first][pos.second + 1] > d[pos.first][pos.second] + 1) {
			d[pos.first][pos.second + 1] = d[pos.first][pos.second] + 1;
			nextlist.push_back(pair<int, int>::pair(pos.first, pos.second + 1));
		}
	}

	//???
	if (pos.first < wn -1) {
		if (!h[pos.first][pos.second] && d[pos.first + 1][pos.second] > d[pos.first][pos.second] + 1) {
			d[pos.first + 1][pos.second] = d[pos.first][pos.second] + 1;
			nextlist.push_back(pair<int, int>::pair(pos.first + 1, pos.second));
		}
	}

	if (nextlist.size() > 0) {
		sort(nextlist.begin(), nextlist.end(), sortRule);
		maze(nextlist[0]);

	}
}



int main() {
	cin >> wn >> hn;
	while (wn + hn) {
		reset();
		for (int i = 0; i < hn-1; i++) {
			for (int j = 0; j < wn-1; j++) {
				cin >> h[j][i];
			}
			for (int j = 0; j < wn; j++) {
				cin >> w[j][i];
			}
		}
		for (int j = 0; j < wn - 1; j++) {
			cin >> h[j][hn - 1];
		}

		d[0][0] = 1;
		nextlist.push_back(pair<int, int>::pair(0, 0));
		maze(nextlist[0]);

		if (d[wn - 1][hn - 1] == INF) {
			cout << 0 << endl;
		}
		else {
			cout << d[wn - 1][hn - 1] << endl;
		}

		cin >> wn >> hn;
	}
	return 0;
}

Compiler output:
a.cc: In function 'void maze(std::pair<int, int>)':
a.cc:39:64: error: cannot call constructor 'std::pair<int, int>::pair' directly [-fpermissive]
   39 |                         nextlist.push_back(pair<int, int>::pair(pos.first, pos.second - 1));
      |                                            ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:39:64: note: for a function-style cast, remove the redundant '::pair'
a.cc:47:64: error: cannot call constructor 'std::pair<int, int>::pair' directly [-fpermissive]
   47 |                         nextlist.push_back(pair<int, int>::pair(pos.first - 1, pos.second));
      |                                            ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:47:64: note: for a function-style cast, remove the redundant '::pair'
a.cc:55:64: error: cannot call constructor 'std::pair<int, int>::pair' directly [-fpermissive]
   55 |                         nextlist.push_back(pair<int, int>::pair(pos.first, pos.second + 1));
      |                                            ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:55:64: note: for a function-style cast, remove the redundant '::pair'
a.cc:63:64: error: cannot call constructor 'std::pair<int, int>::pair' directly [-fpermissive]
   63 |                         nextlist.push_back(pair<int, int>::pair(pos.first + 1, pos.second));
      |                                            ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:63:64: note: for a function-style cast, remove the redundant '::pair'
a.cc: In function 'int main()':
a.cc:93:56: error: cannot call constructor 'std::pair<int, int>::pair' directly [-fpermissive]
   93 |                 nextlist.push_back(pair<int, int>::pair(0, 0));
      |                                    ~~~~~~~~~~~~~~~~~~~~^~~~~~
a.cc:93:56: note: for a function-style cast, remove the redundant '::pair'