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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	int w,h;
	cin>>w>>h;
	int a=1;
	int b=1;
	int ans=0;
	int n;
	cin>>n;
	rep(i,n){
		int t;
		cin>>t;
		if(t==0) a++;
		else b++;
		if(b==w-1){
			if(a==w-1||a==w+1){
				ans++;
			}
		}
		else if(b<w-1){
			if(a==b){
				ans++;
			}
		}
		else if(b==w+h-4){
			if(a==w+h-2||a==w+h){
				ans++;
			}
		}
		else if(b<w+h-4){
			if(b+2==a){
				ans++;
			}
		}
		else{
			if(b+4==a){
				ans++;
			}
		}
	}
	cout<<ans<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}

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

using namespace std;

int main(){

  int w,h;
  int n;
  
  cin>>w>>h;
  cin>>n;

  vector<int> p(n);

  for(int i=0;i<n;++i)
	cin>>p[i];

  int in = 0;
  int out = 0;
  int cnt = 0;

  for(auto i:p){

	if(i == 1)
	  in++;
	else if(i == 0)
	  out++;

	if(out <= w-2 && out == in)
	  cnt++;
	
	else if(out >= w && out <= w+h-3 && out - in == 2)
	  cnt++;

	else if(out >= w+h-1 && out - in == 4)
	  cnt++;
	
  }

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

C++: #define _USE_MATH_DEFINES

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int W, H, N;
int data[100];

int main(){
	scanf("%d%d%d", &W, &H, &N);
	for (int i = 0; i < N; ++i){
		scanf("%d", &data[i]);
	}

	int cnt = 0;
	int curAx = 1, curAy = 1;
	int curBx = 1, curBy = 2;
	for (int i = 0; i < N; ++i) {
		if (data[i] == 0) {
			if (curAy == 1 && curAx != W)  {
				curAx++;
			}
			else if (curAx == W && curAy < H) {
				curAy++;
			}
			else if (curAy == H) {
				curAx--;
			}
		}
		else {
			if (curBy == 2 && curBx != W - 1) {
				curBx++;
			}
			else if (curBx == W - 1 && curBy < H - 1) {
				curBy++;
			}
			else if (curBy == H - 1) {
				curBx--;
			}
		}

		if (curAx == curBx && ((curAy == 1 && curBy == 2) || (curAy == H && curBy == H-1))) {
			++cnt;
		}
		else if (curAy == curBy && curBx - curAx == 1 ) {
			++cnt;
		}
	}
	printf("%d\n", cnt);
	return 0;
}

C++: #include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
int main(void)
{
  int w,h,n;
  cin >> w >> h >> n;
  int udk=0,rin=0;
  int answer=0;
  REP(i,n){
    int tmp;
    cin >> tmp;
    if(tmp==0) ++udk;
    else ++rin;
    if(udk==w-1||udk==w+h-2) continue;
    if((udk<w-1 && udk==rin) ||
       (w-1<udk && udk<w+h-2 && udk-2 == rin) ||
       (w+h-2<udk && udk-4 == rin)) ++answer;
  }
  cout << answer << endl;
  return 0;
}

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

typedef pair<int, int> i_i;

i_i f(int H, int W, int z) {
	int x, y;
	if (z < W) {
		x = z;
		y = 0;
	}
	else if (z >= W + H - 2) {
		x = (W - 1) - (z - (W + H - 2));
		y = H - 1;
	}
	else {
		x = W - 1;
		y = z - (W - 1);
	}
	return i_i(x, y);
}

int main() {
	int W, H; cin >> W >> H;
	int a = 0, b = 0;
	int N; cin >> N;
	int cnt = 0;
	while (N--) {
		int z; cin >> z;
		if (z) a++;
		else b++;
		i_i p = f(H - 2, W - 1, a), q = f(H, W, b);
		if (abs(p.first - q.first) + abs(p.second - q.second + 1) == 1)
			cnt++;
	}
	cout << cnt << endl;
}