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>
#define LLI long long int

using namespace std;

int n,ans,x,y,W,H,p;


int main(){

    cin  >> W >> H >> n;

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

        if(p==0)x++;
        else y++;

        if(x<W&&y<W){
            if(x==y)ans++;
        }else if(W<x&&x<W+H-1&&W-2<y&&y<W+H-3){
            if(x==y-2)ans++;
        }else if(W+H-1<x&&W+H-5<y){
            if(x==y-4)ans++;
        }
    }



    cout << ans << endl;

return 0;
}

C++: #include <iostream>
#include <vector>
#include <cassert>

using namespace std;

int w, h, n;

bool adj(int a, int b) {
    if (0 <= a && a <= w - 2 &&
        0 <= b && b <= w - 2) {
        return a == b;
    }
    if (w - 2 <= a && a <= w - 2 + h - 3 && 
        w <= b && b <= w + h - 3) {
        return a - (w - 2) == b - w;
    }
    if (w - 2 + h - 3 <= a &&
        w + h - 1 <= b) {
        return a - (w - 2 + h - 3) == b - (w + h - 1);
    }
    return false;
}

int main() {
    while (cin >> w >> h >> n) {
        int a = 0, b = 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int p;
            cin >> p;
            if (p) b++; else a++;
            ans += adj(a, b);
        }
        cout << ans << 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 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 < 0 || b < 0) continue;
        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 <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main(){
  int W,H,N;
  int p;
  int cunt=0,in=1,out=1;

  cin>>W>>H;
  cin>>N;
  
  for(int i=0;i<N;i++){
    cin>>p;
    if(p==1)
      in++;
    else
      out++;
    
    if(in<=W-1 && out<=W-1){
      if(in==out)
        cunt++;
    }
    else if(W-1<=in && in<=W+H-4 && W+1<=out && out<=W+H-2){
      if(out==in+2)
        cunt++;
    }
    else if(W+H-4<=in && W+H-2<=out){
      if(out==in+4)
        cunt++;
    }
  }

  cout<<cunt<<endl;
  return 0;
}

C++: #include <iostream>
using namespace	std;
int main(){

  int h,w,n,a0=0,a1=0,r=0;
  cin>>w>>h>>n;
  int c0=w-2,c1=w-1+h-5+1;
  for(int i=0,b;i<n;i++){
    cin>>b;
    if(b) a1++;
    else a0++;
    if(a1<=c0-1){
      if(a0-a1==0) r++;
    } else if(a1==c0){
      if(a0-a1==0||a0-a1==2) r++;
    } else if(a1<=c1-1){
      if(a0-a1==2) r++;
    } else if(a1==c1){
      if (a0-a1==2||a0-a1==4) r++;
    } else {
      if(a0-a1==4) r++;
    }
  }
  cout<<r<<endl;
  return 0;

}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll w,h;
ll n;
ll a[101010];

int main(){
  cin>>w>>h;
  cin>>n;
  rep(i,n)cin>>a[i];
  P p1=P(0,0),p2=P(1,0);
  int res=0;
  rep(i,n){
    if(!a[i]){
      if(p1.fi==0&&p1.se!=w-1){
        p1.se++;
      }else if(p1.se==w-1&&p1.fi!=h-1){
        p1.fi++;
      }else{
        p1.se--;
      }
    }else{
      if(p2.fi==1&&p2.se!=w-2){
        p2.se++;
      }else if(p2.se==w-2&&p2.fi!=h-2){
        p2.fi++;
      }else{
        p2.se--;
      }
    }
    if((p1.fi==p2.fi&&abs(p1.se-p2.se)<=1)||(p1.se==p2.se&&abs(p1.fi-p2.fi)<=1))res++;
  }
  cout<<res<<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(sy==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;
}