  We used near-infrared integral field spectroscopic observations from the
AMAZE and LSD programs to constrain the metallicity in a sample of 40 star
forming galaxies at 3<z<5 (most of which at z~3.4). We measure metallicities by
exploiting strong emission line diagnostics. We found that a significant
fraction of star-forming galaxies at z~3.4 deviate from the Fundamental
Metallicity Relation (FMR), with a metallicity up to a factor of ten lower than
expected according to the FMR. This deviation does not correlate with the
dynamical properties of the galaxy or with the presence of interactions. To
investigate the origin of the metallicity deviations in more detail, we also
infer information on the gas content, by inverting the Schmidt-Kennicutt
relation. In agreement with recent CO observational data, we found that, in
contrast with the steeply rising trend at 0<z<2, the gas fraction in massive
galaxies remains constant, with indication of a marginal decline, at 2<z<3.5.
When combined with the metallicity information, we infer that to explain both
the low metallicity and gas content in z~3.4 galaxies, both prominent outflows
and massive pristine gas inflows are needed. In ten galaxies we can also
spatially resolve the metallicity distribution. We found that the metallicity
generally anticorrelates with the distribution of star formation and with the
gas surface density. We discuss these findings in terms of pristine gas inflows
towards the center, and outflows of metal rich gas from the center toward the
external regions. (Abridged)
