Well done!
There is a small misunderstanding:
This line doesn’t position each button at center of each cell.
it just moves (translates) everything (the entire grid) to the right and to the left by 50.
The center positioning is done here in fact:
rectMode(CENTER); // draw rect from ctr
rect (x, y, sz, sz); //sz, sz);
Explanation
because you inserted this line rectMode(CENTER) the x,y position of each cell is now not the upper right corner anymore but the center of the cell (rect command is seeing x,y now as the center of each rectangle). Therefore everything was too far up and too far left.
Therefore, you did the translate() to compensate that.
Instead you have to change these lines:
int x = 40+50, y = x, // dist from screen border
(50 is half of the width and therefore the difference between center and upper left corner)
Then you can delete
translate( 50, 50); // position each button at center of each cell
Regards, Chrisir