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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int W, H, N;

int mp[100][100];
pii uzuki, rin;

void move_uzuki()
{
    mp[uzuki.fst][uzuki.scd] = 0;
    if (uzuki.fst == 0 && uzuki.scd < W - 1) uzuki = pii(uzuki.fst, uzuki.scd + 1);
    else if (uzuki.scd == W - 1 && uzuki.fst < H - 1) uzuki = pii(uzuki.fst + 1, uzuki.scd);
    else if (uzuki.fst == H - 1) uzuki = pii(uzuki.fst, uzuki.scd - 1);
    mp[uzuki.fst][uzuki.scd] = 1;
}

void move_rin()
{
    mp[rin.fst][rin.scd] = 0;
    if (rin.fst == 1 && rin.scd < W - 2) rin = pii(rin.fst, rin.scd + 1);
    else if (rin.scd == W - 2 && rin.fst < H - 2) rin = pii(rin.fst + 1, rin.scd);
    else if (rin.fst == H - 2) rin = pii(rin.fst, rin.scd - 1);
    mp[rin.fst][rin.scd] = 1;
}

bool check(int y, int x)
{
    int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, -1, 0, 1};

    rep(i, 4) {
        int ny = y + dy[i], nx = x + dx[i];
        if (0 <= ny && ny < H && 0 <= nx && nx < W) {
            if (mp[y][x] && mp[ny][nx]) return true;
        }
    }

    return false;
}

signed main()
{
    omajinai;
    cin >> W >> H;

    mp[0][0] = 1;
    mp[1][0] = 1;

    uzuki = pii(0, 0);
    rin = pii(1, 0);

    int ans = 0;

    cin >> N;

    rep(i, N) {
        int p; cin >> p;
        if (p == 0) {
            move_uzuki();
        } else {
            move_rin();
        }
        if (check(uzuki.fst, uzuki.scd)) ans++;
    }

    cout << ans << endl;
}

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(i1==w-2 || i1==w+h-5){
			i0+=2;
		}
		if(i0 == i1){
			ans++;
			continue;
		}
	}
	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-1){
        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++: #define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include<vector>
#include<algorithm>
using namespace std;

struct init{
	init(){
		cin.tie(0); ios::sync_with_stdio(false);
	}
}________init;
int main() {

#ifdef INPUT_FROM_FILE
	ifstream cin("sample.in");
	ofstream cout("sample.out");
#endif
	int W, H,N;
	while (cin >> W >> H >> N){
		int  res = 0;
		int a = 1, b = 1;
		while (N--){
			int p;
			cin >> p;
			if (p == 0)a++;
			else b++;
			if (a == W || a == W + H - 1)continue;
			if (a < W){
				res += (a == b);
			}
			else if (a < W + H - 1){
				res += (a == b + 2);
			}
			else res += (a == b +4);
		}
		cout << res << 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-6 && w+2 <= a && a <= w+h-3){
            if(a-2 == b) ans++;
        }
        if(w+h-4 <= b && w+h <= a){
            if(fa-a == fb-b) ans++;
        }
    }
    cout << ans << endl;
}

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

typedef complex<double> P;


P f(int W,int H,int t){
	if( t <= W-1 ) return P(t,0);
	else if( t <= W + H - 2 ) return P(W-1,0) + P(0,t-(W-1));
	else return P(W-1 - ( t - (W + H - 2) ),H-1);
}


int main(){
	int W,H;
	cin >> W >> H;
	int t[2] = {};
	int N;
	cin >> N;
	int ans = 0;
	for(int i = 0 ; i < N ; i++){
		int x;
		cin >> x;
		t[x]++;
		P A = f(W,H,t[0]);
		P B = f(W-1,H-2,t[1]) + P(0,1);
		//cout << A << " " << B << endl;
		if( fabs(abs(A-B)-1) < 1e-9 )	ans++;
	}
	cout << ans << endl;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(void){
	int W,H;
	cin>>W>>H;
	int n;
	cin>>n;

	int in = 0 , out = 0 , c = 0;
	for(int i = 0 ; i < n ; i ++){
		int inp;
		cin>>inp;
		if(inp){
			in ++;
		}else{
			out ++;
		}
		if(out < W){
			if(in == out) c ++;
		}else if(out < W + H - 1){
			if(in == out - 2) c ++;
		}else{
			if(in == out - 4) c ++;
		}
	}
	cout<<c<<endl;
}