AI approach for exploring a static grid environment
Obviously one of the best learning methods that matches this problem is the Reinforcement Learning and to be more specific Q-Learning.
You'll find a good starting point here.
Hints: Q-learning is quite well-known and I don't repeat others here, but I give you the key point here.

At each step keep track of what happens (reward/punishment - no need to agent types,... if the environment stays fixed and you want to learn this Env. not the agents) using for example an array (Fortunately you're not learning anything so far!). Keep doing this until the agent stops, that as you said can have two reasons :


Agent's health drops to zero : Do nothing! (discard the array)
Agent leaves from the other corner : Apply the following rule with the values in the array