Are there Zen temples in Japan that allow staying there for a few months for meditation for foreigners? How much does that cost? Are there free ones? How about visa?
Japanese temples don't provide place for meditation for random people, it's only for real followers and pilgrims. Several temples allow to stay for a night only for a fee, again you can only see ceremonies from side. All temples allowing tourists I know are not Zen as it's not the main school in Japan. But they require booking and have queue, they prefer real pilgrims than tourists. You need at least tourist visa.
There is one Zen Temple which does English in Kyoto.
I would suggest you contact them about long term arrangements.
If you are a student and under 30, with some Japanese proficiency. http://nagaokazenjuku.or.jp/english/index.html#sixth
Can't make it to Japan? http://global.sotozen-net.or.jp/eng/temples/outside_jp/index.html