Why this code is showing runtime error?

import java.io.*;
class poll
{
public static void main(String args[])throws IOException
{
String s,s1;
char c;
int T,l,M,S;
DataInputStream in=new DataInputStream(System.in);
T=Integer.parseInt(in.readLine());
for(int j=1;j<=T;j++)
{
M=0;S=0;l=0;s="";s1="";
s1=in.readLine();
s=s1+’ ';
l=s.length();
for(int i=0;i<l;i++)
{
c=s.charAt(i);
if(c==‘s’)
{
S=S+1;
}
if(c==‘m’)
{
M=M+1;
if(s.charAt(i+1)==‘s’||s.charAt(i-1)==‘s’)
{
S=S-1;
}
}
}
if(M==S)
System.out.println(“tie”);
if(M>S)
System.out.println(“mongooses”);
if(S>M)
System.out.println(“snakes”);
}
}
}

You get runtime error at these type of test cases-

Input
1
mmmmmmm

Error message makes it clear why,

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: **String index out of range: -1**
	at java.lang.String.**charAt**(String.java:646)
	**at poll.main(Main.java:27)**

if(s.charAt(i+1)=='s'||s.charAt(i-1)=='s') Add conditions such that it doesnt check out of index. For example, if m is the last character of string, then i+1 is out of index. Similarly if m is first character of string, i-1 will be out of index.