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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	void set(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	int row,col;
};

int main(){

	int W,H;
	scanf("%d %d",&W,&H);

	Info outer,inner;

	outer.set(0,0);
	inner.set(1,0);

	int ans = 0;

	int N;
	scanf("%d",&N);

	int tmp;
	for(int i = 0; i < N; i++){
		scanf("%d",&tmp);

		if(tmp == 0){

			if(outer.row == 0 && outer.col < W-1){
				outer.col++;
			}else if(outer.col == W-1 && outer.row < H-1){
				outer.row++;
			}else{ // outer.row == H-1
				outer.col--;
			}

		}else{

			if(inner.row == 1 && inner.col < W-2){
				inner.col++;
			}else if(inner.col == W-2 && inner.row < H-2){
				inner.row++;
			}else{ //inner.row == H-2
				inner.col--;
			}
		}

		if(inner.col == outer.col && abs(inner.row-outer.row) == 1){
			ans++;
		}else if(inner.row == outer.row && outer.col == inner.col+1){
			ans++;
		}
	}

	printf("%d\n",ans);

	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define pb push_back
#define rep(i,n) for (int i = 0; i < n; ++i)
#define rrep(i,n) for (int i = 1; i <= n; ++i)
#define drep(i,n) for (int i = (n)-1; i >= 0; --i)
#define mins(x,y) x = min(x,y)
#define maxs(x,y) x = max(x,y)
using namespace std;
typedef vector<int> vi;

const int MX = 100005;
const int INF = 1001001001;

int w, h, n;
int ans;

void f(int a, int b) {
  if (a < w && b < w && a == b) ans++;
  a -= w; b -= w-2;
  if (a <= 0 || b <= 0) return;
  if (a <= h-2 && b <= h-2 && a == b) ans++;
  a -= h; b -= h-3;
  if (a <= 0 || b <= 0) return;
  if (a == b) ans++;
}

int main() {
  scanf("%d%d",&w,&h);
  scanf("%d",&n);
  int a = 1, b = 1;
  rep(i,n) {
    int p;
    scanf("%d",&p);
    if (p) b++; else a++;
    f(a,b);
  }
  cout<<ans<<endl;
  return 0;
}

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

using namespace std;

int main(){
    int h,w;
    cin >> w >> h;
    int n;
    cin >> n;
    int ans = 0;
    int fa, fb;
    int a = fa = h+2*w-2;
    int b = fb = h+2*w-6;
    for(int i = 0; i < n; i++){
        int p;
        cin >> p;
        if(p == 0) a--;
        else b--;
        if(a <= w-2 && b <= w-2){
            if(a == b) ans++;
        }
        if(w <= b && b <= w+h-5 && w+2 <= a && a <= w+h-3){
            if(a-2 == b) ans++;
        }
        if(w+h-3 <= b && w+h+1 <= a){
            if(fa-a == fb-b) ans++;
        }
    }
    cout << ans << endl;
}

C++: /*
 * b.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int dxs[] = {1, 0, -1}, dys[] = {0, -1, 0};

/* typedef */

/* global variables */

int xs[2], ys[2], dis[2], xmaxs[2], ymins[2];

/* subroutines */

/* main */

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

  xs[0] = 0, ys[0] = h - 1, dis[0] = 0;
  xs[1] = 0, ys[1] = h - 2, dis[1] = 0;

  xmaxs[0] = w - 1, xmaxs[1] = w - 2;
  ymins[0] = 0, ymins[1] = 1;

  int cnt = 0;
  
  for (int i = 0; i < n; i++) {
    int pi;
    cin >> pi;

    int x = xs[pi] + dxs[dis[pi]], y = ys[pi] + dys[dis[pi]];
    if (x > xmaxs[pi] || y < ymins[pi]) dis[pi]++;
    xs[pi] += dxs[dis[pi]], ys[pi] += dys[dis[pi]];

    if ((xs[0] == xs[1] && abs(ys[0] - ys[1]) == 1) ||
	(ys[0] == ys[1] && abs(xs[0] - xs[1]) == 1)) cnt++;
  }

  printf("%d\n", cnt);
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
#define Fi first
#define Se second
using namespace std;
static const LL INF = 1LL<<61LL;
typedef pair<int,int> PII;

PII U;
PII R;
int W,H;
int N;
int ans;
int main(){
    U.Fi=1;R.Fi=1;
    U.Se=1;R.Se=2;
    cin>>W>>H;
    cin>>N;
    rep(i,N){
        int a;
        cin>>a;
        if(a==0){
            if(U.Fi<W&&U.Se<H)U.Fi+=1;
            else if(U.Fi==W&&U.Se<H)U.Se+=1;
            else if(U.Se==H)U.Fi-=1;
        }
        else if(a==1){
            if(R.Fi<W-1&&U.Se<H-1)R.Fi+=1;
            else if(R.Fi==W-1&&R.Se<H-1)R.Se+=1;
            else if(R.Se==H-1)R.Fi-=1;
        }
        if(U.Fi==R.Fi||U.Se==R.Se)ans++;
    }
    cout<<ans<<endl;
}

C++: #include <iostream>

using namespace std;

int main() {
  int W, H;
  int N;
  int p;
  cin >> W >> H;
  cin >> N;
  int outer = 0;
  int inner = 0;
  int border = 0;
  int cnt = 0;
  for (int i = 0; i < N; ++i) {
    cin >> p;
    if (p == 0) {
      outer += 1;
    } else {
      inner += 1;
    }
    if (outer >= W * 2 + H - 6) {
      continue;
    }
    if (inner >= W * 2 + H - 6) {
      continue;
    }
    if (border == 0 && outer == W - 1) {
      border = 1;
      outer -= 2;
    }
    if (border == 1 && outer == W + H - 4) {
      border = 2;
      outer -= 2;
    }
    if (inner == outer && (border == 0 || border == 1 && inner >= W - 2 || border == 2 && inner >= W + H - 5)) {
      cnt += 1;
    }
  }
  cout << cnt << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;

int w,h,n,ans,A,B,a;

int main(){
  int w,h,n,ans=0,A=0,B=0;
  cin>>w>>h>>n;
  r(i,n){
    cin>>a;
    if(a)A++;
    else B++;
    if(B==w-1)B+=2;
    if(B==w-1+h-3+2)B+=2;
    if(A==B)ans++;
  }
  cout<<ans<<endl;
}

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

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