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

#define dump(...) do{print_vars(cout<<"# "<<#__VA_ARGS__<<'=',__VA_ARGS__);cout<<endl;}while(0)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vd=vector<double>;
using vvd=vector<vd>;
using vs=vector<string>;

void print_vars(ostream&){}
template<typename Car,typename... Cdr>
void print_vars(ostream& os,const Car& car,const Cdr&... cdr){
	print_vars(os<<car<<(sizeof...(cdr)?",":""),cdr...);
}

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<int I,typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<int I,typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	os<<get<I>(t)<<(sizeof...(Cdr)?",":"");
	print_tuple<I+1,Cdr...>(os,t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<0,Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

int solve_naive(const string& s)
{
	map<string,int> vis;
	queue<tuple<string,int>> q;
	q.emplace(s,0);
	while(q.size()){
		string cs; int cc; tie(cs,cc)=q.front(); q.pop();
		if(vis.count(cs)) continue;
		vis[cs]=cc;
		for(int i=0;(i=cs.find("101",i))!=-1;i++){
			cs[i]='0';cs[i+1]='1';cs[i+2]='0';
			q.emplace(cs,cc+1);
			cs[i]='1';cs[i+1]='0';cs[i+2]='1';
		}
	}
	//dump(vis);
	int res=0;
	for(auto p:vis) res=max(res,p.second);
	return res;
}

int calc(const string& s)
{
	//dump(s);
	vi zs;
	for(int i=0;;i++){
		i=s.find('0',i);
		if(i==-1) break;
		zs.push_back(i);
	}
	int m=zs.size();

	if(m==0)
		return 0;

	bool skip=false;
	vi dp1(m);
	dp1[0]=zs[0];
	repi(i,1,m){
		int tmp=zs[i]-zs[i-1]-(skip?1:2);
		dp1[i]=dp1[i-1]+tmp;
		skip=tmp==0;
	}
	
	skip=false;
	vi dp2(m);
	dp2[m-1]=s.size()-zs[m-1]-1;
	per(i,m-1){
		int tmp=zs[i+1]-zs[i]-(skip?1:2);
		dp2[i]=dp2[i+1]+tmp;
		skip=tmp==0;
	}

	//dump(dp1); dump(dp2);

	int res=max(dp1[m-1],dp2[0]);
	rep(i,m-1){
		if(zs[i+1]-zs[i]>2)
			res=max(res,dp1[i]+dp2[i+1]);
		else{
			if(i>0)
				res=max(res,dp1[i-1]+dp2[i+1]);
			if(i+2<m)
				res=max(res,dp1[i]+dp2[i+2]);
		}
	}
	return res;
}

int NextPow2(int x)
{
	x--;
	for(int i=1;i<32;i*=2) x|=x>>i;
	return x+1;
}
const int I=0;
int F(int a,int b){return max(a,b);}

struct SegmentTree{
	int size;
	vi data;
	SegmentTree(int n):size(NextPow2(n)),data(size*2,I){}
	SegmentTree(const vi& a):size(NextPow2(a.size())),data(size*2,I){
		copy(all(a),begin(data)+size);
		peri(i,1,size) data[i]=F(data[i*2],data[i*2+1]);
	}
	int Get(int i){
		return data[size+i];
	}
	void Update(int i,int x){
		data[size+i]=x;
		for(i+=size;i/=2;) data[i]=F(data[i*2],data[i*2+1]);
	}
	int Query(int a,int b,int i,int l,int r){
		if(b<=l || r<=a) return I;
		if(a<=l && r<=b) return data[i];
		return F(Query(a,b,i*2,l,(l+r)/2),Query(a,b,i*2+1,(l+r)/2,r));
	}
	int Query(int a,int b){
		return Query(a,b,1,0,size);
	}
};

int calc2(const string& s)
{
	//dump(s);

	vector<tuple<int,int>> ranges;
	for(int i=0;(i=s.find("101",i))!=-1;i++){
		int l=i,r=i+3;
		while(l>=0&&s[l]=='1') l--;
		while(r<s.size()&&s[r]=='1') r++;
		l++;
		//dump(l,r);
		// 1011..11
		ranges.emplace_back(i,r);
		if(i+3<r)
			ranges.emplace_back(i,r-1);
		// 11..1101
		ranges.emplace_back(l,i+3);
		if(l<i)
			ranges.emplace_back(l+1,i+3);
	}
	sort(all(ranges),[](auto a,auto b){
		return get<1>(a)<get<1>(b);
	});
	//dump(ranges);

	SegmentTree st(s.size());
	for(auto range:ranges){
		int l,r; tie(l,r)=range;
		int x=st.Query(0,l),cur=st.Query(r-1,r);
		if(cur<x+(r-l-2)){
			st.Update(r-1,x+(r-l-2));
		}
	}
	//dump(st.Query(0,s.size()));
	return st.Query(0,s.size());
}

int solve(string s)
{
	//dump(s);
	int i=s.find('1'),j=s.rfind('1');
	if(i==-1)
		return 0;

	s=s.substr(i,j-i+1)+"00";
	int res=0;
	for(i=0;i<s.size();){
		j=s.find("00",i);
		string t=s.substr(i,j-i);
		//res+=calc(t);
		res+=calc2(t);
		for(i=j;i<s.size()&&s[i]=='0';i++);
	}
	return res;
}

bool test()
{
	int n=16;
	for(int i=0;i<(1<<n);i++){
		string s;
		rep(j,n) s+="01"[i>>j&1];
		int ans=solve_naive(s);
		int res=solve(s);
		if(ans!=res){
			dump(s);
			dump(ans,res);
			return false;
		}
	}
	return true;
}

int main()
{
	#ifndef _GLIBCXX_DEBUG
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	constexpr char endl='\n';
	#endif

	//cout<<"### TEST "<<(test()?"PASSED":"FAILED")<<endl;

	for(int n;cin>>n&&n;){
		string s; cin>>s;
		//dump(solve_naive(s));
		cout<<solve(s)<<endl;
	}
}


Measured memory_bytes: 5916