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 <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

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

int main() {

  int W, H; cin >> W >> H;
  int N; cin >> N;

  auto outer = make_tuple(0, 0, 0);
  auto inner = make_tuple(0, 1, 0);

  int ans = 0;

  rep(i, N) {

    auto walk = [](int& x, int& y, int& t, int W, int H){
      if(x == W-1) {
        if(y == H-1) {
          x --;
          t = 1;
        }
        else {
          y ++;
        }
      }
      else if(!t) {
        x ++;
      }
      else {
        x --;
      }
    };

    auto next = [](int x, int y, int s, int t) {
      rep(i, 4) {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx == s && ny == t) { return true; }
      }
      return false;
    };

    int ox, oy, ot; tie(ox, oy, ot) = outer;
    int ix, iy, it; tie(ix, iy, it) = inner;

    int operation; cin >> operation;
    if(operation) {
      walk(ix, iy, it, W-1, H-1);
    }
    else {
      walk(ox, oy, ot, W, H);
    }

    if(next(ox, oy, ix, iy)) { ans ++; }

    outer = make_tuple(ox, oy, ot);
    inner = make_tuple(ix, iy, it);

  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

int W, H;
int _x1 = 0, _y1 = 0; // outside
int _x2 = 0, _y2 = 1; // inside
int mode1 = 0, mode2 = 0;

void Move(int& x, int& y, int mode) {
	if (mode == 0) {
		++x;
	}
	else if (mode == 1) {
		++y;
	}
	else if (mode == 2) {
		--x;
	}
	else {
		assert(false);
	}
}
void f(int target) {
	if (target == 0) { // outside
		if (mode1 == 0 && _x1 >= W-1) ++mode1;
		if (mode1 == 1 && _y1 >= H-1) ++mode1;
		Move(_x1, _y1, mode1);
	}
	else { // inside
		if (mode2 == 0 && _x2 >= W-2) ++mode2;
		if (mode2 == 1 && _y2 >= H-2) ++mode2;
		Move(_x2, _y2, mode2);
	}
}
bool check() {
	if ( abs(_x1 - _x2) + abs(_y1 - _y2) == 1 ) {
		return true;
	}
	return false;
}
int main() {
	int N; cin >> W >> H >> N;
	int ans = 0;
	for (int i = 0; i < N; ++i) {
		int p; cin >> p;
		f(p);
		if (check()) ++ans;
	}
	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;
    for(int i=0; i<n; ++i){
      int tmp;
      if(cin >> tmp,tmp){++out;}else{++in;}
      if(out<=w-2 && !abs(out-in))++sum;
      else if(out<=h+w-3 && out>=w && !abs(out-in-2))++sum;
      else 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;
  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>
using namespace std;

int main()
{
  int W, H;
  int N;

  cin >> W >> H;
  cin >> N;
  int x = W * 2 + H - 2;
  int y = (W - 1) * 2 + H - 4;
  int ret = 0;

  while(N--) {
    int P;
    cin >> P;
    if(P == 0) --x;
    else --y;

    if(x < W) {
      if(x == y) ++ret;
    } else if(x == W) {
      
    } else if(x < W + H - 1) {
      if(x == y + 2) ++ret;
    } else if(x == W + H - 1) {
    
    } else {
      if(x == y + 4) ++ret;
    }
  }
  cout << ret << endl;
}

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) {
        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--;
        }
        // cout << "---" << endl;
        // printf("uzu: (%d, %d)\n", uzu.first, uzu.second);
        // printf("rin: (%d, %d)\n", rin.first, rin.second);
        if ((abs(uzu.first - rin.first) + abs(uzu.second - rin.second)) == 1) {
            ans++;
            // cout << "cnt" << endl;
        }
    }
    cout << ans << endl;
    return 0;
}