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 <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

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

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    
    int W, H;
    cin >> W >> H;
    int N;
    cin >> N;
    vector<int> P(N);
    rep(i, N) cin >> P[i];
    
    pair<int, int> a1 = mp(W - 1, 0), a2 = mp(W - 1, H - 1), b1 = mp(W - 2, 1), b2 = mp(W - 2, H - 2);
    
    int as = 0, bs = 0; // 0, 1, 2
    pair<int, int> anow = mp(0, 0), bnow = mp(0, 1);
    
    int ret = 0;
    
    rep(i, N){
        if (P[i] == 0) {
            if (as == 0) anow.first++;
            if (as == 1) anow.second++;
            if (as == 2) anow.first--;
            if (anow == a1) as = 1;
            if (anow == a2) as = 2;
        }
        if (P[i] == 1) {
            if (bs == 0) bnow.first++;
            if (bs == 1) bnow.second++;
            if (bs == 2) bnow.first--;
            if (bnow == b1) bs = 1;
            if (bnow == b2) bs = 2;
        }
        bool con = false;
        rep(j, 4){
            if (mp(anow.first + dx[j], anow.second + dy[j]) == bnow) {
                con = true;
                break;
            }
        }
        if (con) {
            ret++;
        }
    }
    
    output(ret, 0);
    
    
    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 { // outside
		if (mode2 == 0 && _x1 >= W-2) ++mode2;
		if (mode2 == 1 && _y1 >= 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;
  cin >> w >> h >> n;
  int out=0,in=0,sum=0;
  while(--n){
    int tmp;
    cin >> tmp;
    if(tmp){++in;}else{++out;}
    if(out<=w-2 && out==in)++sum;
    else if(out>=w && out<= w+h-3 && out-in==2)++sum;
    else if(out >= w+h-1 && out-in==4)++sum;
  }
  cout << sum << endl;
}

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++;
	}
	if (abs(X1 - X2) == 1 and Y2 == Y1){
	  res++;
	}
	
      }else if(Y2 >= 2 and X2 == 1){//right
	Y2--;
	if (abs(X1 - X2) == 1 and Y2 == Y1){
	  res++;
	}
	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  res++;
	}

      }else if(Y2 == 1 and X2 >= 1){//lower
	X2++;
	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  res++;
	}

	  
      }
    }else{//outer
      
      if (Y1 == H - 1 and X1 >= 1){//upper
	X1--;

	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  res++;
	}
	if (abs(X1 - X2) == 1 and Y2 == Y1){
	  res++;
	}
	
      }else if(Y1 >= 1 and X1 == 0){//right
	Y1--;
	if (abs(X1 - X2) == 1 and Y2 == Y1){
	  res++;
	}
	if (X1 == X2 and abs(Y2 - Y1) == 1){
	  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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

int main(){
  int w,h,n;
  cin>>w>>h>>n;
  vi p(n);
  REP(i,n)cin>>p[i];
  int ax,ay,bx,by;
  ax = bx = 1;
  ay = 1;
  by = 2;
  int res = 0;
  REP(i,n){
    if(p[i]==0){
      // outer
      if(ay==h){
        --ax;
      }else if(ax==w){
        ++ay;
      }else if(ay==1){
        ++ax;
      }
    }else{
      // inner
      if(by==h-1){
        --bx;
      }else if(bx==w-1){
        ++by;
      }else if(by==2){
        ++bx;
      }
    }
    int dist = abs(bx-ax)+abs(by-ay);
    if(dist==1)++res;
  }
  cout<<res<<endl;
  return 0;
}