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)
}

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(n):
    u += a[i]
    s += 1- a[i]
    if u <= w - 2 and u == s:c += 1
    elif w - 2 <= u and u <= w + h - 5 and u + 2 == s:c += 1
    elif w + h - 5 <= u and u + 3 == s:c += 1
print(c)

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(n):
    u += a[i]
    s += 1- a[i]
    if u <= w - 2 and u == s:c += 1
    elif w - 2 <= u and u <= w + h - 5 and u + 2 == s:c += 1
    elif w + h - 5 <= u and u + 4 == s:c += 1
print(c)

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(n):
    u += a[i]
    s += 1- a[i]
    if u <= w - 2 and u == s:c += 1
    elif w - 2 <= u and u <= w + h - 5 and u + 2 == s:c += 1
    elif w + h - 5 <= u and u + 4 == s:c += 1
print(c)

Python: # AOJ 1575: Product Sale Lines
# Python3 2018.7.13 bal4u

W, H = map(int, input().split())
N = int(input())
P = list(map(int, input().split()))
ans = x = y = 0
for p in P:
	if p == 0: x += 1
	else: y += 1
	if x < W-1:
		if x == y: ans += 1
	elif x == W-1: pass
	elif x < W+H-2:
		if x == y+2: ans += 1
	elif x == W+H-2: pass
	else:
		if x == y+4: ans += 1
print(ans)

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(n):
    u += a[i]
    s += 1- a[i]
    if u <= w - 2 and u == s:c += 1
    elif w - 2 <= u and u <= w + h - 5 and u + 2 == s:c += 1
    elif w + h - 5 <= u and u + 4 == s:c += 1
print(c)

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(len(a)):
    u += a[i]
    s += 1- a[i]
    if u < w - 2:
        if u == s:
            c += 1
    elif u == w - 2:
        if u == s or u + 2 == s:
            c += 1
    elif u < w + h - 5:
        if u + 2 == s:
            c += 1
    elif u == w + h - 5:
        if u + 2 == s or u + 4 == s:
            c += 1
    elif u + 4 == s:c += 1
print(c)

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(n):
    u += a[i]
    s += 1- a[i]
    if u <= w - 2 and u == s:c += 1
    elif w - 2 <= u and u <= w + h - 5 and u + 2 == s:c += 1
    elif w + h - 5 <= u and u + 5 == s:c += 1
print(c)

Python: pos = [[0,0], [1,0]]
way = [[0,1], [0,1]]
ans = 0
w, h = map(int, input().split())
n = int(input())
li = list(map(int, input().split()))
for p in li:
    ni = pos[p][0]+way[p][0]
    nj = pos[p][1]+way[p][1]
    if p == 0:
        if nj >= w or ni >= h:
            way[p][0],way[p][1]=way[p][1],-way[p][0]
            ni = pos[p][0]+way[p][0]
            nj = pos[p][1]+way[p][1]
    else:
        if nj >= w-1 or ni >= h-1:
            way[p][0],way[p][1]=way[p][1],-way[p][0]
            ni = pos[p][0]+way[p][0]
            nj = pos[p][1]+way[p][1]
    pos[p][0] = ni
    pos[p][1] = nj
    if abs(pos[0][0]-pos[1][0])+abs(pos[0][1]-pos[1][1]) == 1:
        ans += 1
print(ans)

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(len(a)):
    if a[i] == 1:
        u += 1
    elif a[i] == 0:
        s += 1
    if u < w - 2:
        if u == s:
            c += 1
    elif u == w - 2:
        if u == s or u + 2 == s:
            c += 1
    elif u < w + h - 5:
        if u + 2 == s:
            c += 1
    elif u == w + h - 5:
        if u + 2 == s or u + 4 == s:
            c += 1
    else:
        if u + 4 == s:
            c += 1
print(c)

Python: # AOJ 1575: Product Sale Lines
# Python3 2018.7.13 bal4u

W, H = map(int, input().split())
N = int(input())
p = list(map(int, input().split()))
ans = x = y = 0
for x in p:
	if x == 0: x += 1
	else: y += 1
	if x < W-1:
		if x == y: ans += 1
	elif x == W-1: pass
	elif x < W+H-2:
		if x == y+2: ans += 1
	elif x == W+H-2: pass
	else:
		if x == y+4: ans += 1
print(ans)

Python: h, w = map(int, input().split())
n = int(input())
a = list(map(int, input().split()))
u, s, c = 0, 0, 0
for i in range(len(a)):
    u += a[i]
    s += 1- a[i]
    if u < w - 2:
        if u == s:
            c += 1
    elif u == w - 2:
        if u == s or u + 2 == s:
            c += 1
    elif u < w + h - 5:
        if u + 2 == s:
            c += 1
    elif u == w + h - 5:
        if u + 2 == s or u + 4 == s:
            c += 1
    else:
        if u + 4 == s:
            c += 1
print(c)

Python: w,h = map(int, input().split())
n = int(input())
p = list(map(int, input().split()))
u = m = cnt = 0
for i in range(n):
    if p[i] == 0:
        u += 1
    else:
        m += 1
    if u <= w-2:
        if u == m:
            cnt += 1
    elif w <= u <= w+h-3:
        if u == m+2:
            cnt += 1
    elif h+h-1 <= u:
        if u == m+4:
            cnt += 1
print(cnt)

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 = 0;
  int i_s = 0;
  int c = 0;
  int ans = 0;

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

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

  cout<<ans<<endl;

  return 0;
}