function grid = life_update (  m, n, grid )

%*****************************************************************************80
%
%% life_update() updates a Life grid.
%
%  Discussion:
%
%    We assume the MxN grid is surrounded by a boundary layer of 0 cells.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    09 September 2013
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, N, the number of rows and columns of interior grid cells.
%
%    integer GRID(1+M+1,1+N+1), the data.
%
%  Output:
%
%    integer GRID(1+M+1,1+N+1), the data.
%
  s = zeros ( m, n );

  for j = 1 : n
    for i = 1 : m
      s(i,j) = grid(i,  j) + grid(i,  j+1) + grid(i,  j+2) ...
             + grid(i+1,j)                 + grid(i+1,j+2) ...
             + grid(i+2,j) + grid(i+2,j+1) + grid(i+2,j+2);
    end
  end
%
%  Any dead cell with 3 live neighbors becomes alive.
%  Any living cell with less than 2 or more than 3 neighbors dies.
%
  for j = 1 : n
    for i = 1 : m
      if ( grid(i+1,j+1) == 0 )
        if ( s(i,j) == 3 )
          grid(i+1,j+1) = 1;
        end
      elseif ( grid(i+1,j+1) == 1 )
        if ( s(i,j) < 2 || 3 < s(i,j) )
          grid(i+1,j+1) = 0;
        end
      end

    end
  end

  return
end
