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 <fstream>
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>

using namespace std;
#define ALL(c) (c).begin(), (c).end()
#define REP(i,n) for(ll i=0; i < (n); ++i)
using ll = long long;
using vl = vector<ll>;
int main(){
#ifdef _WIN32
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	cout << fixed << setprecision(8);
	ll w, h; cin >> w >> h;
	ll n; cin >> n;
	vl p(n); REP(i, n) cin >> p[i];

	ll a = (w - 1) * 2 + h;
	ll b = (w - 1) * 2 + h - 4;
	ll cnt = 0;
	REP(i, n){
		(p[i] == 0 ? a : b)--;
		if (0 < a && a <= w - 1){
			if (a == b) cnt++;
		}
		else if (w < a && a <= w + h - 2){
			if (a - 2 == b) cnt++;
		}
		else if (w + h - 1 < a && a <= w * 2 + h - 2){
			if (a - 4 == b) cnt++;
		}
	}

	cout << cnt << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

int main (int argc, char *argv[]) {
    int w, h, n;
    int ans = 0;
    cin >> w >> h;
    cin >> n;
    int p[n];
    for (int i = 0; i < n; ++i) {
        cin >> p[i];
    }

    pair<int, int> uzu = make_pair(1, h);   // out
    pair<int, int> rin = make_pair(1, h-1); // in
    for (int i = 0; i < n; ++i) {
        if (p[i] == 0) {
            if (uzu.first < w && uzu.second == h) uzu.first++;
            else if (uzu.first == w && uzu.second > 1) uzu.second--;
            else uzu.first--;
        }
        else {
            if (rin.first < w - 1 && rin.second == h - 1) rin.first++;
            else if (rin.first == w - 1 && rin.second > 2) rin.second--;
            else rin.first--;
        }
        // cout << "---" << endl;
        // printf("uzu: (%d, %d)\n", uzu.first, uzu.second);
        // printf("rin: (%d, %d)\n", rin.first, rin.second);
        if ((abs(uzu.first - rin.first) + abs(uzu.second - rin.second)) == 1) {
            ans++;
            // cout << "cnt" << endl;
        }
    }
    cout << ans << endl;
    return 0;
}

C++: #define _USE_MATH_DEFINES

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int W, H, N;
int data[100];

int main(){
	scanf("%d%d%d", &W, &H, &N);
	for (int i = 0; i < N; ++i){
		scanf("%d", &data[i]);
	}

	int cnt = 0;
	int curAx = 1, curAy = 1;
	int curBx = 1, curBy = 2;
	for (int i = 0; i < N; ++i) {
		if (data[i] == 0) {
			if (curAy == 1 && curAx != W)  {
				curAx++;
			}
			else if (curAx == W && curAy < H) {
				curAy++;
			}
			else if (curAy == H) {
				curAx--;
			}
		}
		else {
			if (curBy == 2 && curBx != W - 1) {
				curBx++;
			}
			else if (curBx == W - 1 && curBy < H - 1) {
				curBy++;
			}
			else if (curBy == H - 1) {
				curBx--;
			}
		}

		if (curAx == curBx && ((curAy == 1 && curBy == 2) || (curAy == H && curBy == H-1))) {
			++cnt;
		}
		else if (curAy == curBy && curAx - curBx == 1) {
			++cnt;
		}
	}
	printf("%d\n", cnt);
	return 0;
}

C++: #include<bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int main(){
	int w, h, n;
	cin >> w >> h >> n;
	int a = 0, b = 0, ans = 0;
	rep(i, n){
		int p; cin >> p;
		if(p == 0) a++;
		else b++;
		
		if(b < w - 1 && a == b ||
			w - 2 <= b && b <= w - 2 + h - 3 && a - w == b - w + 3 ||
			w - 2 + h - 3 <= b && b - (w - 2 + h - 3) == a - (w - 1) - h)
		ans++;
	}
	cout << ans << endl;
	return 0;
}

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

int H, W;
int a, b;
int main() {
	cin >> H >> W;
	int N; cin >> N;
	a = 0; b = 0;
	int ans = 0;
	while (N--) {
		int p; cin >> p;
		if (!p) {
			a++;
		}
		else {
			b++;
		}
		if (a == W - 1 || a == W + H - 2) {

		}
		else if (a < W-1) {
			if (a == b)ans++;
		}
		else if (a < W + H-2) {
			if (a == b + 2)ans++;
		}
		else {
			if (a == b + 4)ans++;
		}

	}
	cout << ans << endl;
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iomanip>


using namespace std;

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

	int a=1, b=1;
	int input;
	int count = 0;
	for(int i=0; i<N; i++) {
		cin >> input;
		if(input) b++;
		else a++;

		if(a < W) {
			if(a==b) count++;
		}
		else if(a < W+H-2) {
			if(a==b+2) count++;
		}
		else if(a > W+H-1) {
			if(a==b+4) count++;
		}
	}

	cout << count << 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;
    for(int i=0; i<n; ++i){
      int tmp;
      if(cin >> tmp,tmp){++out;}else{++in;}
      if(out<=w-2 && !abs(out-in))++sum;
      else if(out<=h+w-3 && out>=w && !abs(out-in-2))++sum;
      else if(out >= w+1 && !abs(out-in-4))++sum;
    }
    cout << sum << endl;
  }
  return 0;
}