Go: package main

import (
    "bufio"
    "fmt"
    "strconv"
    "os"
    "strings"
    "math"
)

var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func readLine() string {
    buf := make([]byte, 0, 1000000)
    for {
        l, p, e := rdr.ReadLine()
        if e != nil {
            panic(e)
        }
        buf = append(buf, l...)
        if !p {
            break
        }
    }
    return string(buf)
}

func toInt(str string) int {
    n, _ := strconv.Atoi(str)
    return n
}

func abs(a int) int {
    return int(math.Abs(float64(a)))
}

func main() {
    wh := strings.Split(readLine(), " ")
    w := toInt(wh[0])
    h := toInt(wh[1])
    readLine()
    pString := strings.Split(readLine(), " ")
    var x1 [300]int
    var y1 [300]int
    var x2 [300]int
    var y2 [300]int
    cnt := 0
    for i := 0; i <= w - 1; i++ {
        x1[cnt] = i
        y1[cnt] = 0
        cnt ++
    }
    for i := 1; i <= h - 1; i++ {
        x1[cnt] = w - 1
        y1[cnt] = i
        cnt ++
    }

    for i := w - 2;i >= 0; i-- {
        x1[cnt] = i
        y1[cnt] = h - 1
        cnt ++
    }
    cnt = 0
    for i := 0; i <= w - 2; i++ {
        x2[cnt] = i
        y2[cnt] = 1
        cnt ++
    }
    for i := 2; i <= h - 2; i++ {
        x2[cnt] = w - 2
        y2[cnt] = i
        cnt ++
    }

    for i := w - 3;i >= 0; i-- {
        x2[cnt] = i
        y2[cnt] = h - 2
        cnt ++
    }

    pos1 := 0
    pos2 := 0
    ans := 0
    for _, value := range pString {
        p := toInt(value)
        if p == 0 {
            pos1 ++
        } else {
            pos2 ++
        }
        px1, py1 := x1[pos1], y1[pos1]
        px2, py2 := x2[pos2], y2[pos2]
        if abs(px1 - px2) + abs(py1 - py2) == 1 {
            ans ++
        }
    }
    fmt.Println(ans)
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
	int w,h;	cin>>w>>h;
	int n;	cin>>n;
	vector<int> p(n);
	for(int i=0;i<n;i++)	cin>>p[i];
	int posa=0,posb=0;
	int cnt=0;
	for(int i=0;i<n;i++){
		if(p[i]==1)	posa++;
		else posb++;
		if(0<=posa&&posa<=w-2){
			if(posa==posb)	cnt++;
		}
		if(w-2<=posa&&posa<=w+h-5){
			if(posa+2==posb)	cnt++;
		}
		if(w+h-5<=posa&&posa<=w+w+h-7){
			if(posa+4==posb)	cnt++;
		}
	}
	cout<<cnt<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
 
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
 
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
 
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1,6);
uniform_real_distribution<double> score(0.0,10.0);
 
int main(void){
    int w, h; cin >> w >> h;
    int n; cin >> n;
    vi p(n);
    for(auto & e : p) cin >> e;

    int is = 0, os = 0, res = 0;
    for(auto & e : p){
        if(e == 0){
            os++;
        }
        else {
            is++;
        }

        if(os <= w - 2){
            if(is <= w - 2 and os == is)
                res++;
        }
        else if(os <= w + h - 3){
            if(w - 2 <= is and is <= w + h - 5 and os - 2 == is)
                res++;
        }
        else {
            if(w + h - 5 <= is and os - 4 == is){
                res++;
            }
        }
    }

    cout << res << endl;

    return 0;
}

C++: #include <iostream>
#include <vector>

int main()
{
    int W, H, N;
    std::cin >> W >> H >> N;
    std::vector<int> p(N);
    for (int i = 0; i < N; i++) {
        std::cin >> p[i];
    }
    
    int ux = 0, uy = 0, ud = 0;
    int rx = 0, ry = 1, rd = 0;


    constexpr int dx[] = {+1, +0, -1, +0};
    constexpr int dy[] = {+0, +1, +0, -1};
    
    int adj_count = 0;
    for (int i = 0; i < N; i++) {
        if (p[i] == 0) {
            ux += dx[ud];
            uy += dy[ud];
            if ((ud == 0 && ux == W - 1) ||
                (ud == 1 && uy == H - 1)) {
                ud++;
            } 
        } else {
            rx += dx[rd];
            ry += dy[rd];
            if ((rd == 0 && rx == W - 2) ||
                (rd == 1 && ry == H - 2)) {
                rd++;
            }
        }
        for (int j = 0; j < 4; j++) {
            int nx = ux + dx[j];
            int ny = uy + dy[j];
            if (nx == rx && ny == ry) {
                adj_count++;
                break;
            }
        }
    }
    
    std::cout << adj_count << std::endl;
    
    return 0;
}

C++: #include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

int main() {
	int W; int H;
	while(~scanf("%d%d", &W, &H)) {
		int N;
		scanf("%d", &N);
		int pos[2] = {};
		int ans = 0;
		rep(i, N) {
			int p;
			scanf("%d", &p);
			++ pos[p];
			if(pos[1] < W - 2)
				ans += pos[0] == pos[1];
			else if(pos[1] == W - 2)
				ans += pos[0] == pos[1] || pos[0] == pos[1] + 2;
			else if(pos[1] < W + H - 5)
				ans += pos[0] == pos[1] + 2;
			else if(pos[1] == W + H - 5)
				ans += pos[0] == pos[1] + 2 || pos[0] == pos[1] + 4;
			else
				ans += pos[0] == pos[1] + 4;
		}
		printf("%d\n", ans);
	}
	return 0;
}