Go: ///
// File:  1160.go
// Author: ymiyamoto
//
// Created on Sun Mar  4 20:45:57 2018
//
package main

import (
	"fmt"
)

var w, h int
var graph [][]string

func dfs(visited [][]bool, x, y int) {
	visited[x][y] = true

	for i := -1; i <= 1; i++ {
		for j := -1; j <= 1; j++ {
			dx := x + i
			dy := y + j
			if 0 <= dx && dx < h && 0 <= dy && dy < w && graph[dx][dy] == "1" && !visited[dx][dy] {
				dfs(visited, dx, dy)
			}
		}
	}
}

func main() {
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			break
		}

		graph = make([][]string, h)
		for i := range graph {
			graph[i] = make([]string, w)
		}

		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				var s string
				fmt.Scan(&s)
				graph[i][j] = s
			}
		}

		visited := make([][]bool, h)
		for i := range visited {
			visited[i] = make([]bool, w)
		}

		count := 0
		for i := range graph {
			for j := range graph[i] {
				if graph[i][j] == "1" && !visited[i][j] {
					dfs(visited, i, j)
					count++
				}
			}
		}

		fmt.Println(count)
	}
}

Python: def dfs():
    while len(stack) > 0:
        height, width = stack.pop()
        for k in range(-1, 2):
            for l in range(-1, 2):
                new_h, new_w = height + k, width + l
                if new_h < 0 or new_w < 0 or new_h >= h or new_w >= w:
                    continue
                #訪れていない場所で，そこが島だったら
                if visited[new_h][new_w] == 0 and c[new_h][new_w] == 1:
                    visited[new_h][new_w]  = 1
                    stack.append([new_h, new_w])

    return 1

#実行部分
while True:
    w, h = map(int, input().split())
    #入力が終わるとき
    if w == 0 and h == 0:
        break
    else:
        c = [list(map(int, input().split())) for _ in range(h)]
        visited = [[0] * w for _ in range(h)]
        ans = 0

        #実行部分
        for i in range(h):
            for j in range(w):
                if visited[i][j] == 0 and c[i][j] == 1:
                    visited[i][j] = 1
                    stack = [[i, j]]
                    ans += dfs()
        print(ans)

Python: import sys
sys.setrecursionlimit(10 ** 9)
while True:
    w, h = map(int, input().split())
    if w == 0 and h == 0:
        break
    c = [list(map(int, input().split())) for _ in range(h)]
    seen = [[False for _ in range(w)] for _ in range(h)]

    def dfs(x, y, seen):
        if not (0 <= x < h and 0 <= y < w):
            return
        if seen[x][y]:
            return
        if c[x][y] == 0:
            return
        seen[x][y] = True
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                dfs(x + dx, y + dy, seen)

        return seen

    cnt = 0
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1 and seen[i][j] == False:
                cnt += 1
                dfs(i, j, seen)

    print(cnt)

Python: import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]

class Ans():

    def __init__(self, H, W, mat):
        self.H = H
        self.W = W
        self.mat = mat
        self.visited = [[False] * W for _ in range(H)]

    def dfs(self, h, w):
        if self.mat[h][w] == 0 or self.visited[h][w] == True:
            return
        
        self.visited[h][w] = True
        for dh, dw in directions:
            if not (0 <= h + dh < self.H and 0 <= w + dw < self.W):
                continue
            if self.mat[h+dh][w+dw] == 0 or self.visited[h+dh][w+dw] == True:
                continue
            self.dfs(h+dh, w+dw)
        return

def main():
    while 1:
        W, H = map(int, readline().rstrip().split())
        if W == H == 0:
            break

        mat = [list(map(int, readline().rstrip().split())) for _ in range(H)]
        ans = Ans(H, W, mat)

        res = 0
        for h in range(H):
            for w in range(W):
                if ans.mat[h][w] == 1 and ans.visited[h][w] == False:
                    res += 1
                ans.dfs(h, w)

        print(res)

if __name__ == '__main__':
    main()

Python: import sys
sys.setrecursionlimit(1000000)
moves = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1))

def dfs(y, x):
  c[y][x] = 0

  for dy, dx in moves:
    ny = y + dy
    nx = x + dx 
    if 0 <= ny < h and 0 <= nx < w and c[ny][nx] == 1:
      dfs(ny, nx)

while True:
  w, h = map(int, input().split())
  if w == 0 and h == 0:
    break
  c = [list(map(int, input().split())) for _ in range(h)]
  ans = 0
  for i in range(h):
    for j in range(w):
      if c[i][j] == 1:
        dfs(i, j)
        ans += 1
  print(ans)

Python: import sys
sys.setrecursionlimit(100000)


def dfs(h, w):
    checked[h][w] = 1
    for hi in range(-1, 2):
        for wj in range(-1, 2):
            if 0 <= h+hi <= H-1 and 0 <= w+wj <= W-1:
                if hw[h+hi][w+wj] == 1 and checked[h+hi][w+wj] == -1:
                    dfs(h+hi, w+wj)


while True:
    W, H = list(map(int, input().split()))
    if W == 0 and H == 0:
        break

    hw = []
    for _ in range(H):
        tmp = list(map(int, input().split()))
        hw.append(tmp)

    checked = [[-1] * W for i in range(H)]
    ans = 0
    for w in range(W):
        for h in range(H):
            if hw[h][w] == 1 and checked[h][w] == -1:
                ans += 1
                dfs(h, w)
    print(ans)

Python: #AOJ1160
def search(w,h,data):
    for i in range(h):
        if 1 in data[i]:
            return (i,data[i].index(1))
    return (-1,-1)

def main(w,h):
    data=[list(map(int,input().split())) for j in range(h)]
    start=search(w,h,data)
    island=0
    while start!=(-1,-1):
        island+=1
        checking=[start]
        data[start[0]][start[1]]=0
        while len(checking)>0:
            now=checking.pop()
            for j in range(now[0]-1,now[0]+2):
                for k in range(now[1]-1,now[1]+2):
                    if (j,k)!=now and j>=0 and k>=0 and j<h and k<w:
                        if data[j][k]==1:
                            data[j][k]=0
                            checking.append((j,k))
        start=search(w,h,data)
    return island          

w,h=map(int,input().split())
while w!=0 and h!=0:
    print(main(w,h))
    w,h=map(int,input().split())

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)

while True:
    w, h = map(int,input().split())
    if w == 0 and h == 0 :
        break
    
    c = [list(map(int,input().split())) for j in range(h)]
    count = 0
    
    for i in range(h):
        for j in range(w):
            if (c[i][j] == 1):
                dfs(i,j)
                count += 1

    print(count)