#include <iostream>
#include <vector> 
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <cerrno>
#include <cfenv>
#include <math.h>
#include <iomanip>
#include <limits>
#include <queue>
#define ll long long int
#define mp make_pair

using namespace std;

int main(){

    int w,h;

    int map[51][51];

    int flag[60][60];

    while(true){

        cin >> w >> h;

        if(w==0 && h==0){
            break;
        }

        for(int i=0;i<60;i++){
            for(int j=0;j<60;j++){
                flag[i][j] = 0;
            }
        }

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin >> map[i][j];
            }
        }

        queue<pair<int,int> > q;

        ll ans = 0;

        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(map[i][j] == 1 && flag[i][j] == 0){
                    q.push(mp(i,j));
                    while(!q.empty()){
                        pair<int,int> tmp;
                        tmp = q.front();
                        q.pop();
                        if(flag[tmp.first][tmp.second] == 1){
                            continue;
                        }
                        //cout << tmp.first << " " << tmp.second << " " << ans << endl;
                        flag[tmp.first][tmp.second] = 1;
                        if(tmp.first > 0 && tmp.second > 0 && map[tmp.first - 1][tmp.second - 1] == 1 && flag[tmp.first - 1][tmp.second - 1] == 0){
                            q.push(mp(tmp.first - 1,tmp.second - 1));
                        }
                        if(tmp.first > 0 && map[tmp.first - 1][tmp.second] == 1 && flag[tmp.first - 1][tmp.second] == 0){
                            q.push(mp(tmp.first -1,tmp.second));
                        }
                        if(tmp.first > 0 && tmp.second < w-1 && map[tmp.first - 1][tmp.second + 1] == 1 && flag[tmp.first - 1][tmp.second + 1] == 0){
                            q.push(mp(tmp.first - 1,tmp.second + 1));
                        }
                        if(tmp.second > 0 && map[tmp.first][tmp.second - 1] == 1 && flag[tmp.first][tmp.second - 1] == 0){
                            q.push(mp(tmp.first,tmp.second - 1));
                        } 
                        if(tmp.second < w-1 && map[tmp.first][tmp.second + 1]==1 && flag[tmp.first][tmp.second + 1] == 0){
                            q.push(mp(tmp.first,tmp.second + 1));
                        }
                        if(tmp.first < h-1 && tmp.second > 0 && map[tmp.first + 1][tmp.second - 1]==1 && flag[tmp.first + 1][tmp.second - 1] == 0){
                            q.push(mp(tmp.first + 1,tmp.second - 1));
                        }
                        if(tmp.first < h-1 && map[tmp.first + 1][tmp.second] == 1 && flag[tmp.first + 1][tmp.second] == 0){
                            q.push(mp(tmp.first + 1,tmp.second));
                        }
                        if(tmp.first < h-1 && tmp.second < w-1 && map[tmp.first + 1][tmp.second + 1] == 1 && flag[tmp.first + 1][tmp.second + 1] == 0){
                            q.push(mp(tmp.first + 1,tmp.second + 1));
                        }
                    }

                    ans++;

                }
            }
        }

        cout << ans << endl;

    }

    return 0;
}

/*
25 33
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 0 0 1 0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