      PROGRAM LIFE
      IMPLICIT NONE
      
      INTEGER I, J, DIM
      PARAMETER (DIM = 5)
      CHARACTER*1 BOARD1(DIM, DIM)
      DATA BOARD1 /25 * ' '/
      CHARACTER*1 BOARD2(DIM, DIM)
      
      INTEGER NEIGHBORS, N, ITER
      
      BOARD1(2, 2) = 'X'
      BOARD1(2, 4) = 'X'
      BOARD1(3, 3) = 'X'
      BOARD1(4, 3) = 'X'
      BOARD1(5, 2) = 'X'
      BOARD1(5, 4) = 'X'

      PRINT *, 'INITIAL CONFIGURATION'          
      CALL PRINTM(BOARD1, DIM)

      DO 60 ITER = 1, 10
        DO 20 I = 1, DIM
          DO 30 J = 1, DIM
            N = NEIGHBORS(BOARD1, I, J, DIM)
            IF (BOARD1(I, J) .EQ. ' ') THEN
              IF (N .EQ. 3) THEN
                BOARD2(I, J) = 'X'
              ELSE
                BOARD2(I, J) = ' '
              ENDIF    
            ELSE 
              IF (N .GE. 4 .OR. N .LE. 1) THEN
                BOARD2(I, J) = ' '
              ELSE
                BOARD2(I, J) = 'X'
              ENDIF    
            ENDIF  
30        CONTINUE
20      CONTINUE        
      
        DO 40 I = 1, DIM
          DO 50 J = 1, DIM
            BOARD1(I, J) = BOARD2(I, J)
50        CONTINUE
40      CONTINUE

        PRINT *, 'AFTER ', ITER, ' ITERATIONS' 
        CALL PRINTM(BOARD1, DIM)
        PRINT *, ' '
        
60      CONTINUE           

      STOP
      END
      
      
      
      SUBROUTINE PRINTM(MATRIX, DIM)
      INTEGER DIM
      CHARACTER*1 MATRIX(DIM, DIM)
      
      INTEGER I, J

      DO 10 I = 1, DIM
        PRINT *, (MATRIX(I, J), J = 1, DIM)
10    CONTINUE
        
      RETURN
      END
      
      
      
      INTEGER FUNCTION NEIGHBORS(BOARD, ROW, COL, DIM)
      IMPLICIT NONE
      INTEGER ROW, COL, DIM
      CHARACTER*1 BOARD(DIM, DIM)
      
      INTEGER XDIR(8)
      INTEGER YDIR(8)
      DATA XDIR /1, 1, 0, -1, -1, -1, 0, 1/
      DATA YDIR /0, 1, 1, 1, 0, -1, -1, -1/
      
      INTEGER I, N, NX, NY
      
      N = 0
      DO 10 I = 1, 8
        NX = ROW + XDIR(I)
        NY = COL + YDIR(I)
        IF (NX .GE. 1 .AND. NX .LE. DIM .AND. 
     +      NY .GE. 1 .AND. NY .LE. DIM) THEN
          IF (BOARD(NX, NY) .EQ. 'X') THEN
            N = N + 1
          ENDIF  
        ENDIF
10    CONTINUE    

      NEIGHBORS = N
      RETURN
      
      END      