Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout
	cnt := 0

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
		cnt = 0
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)
	for solve(scanner, writer) {

	}
	for i := 0; i < cnt; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
	writer.Flush()
}

func solve(scanner *bufio.Scanner, writer *bufio.Writer) bool {
	sol := solver{}
	sol.w = getNextInt(scanner)
	sol.h = getNextInt(scanner)
	if sol.w == 0 && sol.h == 0 {
		return false
	}
	sol.land = makeInts(sol.h, sol.w)
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			sol.land[y][x] = getNextInt(scanner)
		}
	}
	ans := 0
	for y := 0; y < sol.h; y++ {
		for x := 0; x < sol.w; x++ {
			if sol.land[y][x] == 0 {
				continue
			}
			ans++
			sol.dfs(y, x)
		}
	}
	fmt.Fprintln(writer, ans)
	return true
}

type solver struct {
	w, h int
	land [][]int
}

func (sol *solver) dfs(y, x int) {
	if y < 0 {
		return
	}
	if y >= sol.h {
		return
	}
	if x < 0 {
		return
	}
	if x >= sol.w {
		return
	}
	if sol.land[y][x] == 0 {
		return
	}
	sol.land[y][x] = 0
	dy := [8]int{-1, -1, -1, 0, 0, 1, 1, 1}
	dx := [8]int{-1, 0, 1, -1, 1, -1, 0, 1}
	for i := 0; i < 8; i++ {
		sol.dfs(y+dy[i], x+dx[i])
	}
}
func makeInts(h, w int) [][]int {
	index := make([][]int, h, h)
	data := make([]int, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

C++: #include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>
#include <ios>
#include <iomanip>
using namespace std;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define print(x) cout<<(x)<<'\n'
#define print_vec(v) rep(i,v.size()){if(i!=0)cout<<" ";cout<<v[i];}cout<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";rep(i,v.size())cout<<" "<<v[i];cout<<'\n'
#define pb push_back
#define mp make_pair
#define MOD 1000000007 //10^9+7
#define fi first
#define sc second
using ll=long long;
using pi=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
using vstr=vector<string>;


int w,h,c[50][50];
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};

bool inRange(int y,int x){
    if(0<=x && x<w && 0<=y && y<h) return true;
    else return false;
}

void dfs(int x,int y){
    c[x][y] = 0;
    rep(i,8){
        int x_next=x+dx[i],y_next=y+dy[i];
        if(inRange(x_next,y_next) && c[x_next][y_next]==1){
            dfs(x_next,y_next);
        }
    }
}

void solve() {
    while(1){
        cin >> w >> h;
        if(w==0 && h==0) break;
        rep(row,h){
            rep(col,w){
                cin >> c[row][col];
            }
        }

        int ans = 0;
        rep(row,h){
            rep(col,w){
                if(c[row][col]==1){
                    ans++;
                    dfs(row,col);
                }
            }
        }

        print(ans);
    }
}
 
int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
	cout<<fixed<<setprecision(20);
    //int q; cin >> q;
	//while (q--)
		solve();
    return 0;
}

C++: #include<iostream>
using namespace std;
const int MAXW = 51;
const int MAXH = 51;
int map[MAXW][MAXH];
int w,h;
int dx[] = {-1,0,1,-1,1,-1,0,1};
int dy[] = {1,1,1,0,0,-1,-1,-1};
int dfs(int x,int y){
  if(map[x][y] == 0)return 0;
  map[x][y] = 0;
  for(int i = 0;i < 9;i++){
    int nx = x+dx[i];
    int ny = y+dy[i];
    if(nx >= 0 && nx < w & ny >= 0 && ny < h)
      dfs(nx,ny);
  }
  return 1;
}

int main(){
  while(true){
    cin >> w >> h;
    if(w == 0 && h == 0)break;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        cin >> map[j][i];
      }
    }
    int count = 0;
    for(int i = 0;i < h;i++){
      for(int j = 0;j < w;j++){
        count += dfs(j,i);
      }
    }
    cout << count << endl;
  }
}

C++: #include<iostream>

using namespace std;

const int MaxI = 51;
int C[MaxI][MaxI];
int Dh[] = {-1,-1,-1,0,0,1,1,1};
int Dw[] = {-1,0,1,-1,1,-1,0,1};
int H, W;

void dfs(int i, int j);

int main() {

  while(cin>>W>>H && !(W==0 && H==0)) {
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	cin>>C[i][j];
      }
    }

    int ans = 0;
    for(int i=0; i<H; i++) {
      for(int j=0; j<W; j++) {
	if(C[i][j] == 1) {
	  dfs(i,j);
	  ans++;
	}
      }
    }
    cout<<ans<<endl;
  }
}

void dfs(int i, int j) {
  C[i][j] = 0;
  for(int a=0; a<8; a++) {
    if(0<=i+Dh[a] && i+Dh[a]<H && 0<=j+Dw[a] && j+Dw[a]<W && C[i+Dh[a]][j+Dw[a]] == 1) {
      dfs(i+Dh[a], j+Dw[a]);
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MOD (int) (1e9+7)
#define INF (int) 2e9

bool seen[50*50];
vector<int> edges[50*50];

void dfs(int s, int p){
    seen[s] = true;
    for (auto v: edges[s]){
        if(v==p) continue;
        if(!seen[v]) dfs(v, s);
    }
}


int main(){
    int w, h;
    int di[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dj[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
    
    while(true){
        cin >> w >> h;
        if(w==0 && h==0) break;
        int c[h][w];
        rep(i, h){
            rep(j, w){
                cin >> c[i][j];
                edges[w*i+j] = {};
            }
        }

        // 隣接リスト構築
        rep(i, h){
            rep(j, w){
                if(c[i][j]==1){
                    rep(k, 8){
                        int i_ = i+di[k];
                        int j_ = j+dj[k];
                        if(i_>=0 && i_<h && j_>=0 && j_<w && c[i_][j_]==1){
                            int idx = w * i_ + j_;
                            edges[w*i+j].emplace_back(idx);
                            // edges[idx].emplace_back(w*i+j);
                        }
                    }
                }
            }
        }

        rep(i, h){
            rep(j, w){
                seen[w*i+j] = false;
            }
        }

        // ここからdfs実行
        int cnt = 0;
        rep(i, h){
            rep(j, w){
                int num = w*i+j;
                if(c[i][j]==1 && !seen[num]){
                    dfs(num, -1);
                    cnt++;
                }
            }
        }
        cout << cnt << endl;
    }

    // printf("%d\n", N);
    return 0;
}