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>

using namespace std;

int main(){

  int w,h;
  int n;
  
  cin>>w>>h;
  cin>>n;

  vector<int> p(n);

  for(int i=0;i<n;++i)
	cin>>p[i];

  int in = 0;
  int out = 0;
  int cnt = 0;

  for(auto i:p){

	if(i == 1)
	  in++;
	else if(i == 0)
	  out++;

	if(out <= w-2 && out == in)
	  cnt++;
	
	else if(out >= w && out <= w+h-2 && out - in == 2)
	  cnt++;

	else if(out >= w+h && out - in == 4)
	  cnt++;
	
  }

  cout<<cnt<<endl;

}

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

using namespace std;

int main(){
    int h,w;
    cin >> w >> h;
    int n;
    cin >> n;
    int ans = 0;
    int a = h+2*w-2;
    int b = h+2*w-6;
    while(n--){
        int p;
        cin >> p;
        if(p == 0) a--;
        else b--;
        if(a <= w-1 && b <= w-1){
            if(a == b) ans++;
        }
        if(w-1 <= b && b <= w+h-4 && w+1 <= a && a <= w+h-2){
            if(a-2 == b) ans++;
        }
        if(w+h-2 <= b && w+h <= a){
            if(a-4 == b) ans++;
        }
    }
    cout << ans << endl;
}

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 - 1);
			}
			else res += (a == b - 2);
		}
		cout << res << endl;
	}
	return 0;
}

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

#define int long long

typedef long long ll;
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

int W,H;
int N;

vpint pr;

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

    rep(i,W-1)pr.pb(pint(i,i));
    rep(i,H-2)pr.pb(pint(W-2+i,W+i));
    rep(i,W)pr.pb(pint(W-2+H-3+i,W+H-1+i));


    int a=0,b=0;
    int cnt=0;
    rep(i,N){
        int p;cin>>p;
        if(p==0)b++;
        else a++;
        bool ok=false;
        rep(j,pr.size())ok|=pr[j].fi==a&&pr[j].se==b;
        cnt+=ok;
    }

    cout<<cnt<<endl;
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define INF 999999999
#define X second
#define Y first

int w,h;

void move(pii &A,pii &B,int p){
    if(p==0){
        if(A.Y==h-1 && A.X==0) A = pii(-1,-1);
        else if(A.Y==h-1)A.X--;
        else if(A.X==w-1)A.Y++;
        else A.X++;
    }else{
        if(B.Y==h-2 && B.X==0) B = pii(-1,-1);
        else if( B.Y==h-2 )B.X--;
        else if(B.X==w-2)B.Y++;
        else B.X++;
    }
}


bool is(pii A,pii B){
    int dx[4]={0,1,0,-1};
    int dy[4]={1,0,-1,0};
    rep(i,4){
        if( A.X+dx[i]==B.X &&
            A.Y+dy[i]==B.Y){
                return true;
            }
    }
    return false;
}


int main(){
    cin>>w>>h;
    int n;
    cin>>n;
    
    pii A,B;
    A = pii(0,0);
    B = pii(1,0);
    
    int c=0;
    rep(i,n){
        int p;
        cin>>p;
        move(A,B,p);
        if(is(A,B))c++;
    }
    cout<<c<<endl;
}

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

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

using P = pair<int, int>;

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

P next(P p, int w, int h){
  if(p.second == h - 1){
    --p.first;
  }
  else if(p.first == w - 1){
    ++p.second;
  }
  else{
    ++p.first;
  }
  return p;
}

bool adjacent(P p, P q){
  rep(i, 4){
    if(p.first + dx[i] == q.first && p.second + dy[i] == q.second){
      return true;
    }
  }
  return false;
}

int w, h, n;

int main(){
  cin >> w >> h >> n;
  
  P p(0, 0), q(0, 0);
  int ans = 0;
  rep(i, n){
    int t;
    cin >> t;
    if(!t){
      p = next(p, w, h);
    }
    else{
      q = next(q, w - 1, h - 2);
    }
    if(adjacent(p, P(q.first, q.second + 1))){
      ++ans;
    }
  }
  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;
    while(--n){
      int tmp;
      if(cin >> tmp,tmp){+in;}else{++out;}
      if(out<=w-2 && !(out-in))++sum;
      else if(out<=h+w-3 && out>=w && !(out-in-2))++sum;
      else if(out >= w+1 && !(out-in-4))++sum;
    }
    cout << sum << 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)B+=2;
    if(A==B)ans++;
  }
  cout<<ans<<endl;
}