Extruding problem

The image is a work in progress. It consists of a large number of extruded cubes. All the cubes are joined in a single mesh. (It’s a mathimatical representation of a photograph)
Here’s the problem: making this object by using the extrude option turned it into a non manifold nightmare. The base of the cubes form a nice unified mesh. But the tops are not attahced to each other. I have included an image to illustrate my point:

In the image I moved the top of one of the cubes to the left. So the cubes are all right next to each other but not correctly joined. (hope it’s clear what I am trying to say)

So basically I have 3 questions:

  • How can I extrude faces without running into this problem?
  • Is there a better way to make this object?
  • Is there an efficient way to fix my current object?

Add a new plane, then add a remesh modifier, and set it to cube. Dont apply it, and now, go into edditmode and subdevide it, and start moving the vertexes. Is this what you need? Also plah with the setting in the modifierPs. Im on my phone so havent tested this myself