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;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int w,h,n;
    cin >>w >>h >>n;

    vector<int> p(n);
    rep(i,n) cin >>p[i];

    int ux=1, uy=1;
    int udx=1, udy=0;

    int sx=1, sy=2;
    int sdx=1, sdy=0;

    int ans = 0;
    rep(i,n){
        if(p[i]==0){
            ux += udx;
            uy += udy;

            if(ux==w){
                udx = 0;
                udy = 1;
                if(uy==h){
                    udx = -1;
                    udy = 0;
                }
            }
        }
        else{
            sx += sdx;
            sy += sdy;

            if(sx==w-1){
                sdx = 0;
                sdy = 1;
                if(uy==h-1){
                    sdx = -1;
                    sdy = 0;
                }
            }
        }

        if(ux<0 || sx<0) break;
        if(abs(ux-sx)+abs(uy-sy) == 1) ++ans;
    }
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;

typedef long long ll;
typedef ll int__;
#define rep(i,j) for(int__ i=0;i<(int__)(j);i++)

template<typename T>
istream& operator >> (istream &is , vector<T> &v){
    rep(i,v.size()) is >> v[i]; return is;
}

bool solve(){
    int W, H; cin >> W >> H;
    int N; cin >> N;
    vector<int> P(N); cin >> P;

    int x[2] = {0, 0}, y[2] = {0, 1};
    int dx[2] = {1, 1}, dy[2] = {};

    int ans = 0;
    for(int p : P){
        x[p] += dx[p];
        y[p] += dy[p];
        if(y[p] == p and x[p] == W-1-p) dx[p] =  0, dy[p] = 1;
        if(x[p] == W-1-p and y[p] == H-1-p) dx[p] = -1, dy[p] = 0;

        if(x[0] == x[1] and abs(y[0] - y[1]) == 1) ans++;
        if(y[0] == y[1] and abs(x[0] - x[1]) == 1) ans++;
    }
    
    cout << ans << endl;
        
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    while(solve());
    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 == W - 1){
        c = 1;
      }
      if(o_s == i_s && o_s != W - 1){
        ++ans;
      }
    }
    else if(c == 1){
      if(o_s == W + H - 2){
        c = 2;
      }
      if(o_s == i_s + 2 && o_s != W + H - 2){
        ++ans;
      }
    }
    else{
      //if(i_s == W + H + W - 5 || o_s == W + H + W - 2){
        //break;
      //}
      if(o_s == i_s + 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 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;
}