Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

def rec(y, x, used, TILE):
    if (not(0<=x<W)) or (not(0<=y<H)) or (TILE[y][x]==0) or (used[y][x]==1):
        return 0
    else:
        used[y][x] = 1
        rec(y-1, x-1, used, TILE)
        rec(y-1, x, used, TILE)
        rec(y-1, x+1, used, TILE)
        rec(y, x-1, used, TILE)
        rec(y, x, used, TILE)
        rec(y, x+1, used, TILE)
        rec(y+1, x-1, used, TILE)
        rec(y+1, x, used, TILE)
        rec(y+1, x+1, used, TILE)
    return 1

(W, H) = map(int, raw_input().split())
while W!=0:
    used = [[0]*W for i in range(H)]
    TILE = []
    s = []
    for i in range(H):
        TILE.append(map(int, raw_input().split()))

    Sum = 0
    for i in range(H):
        for j in range(W):
            Sum += rec(i, j, used, TILE)
    print Sum
    #print used.count(1)

    (W, H) = map(int, raw_input().split())

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

def rec(y, x, used, TILE):
    if (not(0<=x<W)) or (not(0<=y<H)) or (TILE[y][x]==0) or (used[y][x]==1):
        return 0
    else:
        used[y][x] = 1
        rec(y-1, x-1, used, TILE)
        rec(y-1, x, used, TILE)
        rec(y-1, x+1, used, TILE)
        rec(y, x-1, used, TILE)
        rec(y, x, used, TILE)
        rec(y, x+1, used, TILE)
        rec(y+1, x-1, used, TILE)
        rec(y+1, x, used, TILE)
        rec(y+1, x+1, used, TILE)
    return 1

(W, H) = map(int, raw_input().split())
while W!=0 :
    used = [[0]*W for i in range(H)]
    TILE = []
    stack = []
    for i in range(H):
        TILE.append(map(int, raw_input().split()))
    Sum = 0
    for i in range(H):
        for j in range(W):
            if used[i][j] == 0 and TILE[i][j] == 1:
                Sum += 1
                stack.append([i, j])
                while len(stack)>0:
                    cur = stack[len(stack)-1]
                    stack.pop()
                    for y in [-1, 0, 1]:
                        for x in [-1, 0, 1]:
                            if 0<=cur[0]+y<H and 0<=cur[1]+x<W and TILE[cur[0]+y][cur[1]+x]==1 \
                                    and used[cur[0]+y][cur[1]+x]==0:
                                stack.append([cur[0]+y, cur[1]+x])
                                used[cur[0]+y][cur[1]+x] = 1

    """for i in range(H):
        for j in range(W):
            if used[i][j] == 0 and TILE[i][j] == 1:
                Sum += rec(i, j, used, TILE)
    print Sum"""
    print Sum
    #print used.count(1)

    (W, H) = map(int, raw_input().split())

Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

def rec(y, x, used, TILE):
    if (not(0<=x<W)) or (not(0<=y<H)) or (TILE[y][x]==0) or (used[y][x]==1):
        return 0
    else:
        used[y][x] = 1
        rec(y-1, x-1, used, TILE)
        rec(y-1, x, used, TILE)
        rec(y-1, x+1, used, TILE)
        rec(y, x-1, used, TILE)
        rec(y, x, used, TILE)
        rec(y, x+1, used, TILE)
        rec(y+1, x-1, used, TILE)
        rec(y+1, x, used, TILE)
        rec(y+1, x+1, used, TILE)
    return 1

(W, H) = map(int, raw_input().split())
while W!=0 :
    used = [[0]*W for i in range(H)]
    TILE = []
    for i in range(H):
        TILE.append(map(int, raw_input().split()))

    Sum = 0
    for i in range(H):
        for j in range(W):
            if used[i][j] == 0 and TILE[i][j] == 1:
                Sum += rec(i, j, used, TILE)
    print Sum
    #print used.count(1)

    (W, H) = map(int, raw_input().split())

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

def rec(y, x, used, TILE):
    if (not(0<=x<W)) or (not(0<=y<H)) or (TILE[y][x]==0) or (used[y][x]==1):
        return 0
    else:
        used[y][x] = 1
        rec(y-1, x-1, used, TILE)
        rec(y-1, x, used, TILE)
        rec(y-1, x+1, used, TILE)
        rec(y, x-1, used, TILE)
        rec(y, x, used, TILE)
        rec(y, x+1, used, TILE)
        rec(y+1, x-1, used, TILE)
        rec(y+1, x, used, TILE)
        rec(y+1, x+1, used, TILE)
    return 1

(W, H) = map(int, raw_input().split())
while W!=0 :
    used = [[0]*W for i in range(H)]
    TILE = []
    stack = []
    for i in range(H):
        TILE.append(map(int, raw_input().split()))
    Sum = 0
    for i in range(H):
        for j in range(W):
            if used[i][j] == 0 and TILE[i][j] == 1:
                Sum += 1
                stack.append([i, j])
                while len(stack)>0:
                    cur = stack[len(stack)-1]
                    stack.pop()
                    for y in [-1, 0, 1]:
                        for x in [-1, 0, 1]:
                            if 0<=cur[0]+y<H and 0<=cur[1]+x<W and TILE[cur[0]+y][cur[1]+x]==1 \
                                    and used[cur[0]+y][cur[1]+x]==0:
                                stack.append([cur[0]+y, cur[1]+x])
                                used[cur[0]+y][cur[1]+x] = 1

    """for i in range(H):
        for j in range(W):
            if used[i][j] == 0 and TILE[i][j] == 1:
                Sum += rec(i, j, used, TILE)
    print Sum"""
    print Sum
    #print used.count(1)

    (W, H) = map(int, raw_input().split())