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;
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 && A.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<bits/stdc++.h>
#define LLI long long int

using namespace std;

int n,ans,x,y,W,H,p;


int main(){

    cin  >> W >> H >> n;

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

        if(p==0)x++;
        else y++;

        if(x<W&&y<=W-1){
            if(x==y)ans++;
        }else if(W<x&&x<W+H-1&&W-1<=y&&y<=W+H-4){
            if(x==y-2)ans++;
        }else if(W+H-1<x&&W+H-4<=y){
            if(x==y-4)ans++;
        }
    }



    cout << ans << 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 == i_s && o_s != W - 1){
        ++ans;
      }
      if(o_s == W - 1){
        c = 1;
      }
    }

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

    else{
      if(i_s == W + H + W - 6 || o_s == W + H + W - 2){
        break;
      }
      if(o_s == i_s + 4){
        ++ans;
      }
    }
  }

  cout<<ans<<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-2)B+=2;
    if(B==w+h-3)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(i0 == i1){
			ans++;
			continue;
		}
		if(i==w-2 || i==w+h-5){
			i0+=2;
		}
		if(i0 == i1){
			ans++;
			continue;
		}
	}
	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 d[100][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 && abs(curAy - curBy) <= 1) {
			++cnt;
		}
		else if (curAy == curBy && curBx - curAx == 1 ) {
			++cnt;
		}
	}
	printf("%d\n", cnt);
	return 0;
}

C++: #include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int D[3][2]={{1,0},{0,1},{-1,0}};
int main(){
	int W,H,N,d,r=0;
	scanf("%d%d%d",&W,&H,&N);
	vector<vector<int>> v={{W-1,H-1,W-1},{W-2,H-3,W-2}}, p={{0,0},{0,1}}, c={{0,0},{0,0}};
	for(;N--;){
		scanf("%d",&d);
		p[d][0]+=D[c[d][0]][0];
		p[d][1]+=D[c[d][0]][1];
		c[d][1]++;
		if(c[d][1]==v[d][c[d][0]])c[d][0]++,c[d][1]=0;
		if(abs(p[0][0]-p[1][0])+abs(p[0][1]-p[1][1])<2)r++;
	}
	printf("%d\n",r);
}

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) {
        printf("uzu: (%d, %d)\n", uzu.first, uzu.second);
        printf("rin: (%d, %d)\n", rin.first, rin.second);
        if (p[i] == 0) {
            if (uzu.first < w && uzu.second == h) uzu.first++;
            else if (uzu.first == w && uzu.second > 0) 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 > 1) rin.second--;
            else rin.first--;
        }
        if ((uzu.first == rin.first) || (uzu.second == rin.second)) ans++;
    }
    cout << ans << endl;
    return 0;
}