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;	cin>>w>>h;
	int n;	cin>>n;
	vector<int> p(n);
	for(int i=0;i<n;i++)	cin>>p[i];
	int posa=0,posb=0;
	int cnt=0;
	for(int i=0;i<n;i++){
		if(p[i]==1)	posa++;
		else posb++;
		if(0<=posa&&posa<=w-1){
			if(posa==posb)	cnt++;
		}
		if(w-1<=posa&&posa<=w+h-4){
			if(posa+2==posb)	cnt++;
		}
		if(w+h-4<=posa&&posa<=w+w+h-6){
			if(posa+4==posb)	cnt++;
		}
	}
	cout<<cnt<<endl;
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define pb push_back
#define rep(i,n) for (int i = 0; i < n; ++i)
#define rrep(i,n) for (int i = 1; i <= n; ++i)
#define drep(i,n) for (int i = (n)-1; i >= 0; --i)
#define mins(x,y) x = min(x,y)
#define maxs(x,y) x = max(x,y)
using namespace std;
typedef vector<int> vi;

const int MX = 100005;
const int INF = 1001001001;

int w, h, n;
int ans;

void f(int a, int b) {
  if (a < w && b < w && a == b) ans++;
  a -= w; b -= w-2;
  if (a <= 0 || b <= 0) return;
  if (a <= h-2 && b <= h-2 && a == b) ans++;
  a -= h-1; b -= h-3;
  if (a <= 0 || b <= 0) return;
  if (a == b) ans++;
}

int main() {
  scanf("%d%d",&w,&h);
  scanf("%d",&n);
  int a = 1, b = 1;
  rep(i,n) {
    int p;
    scanf("%d",&p);
    if (p) b++; else a++;
    f(a,b);
  }
  cout<<ans<<endl;
  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,H,W},{W-1,H-2,W-1}}, 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];
		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 <vector>
#include <cassert>

using namespace std;

int w, h, n;

bool adj(int a, int b) {
    if (0 <= a && a <= w - 2 &&
        0 <= b && b <= w - 2) {
        return a == b;
    }

    if (w <= a && a <= w + h - 3 &&
        w - 2 <= b && b <= w - 2 + h - 3) {
        return a - w == b - (w - 2);
    }

    if (w + h - 1 <= a &&
        w - 2 + h - 3 <= b) {
        return a - (w + h - 1) == b - (w - 2 + h - 3);
    }
    return false;
}

int main() {
    while (cin >> w >> h >> n) {
        int a = 0, b = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int p;
            cin >> p;
            if (p) b++; else a++;
            ans += adj(a, b);
        }
        cout << ans << endl;
    }
}

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 (uzuki.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;
    cin >> N;

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

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

    int ans = 0;

    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 <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,H,W},{W-1,H-2,W-1}}, 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];
		if(++c[d][1]==v[d][c[d][1]])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);
}