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<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*2) 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 <iostream>
#include <vector>
using namespace std;
int main(void){
	int W,H;
	cin>>W>>H;
	int n;
	cin>>n;

	int in = 0 , out = 0 , c = 0;
	for(int i = 0 ; i < n ; i ++){
		int inp;
		cin>>inp;
		if(inp){
			in ++;
		}else{
			out ++;
		}
		if(out < W){
			if(in == out) c ++;
		}else if(out < W + H - 1){
			if(in == out - 1) c ++;
		}else{
			if(in == out - 2) c ++;
		}
	}
	cout<<c<<endl;
}

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

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 = W + H + W - 2;
  int o = W + H + W - 2;
  int i_s = W + H + W - 6;
  int c = 0;
  int ans = 0;

  for(int e : row){
    if(e == 0){
      --o_s;
    }
    else{
      --i_s;
    }

    if(o_s == 0 || i_s == 0){
      break;
    }
    else if(o - (W - 1) < o_s){
      if(o_s == i_s + 4){
        ++ans;
      }
    }
    else if(o - (W - 1) == o_s){
      continue;
    }
    else if(o - (W - 1) > o_s && o_s > o - (W + H - 2)){
      if(o_s == i_s + 2){
        ++ans;
      }
    }
    else if(o_s = o - (W + H - 2){
      continue;
    }
    else if(o_s < o - (W + H - 2)){
      if(o_s == i_s){
        ++ans;
      }
    }
  }

  cout<<ans<<endl;

  return 0;
}

C++: #include <iostream>

using namespace std;
int W,H;
int N;

int main(){
  cin >> W >> H;
  cin >> N;
  int Y1,X1;//outer
  int Y2,X2;//inner
  Y1 = H - 1;
  X1 = W - 1;
  
  Y2 = H - 2;
  X2 = W - 1;

  int res = 0;
  for (int i = 0; i < N; i++){
    int tmp;
    cin >> tmp;
    if (tmp){//inner
      if (Y2 == H - 2 and X2 >= 2){//upper
	X2--;
	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  res++;
	}
					  
      }else if(Y2 >= 2 and X2 == 1){//right
	Y2--;
	if (abs(X1 - X2) == 1 and Y2 == Y1){
	  res++;
	}
      }else if(Y2 == 1 and X2 >= 1){//lower
	X2++;
	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  res++;
	}
	  
      }
      
      
    }else{
      if (Y1 == H - 1 and X1 >= 1){
	X1--;
	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  res++;
	}
      }else if(Y1 >= 1 and X1 == 0){
	Y1--;
	if (abs(X1 - X2) == 1 and Y2 == Y1){
	  res++;
	}
      }else if(Y1 == 0 and X1 >= 0){
	X1++;
	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  res++;
	}	
      }
      
      
    }
  }
  cout << res << 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-6 && w+2 <= a && a <= w+h-4){
            if(a-2 == b) ans++;
        }
        if(w+h-4 <= b && w+h <= a){
            if(fa-a == fb-b) ans++;
        }
    }
    cout << ans << endl;
}