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,N;
  cin>>W;
  cin>>H;
  cin>>N;
  
  vector<int> row;
  for(int i = 0;i < N;++i){
    int a;
    cin>>a;
    row.push_back(a);
  }

  int o_s = W + H + W - 2;
  int o = W + H + W - 2;
  int i_s = W + H + W - 6;
  int c = 0;
  int ans = 0;

  for(int e : row){
    if(e == 0){
      --o_s;
    }
    else{
      --i_s;
    }

    if(o_s == 0 || i_s == 0){
      break;
    }
    else if(o - (W - 1) < o_s){
      if(o_s == i_s + 4){
        ++ans;
      }
    }
    else if(o - (W - 1) == o_s){
      continue;
    }
    else if(o - (W - 1) > o_s && o_s > o - (W + H - 2)){
      if(o_s == i_s + 2){
        ++ans;
      }
    }
    else if(o_s = o - (W + H - 2){
      continue;
    }
    else if(o_s < o - (W + H - 2)){
      if(o_s == i_s){
        ++ans;
      }
    }
  }

  cout<<ans<<endl;

  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&&R.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+1==R.Se)||(U.Se==R.Se&&U.Fi==R.Fi+1))ans++;
        else if((R.Fi==U.Fi&&R.Se+1==U.Se)||(R.Se==U.Se&&U.Fi==R.Fi+1))ans++;
        //cout<<U.Fi<<" x "<<R.Fi<<endl;
        //cout<<U.Se<<" y "<<R.Se<<endl;
    }
    cout<<ans<<endl;
}

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++: #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-1 && !abs(out-in))++sum;
      else if(out<=h+w-2 && in>=w-1 && !abs(out-in-2))++sum;
      else if(out<2*w+h-2 && in>= h+w-4 && !abs(out-in-4))++sum;
    }
    cout << sum << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <utility>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int,int> pii;


int main(){
	int w,h,n;
	cin>>w>>h>>n;
	vi p(n);
	rep(i,0,n) cin>>p[i];
	int x1=0,y1=0,x2=0,y2=1,ans=0;
	int dx[3]={0,-1,0},dy[3]={1,0,-1};
	rep(i,0,n){
		if(p[i]==0){
			if(x1<w-1 && y1==0) x1++;
			else if(x1==w-1 && y1<h-1) y1++;
			else x1--;
		}else{
			if(x2<w-2 && y2==1) x2++;
			else if(x2==w-2 && y2<h-2) y2++;
			else x2--;
		}
		bool f=0;
		rep(j,0,3){
			if(x1+dx[j]==x2 && y1+dy[j]==y2){
				f=1; break;
			}
		}
		if(f) ans++;
	}
	o(ans);
}

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(W < a && a < W+H-1) {
			if(a==b+2) count++;
		}
		else if(W+H-1 < a) {
			if(a==b+4) count++;
		}
	}

	cout << count << endl;

	return 0;
}

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

using namespace std;

int main(){
  int W,H,N;
  cin>>W;
  cin>>H;
  cin>>N;
  
  vector<int> row;
  for(int i = 0;i < N;++i){
    int a;
    cin>>a;
    row.push_back(a);
  }

  int o_s = 0;
  int i_s = 0;
  int c = 0;
  int ans = 0;

  for(int e : row){
    if(e == 0){
      ++o_s;
    }
    else{
      ++i_s;
    }

    if(c == 0){
      if(o_s == W - 1){
        c = 1;
      }
      if(o_s == i_s && o_s != W - 1){
        ++ans;
      }
    }
    else if(c == 1){
      if(o_s == W + H - 2){
        c = 2;
      }
      if(o_s == i_s + 2 && o_s != W + H - 2){
        ++ans;
      }
    }
    else{
      if(i_s == W + H + W - 5 || o_s == W + H + W - 2){
        break;
      }
      if(o_s == i_s + 4){
        ++ans;
      }
    }
  }

  cout<<ans<<endl;

  return 0;
}

C++: #include <iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int W,H;
int N;
int p[102];
int pos[2];
int ans;
int main() {
    cin>>W>>H;
    cin>>N;
    rep(i,N) cin>>p[i];
    rep(i,N) {
        pos[p[i]]++;
        if(pos[0]<W) {
            ans+=pos[0]==pos[1];
        }
        else if(pos[0]==W-1) {
            // pass
        }
        else if(pos[0]<W+H-2) {
            ans+=pos[0]==pos[1]+2;
        }
        else if(pos[0]==W+H-2) {
            // pass
        }
        else {
            ans+=pos[0]==pos[1]+4;
        }
        //cout<<i<<": "<<pos[0]<<", "<<pos[1]<<" -> "<<ans<<endl;
    }
    cout<<ans<<endl;
    return 0;
}