As others have stated, the problem discussed is not possible, since 600 bytes are required to represent all possible grids. 600 bytes - of 40 rows, 40 columns, 3 bits per cell and 8 bits per byte ( 40 * 40 * 3 / 8 ). As Kerrek SB explained in the comments, you add 8 cells in 3 bytes.
In your own comments, you mentioned that this state of the game is transmitted over the network. Assuming you have a mechanism to ensure reliable data transport, then if there is a reasonable limit to the number of cells that can change between updates, or if you are allowed to send your updates when a certain number of cells change, you can get a 512 byte representation. If you use 1 bit to represent whether a cell has changed, you will need 200 bytes. Then you have 312 bytes remaining to represent the new values โโof the changed cells. Thus, you can represent up to 312*8/3 = 832 changed cells.
Aside, this view can represent up to 1064 changed cells in less than 600 bytes.
source share