#include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int flood(char map[][14], int i, int j){
    if(map[i][j] == '1')
        map[i][j] = '0';
    if(map[i+1][j] == '1')
        flood(map,i+1,j);
    if(map[i][j+1] == '1')
        flood(map,i,j+1);
    if(map[i-1][j] == '1')
        flood(map,i-1,j);
    if(map[i][j-1] == '1')
        flood(map,i,j-1);
    return 1;
}

int main(){
    bool loop = true;
    char map[14][14] = {{'0'}}, s[14];
    while(loop){
        range(i,1,13){
            if(!(cin >> s)) loop = false;
            rep(j,12){
                map[i][j+1] = s[j];
            }
        }
        int ans = 0;
        range(i,1,13){
            range(j,1,13){
                if(map[i][j] == '1')
                    ans += flood(map,i,j);
            }
        }
        if(loop)
            cout << ans << endl;
    }
}
#include <bits/stdc++.h>
#define cinf(n,x) for(int i=0;i<(n);i++) cin >> x[i];
typedef long long int ll;
using namespace std;

int main(){
	int n,p;
	int have[50];
	while(true){
		cin >> n >> p;
		if(n==0 && p==0) break;
		for(int i=0;i<n;i++) have[i]=0;
		int turn=0;
	    int cnt=p;
		while(true){
			if(cnt>0){
				cnt--;
				have[turn%n]++;
				if(have[turn%n]==p){
					cout << turn%n << endl;
					break;
				}

			}
			else{
					cnt =have[turn%n];
					have[turn%n]=0;
				}
			turn++;

		}

	}
}
