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,n;
  while(cin >> w >> h >> n){
    int out=0,in=0,sum=0;
    int tmp;
    for(int i=0; i<n; ++i){
      //cout << "frg" << endl; 
      if(out<w-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in))++sum;
      }else if(out<h+w-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in-2))++sum;
      }else if(out<2*w+h-2){
        int tmp;
        if(cin >> tmp,tmp){++out;}else{++in;}
        if(!(out-in-4))++sum;
      }
      //cout << "frg2" << endl; 
    }
    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 - 2) {
      if(x == y + 2) ++ret;
    } else if(x == W + H - 2) {
    
    } else {
      if(x == y + 4) ++ret;
    }
  }
  cout << ret << 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;
    while(--n){
      int tmp;
      if(cin >> tmp,tmp){++in;}else{++out;}
      if(out<=w-1 && !(out-in))++sum;
      else if(out<=h+w-2 && out>w && !(out-in-2))++sum;
      else if(out >= w+h && !(out-in-4))++sum;
    }
    cout << sum << endl;
  }
  return 0;
}

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))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
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;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1,6);
uniform_real_distribution<double> score(0.0,10.0);
 
int main(void){
    int w, h; cin >> w >> h;
    int n; cin >> n;
    vi p(n);
    for(auto & e : p) cin >> e;

    int is = 0, os = 0, res = 0;
    for(auto & e : p){
        if(e == 0){
            os++;
        }
        else {
            is++;
        }

        if(os <= w - 2){
            if(is <= w - 2 and os == is)
                res++;
        }
        else if(os <= w + h - 3){
            if(w - 2 <= is <= w + h - 5 and os - 2 == is)
                res++;
        }
        else {
            if(w + h - 5 <= is and os - 4 == is){
                res++;
            }
        }
    }

    cout << res << endl;

    return 0;
}

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

int H, W;
int a, b;
int main() {
	cin >> H >> W;
	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>
#include<cassert>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<tuple>
#include<numeric>
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i,j) for(int__ i=0;i<(int__)(j);i++)
#define repeat(i,j,k) for(int__ i=(j);i<(int__)(k);i++)
#define all(v) v.begin(),v.end()

template<typename T>
ostream& operator << (ostream &os , const vector<T> &v){
    rep(i,v.size()) os << v[i] << (i!=v.size()-1 ? " " : "\n"); return os;
}

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

#ifdef DEBUG
void debug(){ cerr << endl; }
#endif
template<class F,class...R>
void debug(const F &car,const R&... cdr){
#ifdef DEBUG
    cerr << car << " "; debug(cdr...);
#endif
}


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++;
        debug("x", x[p], "y", y[p]);
    }
    
    cout << ans << endl;
        
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    while(solve());
    return 0;
}