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>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;
using ll=long long;

const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};

int w,h,n;
int p[110];

int main(void){
	cin >> w >> h >> n;
	rep(i,n) cin >> p[i];
	int cx[2]={0,0},cy[2]={0,1},mode[2]={1,1};
	
	int ans=0;
	rep(i,n){
		switch(mode[p[i]]){
			case 1:
				cx[p[i]]+=1;
				break;
			case 2:
				cy[p[i]]+=1;	
				break;
			case 3:
				cx[p[i]]-=1;
				break;
		}
		if(cx[p[i]]==w-1-p[i] && mode[p[i]]==1) mode[p[i]]++;
		if(cy[p[i]]==h-1-p[i] && mode[p[i]]==2) mode[p[i]]++;
		if(abs(cx[1]-cx[0])+abs(cy[1]-cy[0])==1) ans++;
	}
	cout << ans << endl;
	return 0;
}

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

int main()
{
    int W, H, N;
    std::cin >> W >> H >> N;
    std::vector<int> p(N);
    for (int i = 0; i < N; i++) {
        std::cin >> p[i];
    }
    
    int ux = 0, uy = 0, ud = 0;
    int rx = 0, ry = 1, rd = 0;

    constexpr int dx[] = {+1, +0, -1, +0};
    constexpr int dy[] = {+0, +1, +0, -1};
    
    int adj_count = 0;
    for (int i = 0; i < N; i++) {
        if (p[i] == 0) {
            ux += dx[ud];
            uy += dy[ud];
            if ((ud == 0 && ux == W - 1) ||
                (ud == 1 && uy == H - 1)) {
                ud++;
            } 
        } else {
            rx += dx[rd];
            ry += dy[rd];
            if ((rd == 0 && rx == W - 2) ||
                (rd == 1 && ry == H - 2)) {
                rd++;
            }
        }
        for (int j = 0; j < 4; j++) {
            int nx = ux + dx[j];
            int ny = uy + dy[j];
            if (nx == rx && ny == ry) {
                adj_count++;
                break;
            }
        }
    }
    
    std::cout << adj_count << std::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(!abs(out-in))++sum;
      }else if(out<=h+w-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!abs(out-in-2))++sum;
      }else if(out<2*w+h-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!abs(out-in-4))++sum;
      } 
    }
    cout << sum << endl;
  }
  return 0;
}

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

int main(){
  int w,h,n;
  int out=0,in=0,sum=0;
  cin >> w >> h >>n;
  int str[n];
  for(int i=0; i<n; ++i)cin >> str[i];
  for(auto i: str){
    if(i){++in;}else{++out;}
    if(out<=w-2 && out==in)++sum;
    else if(out>=w && out<= w+h-3 && out-in==2)++sum;
    else if(out >= w+h-1 && out-in==4)++sum;
  }
  cout << sum << endl;
}

C++: /*   _/                _/                 _/_/_/   _/
  _/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
   _/     _/    _/   _/     _/    _/  _/_/_/     _/
  _/     _/    _/   _/     _/    _/  _/    _/   _/
   _/_/   _/_/       _/_/   _/_/      _/_/     _/ */
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
using ll=long long;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=2e9;
const ll INF=1e18;
using P=pair<int,int>;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  int w,h,n,p,ans=0,out=1,in=1;
  cin >> w >> h >> n;
  for(int i=0; i<n; i++) {
    cin >> p;
    if(p) {
      in++;
    }
    else {
      out++;
    }
    if(out==w||out==w+h-1) {
      continue;
    }
    if(out<w) {
      if(out==in) {
        ans++;
      }
    }
    else if(out<w+h-1) {
      if(out-2==in) {
        ans++;
      }
    }
    else {
      if(out-4==in) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

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

int H, W;
int a, b;
int main() {
	cin >> W >> H;
	int N; cin >> N;
	a = 0; b = 0;
	int ans = 0;
	while (N--) {
		int p; cin >> p;
		if (!p) {
			a++;
		}
		else {
			b++;
		}
		if (a == W - 1 || a == W + H - 2) {

		}
		else if (a < W-1) {
			if (a == b)ans++;
		}
		else if (a < W + H-2) {
			if (a == b + 2)ans++;
		}
		else {
			if (a == b + 4)ans++;
		}

	}
	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-4 <= b && w+h <= a){
            if(fa-a == fb-b) ans++;
        }
    }
    cout << ans << endl;
}

C++: #include<bits/stdc++.h>
#define LLI long long int

using namespace std;

int n,ans,x=1,y=1,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;
}