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 <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};

int main() {

  int W, H; cin >> W >> H;
  int N; cin >> N;

  auto outer = make_tuple(0, 0, 0);
  auto inner = make_tuple(0, 1, 0);

  int ans = 0;

  rep(i, N) {

    auto walk = [](int& x, int& y, int& t, int W, int H){
      if(x == W-1) {
        if(y == H-1) {
          x --;
        }
        else {
          y ++;
        }
      }
      else if(!t) {
        x ++;
      }
      else {
        x --;
      }
    };

    auto next = [](int x, int y, int s, int t) {
      rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx == s && ny == t) { return true; }
      }
      return false;
    };

    int ox, oy, ot; tie(ox, oy, ot) = outer;
    int ix, iy, it; tie(ix, iy, it) = inner;

    int operation; cin >> operation;
    if(operation) {
      walk(ix, iy, it, W-1, H-1);
    }
    else {
      walk(ox, oy, ot, W, H);
    }

    if(next(ox, oy, ix, iy)) { ans ++; }

    outer = make_tuple(ox, oy, ot);
    inner = make_tuple(ix, iy, it);

  }

  cout << ans << endl;

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int w,h,n;
  while(cin >> w >> h >> n){
    int out=0,in=0,sum=0;
    int tmp;
    for(int i=0; i<n; ++i){ 
      if(out<=w-1){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in))++sum;
      }else if(out<=h+w-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in-2))++sum;
      }else if(out<2*w+h-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in-4))++sum;
      } 
    }
    cout << sum << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main()
{
  int W, H;
  int N;

  cin >> W >> H;
  cin >> N;
  int x = W * 2 + H - 2;
  int y = (W - 1) * 2 + H - 4;
  int ret = 0;

  while(N--) {
    int P;
    cin >> P;
    if(P == 0) --x;
    else --y;

    if(x < W) {
      if(x == y) ++ret;
    } else if(x == W) {
      
    } else if(x < W + H - 1) {
      if(x == y + 1) ++ret;
    } else if(x == W + H - 1) {
    
    } else {
      if(x == y + 4) ++ret;
    }
  }
  cout << ret << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int w,h,n;
    cin >>w >>h >>n;

    vector<int> p(n);
    rep(i,n) cin >>p[i];

    int ux=1, uy=1;
    int udx=1, udy=0;

    int sx=1, sy=2;
    int sdx=1, sdy=0;

    int ans = 0;
    rep(i,n){
        if(p[i]==0){
            ux += udx;
            uy += udy;

            if(ux==w){
                udx = 0;
                udy = 1;
                if(uy==h){
                    udx = -1;
                    udy = 0;
                }
            }
        }
        else{
            sx += sdx;
            sy += sdy;

            if(sx==w-1){
                sdx = 0;
                sdy = 1;
                if(sy==h-1){
                    sdx = -1;
                    sdy = 0;
                }
            }
        }

        if(ux<0 || sx<0) break;
        if(abs(ux-sx)+abs(uy-sy) == 1) ++ans;
    }
    cout << ans << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(){
	int w,h;cin >> w >> h;
	
	int i0=0,i1=0;
	
	int ans = 0;
	
	int n;cin >> n;
	for(int i=0;i<n;i++){
		int p;
		cin >> p;
		if(p){
			i1++;
		} else {
			i0++;
		}
		if(i0 == i1){
			ans++;
			continue;
		}
		if(i==w-2 || i==w+h-5){
			i0+=2;
		}
		if(i0 == i1){
			ans++;
			continue;
		}
	}
	cout << ans << endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int n,ans,W,H,in,pp1,pp2;

int main(){

    cin >> W >> H;
    cin  >> n;

    for(int i=0;i<n;i++){
        cin >> in;
        if(in==0)pp1++;
        else pp2++;
        if(pp1<=W-1&&pp2<=W-1){
            if(pp1==pp2)ans++;
        }else if(pp1>=W+1&&pp1<=W+H-2&&pp2>=W-1&&pp2<=W+H-4){
        if(pp1==pp2)ans++;
        }else if(pp2>=W+H-4&&pp1>=W+H){
        if(pp1==pp2)ans++;
        }
    }


    cout << ans << endl;

return 0;
}